Some times we have to use our own Modal binding mechanism in MVC. In MVC default Modal binding machansim is perfect. It binds our modal to posted variables . We can create our custom Modal binder. We can use DefaultModalBinder Or IModalBinder to do that. In following video you can find Custom ModalBinder Using IModalBinder.
Month: April 2016
Reading excel file in c#
Some times Reading Excel files can be bit cumbersome. I needed to allow user to upload excel document and read data from excel file. So I followed following way to get it done.
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
xlWorkBook = xlApp.Workbooks.Open(Application.StartupPath + "\\sample.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
// read data and bind to grid view
System.Data.DataTable dt = new System.Data.DataTable("dtExcel");
ds.Tables.Add(dt);
DataRow dr;
StringBuilder sb = new StringBuilder();
int jValue = xlWorkSheet.UsedRange.Cells.Columns.Count;
int iValue = xlWorkSheet.UsedRange.Cells.Rows.Count;
for (int j = 1; j <= jValue; j++)
{
dt.Columns.Add("column" + j, System.Type.GetType("System.String"));
}
for (int i = 1; i <= iValue; i++)
{
dr = ds.Tables["dtExcel"].NewRow();
for (int j = 1; j <= jValue; j++)
{
range = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i, j];
string strValue = range.Text.ToString();
dr["column" + j] = strValue;
}
ds.Tables["dtExcel"].Rows.Add(dr);
}
dataGridView1.DataSource = ds.Tables[0];
xlWorkBook.Close(true, null, null);
xlApp.Quit();
// release object
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
simple amazing popup menu
So I just created simple but nice menu in code pen using jquery and css. You can find it in below
Difference between == and === in JavaScript
So we know in javascript we have == and === comparisons. Why two? What are the differences between them?
So what is the difference between followings
'1' == 1
this returns true.
when it comes === equals
'1' === 1
this returns false.
So what it is?
The triple-equals comparator compares both type and contents
So ‘1’ === 1 contains string value and int value. Even contents are same they are in different types. So it is not equals.
Double equals tries to help with type coercion. But it’s not always the help we want. So keep in mind when you are using above equal comparisons.
format currency in jquery
So when we are in web development we want show Currency value with some formatting. Like thousand separator, decimal separator and currency symbol.
I am using below Jquery function to format the currency values.
function formatCurrency(value, decimals, decpoint, separator, symbol) {
decimals = (typeof decimals == 'undefined' ? 2 : decimals);
decpoint = (typeof decpoint == 'undefined' ? '.' : decpoint);
separator = (typeof separator == 'undefined' ? ',' : separator);
symbol = (typeof symbol == 'undefined' ? '$' : symbol);
var parts = value.toFixed(decimals).toString().split(decpoint);
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, separator);
return (symbol + parts.join(decpoint)).toLocaleString();
}
We can format currency value as we required. If we just pass the currency value it will give us value with default formatting.
formatCurrency(5873839) will give us ” $5,873,839.00″
bootstrap accordion examples
So I wanted to integrate Bootstrap according to a website. Then I created enhanced Bootstrap according. Below you can find a Code pen. I think it might help you.
reading xml in sql server
So some times we want to read xml file in sql server. I am passing list of details to sql server using xml file format. It reduce several db calls when we passing list of data.
Suppose we have List of Students. In List of Student we have to loop it and fire db calls to save data. To overcome this I create xml of List of Student and Pass it to sql server with one db call and then in my stored procedure I will handle those data as follow.
DECLARE @Xml XML DECLARE @HDoc INT SET @Xml = '<ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Student> <Id>1</Id> <FirstName>jeevan</FirstName> <LastName>JLast name</LastName> </Student> <Student> <Id>2</Id> <FirstName>john</FirstName> <LastName>johnLast name</LastName> </Student> </ArrayOfStudent>' EXECUTE SP_XML_PREPAREDOCUMENT @HDoc OUTPUT,@Xml SELECT [Id], [FirstName], [LastName] INTO #Temp FROM OPENXML(@HDoc, '/ArrayOfStudent/Student', 1) WITH ( [Id] INT 'Id', [FirstName] VARCHAR(400) 'FirstName', [LastName] VARCHAR(400) 'LastName' ) EXECUTE SP_XML_REMOVEDOCUMENT @HDoc SELECT * FROM #Temp

Serializing Lists of Classes to XML
So some times we want to convert object to xml. We can follow severel cumbersome methods. So I am using Utlity methods to convert c# object to xml and xml to c# object.
Suppose we have class like below
public class Student
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
And we want serialize List<Student> to xml. So we can use this utlity function
/// <summary>
/// Serialize object to xml
/// </summary>
/// <typeparam name="T">Object type</typeparam>
/// <param name="obj">Object</param>
/// <returns>XML</returns>
public static string ToXml<T>(T obj)
{
using (StringWriter stringWriter = new StringWriter(new StringBuilder(), CultureInfo.InvariantCulture))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(stringWriter, obj);
return stringWriter.ToString();
}
}
As a example we populate List<student> with dummy data
List <Student> studentList = new List<Student>();
studentList.Add(new Student {
FirstName = "jeevan",
LastName = "JLast name",
Id = 1
});
studentList.Add(new Student
{
FirstName = "john",
LastName = "johnLast name",
Id = 2
});
Then call out utility function
string xml = Utility.ToXml(studentList);
then out xml variable look like as below
<?xml version="1.0" encoding="utf-16"?> <ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Student> <Id>1</Id> <FirstName>jeevan</FirstName> <LastName>JLast name</LastName> </Student> <Student> <Id>2</Id> <FirstName>john</FirstName> <LastName>johnLast name</LastName> </Student> </ArrayOfStudent>