Wednesday, July 29, 2009

Abstract Class vs Interface Table





































Feature Interface Abstract Class
Multiple inheritance A class may inherit severalinterfaces A class may inherit only one abstract class
Default implementation An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden.
Constants Only Static final constants Both instance and static constants are possible
Speed Requires more time to find the actual method in the corresponding classes. Fast
Adding functionality If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Homogeneity If the various implementations only share method signatures then it is better to use Interface If the various implementations abstract class then we have the common behavior or status then abstract class is better to use.

Loading Data by Generic List using stored procedure with DataTable and DataReader

In the above code snipeet, I have two method called LoadDataThroughDataReader and LoadDataThroughDataTable. In both method, I have declared a variable for List that is filled with the Person records returned from the database. The first method LoadDataThroughDataReader use
cmd.ExecuteReader() method to get the SqlDataReader and read through all the records in the reader and add the Person object into the List.


The second method is slightly different and use DataTable instead of DataReader to fetch data from database, loop through all the rows of the DataTable and add Person objects into the list.

You may wonder why these two methods when both are doing the same thing. I prefer using DataReader when I have lesser amount of data from the database and loop through because DataReader expect live connection when it loops through all the records. For large amount of data, it may be a bottleneck for the database to keep the connection alive and loop through. So for large amount of data I prefer to fill the DataTable, close the database connection and then loop through all the records do the required operation.

public List LoadThroughDataReader()
{
var list = new List();
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand("LoadAll", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Person p = new Person()
{
Age = reader.GetInt32(reader.GetOrdinal("Age")),
FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
LastName = reader.GetString(reader.GetOrdinal("LastName"))
};
list.Add(p);
}
}
conn.Close();
}
return list;
}
}

public List LoadThroughDataTable()
{
var list = new List();
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand("LoadAll", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
DataTable table = new DataTable();
conn.Open();
// open the connection
ad.Fill(table);
conn.Close();
// close the connection
// loop through all rows
foreach (DataRow row in table.Rows)
{
Person p = new Person()
{
Age = int.Parse(row["Age"].ToString()),
FirstName = row["FirstName"].ToString(),
LastName = row["LastName"].ToString()
};
list.Add(p);
}
}
}
return list;
}
}

Wednesday, July 15, 2009

Javascript Split method

<body>
<form id="Form1" runat="server">
<asp:TextBox ID="tbxabc" runat="server" />
<br />
<asp:Label ID="lblFirstName" runat="server" Text="">
<br />
<asp:Label ID="lblSecondName" runat="server" Text="">
<input type="button" id="Calculate" value="Split" onclick="split()" />
</form>

<script type="text/javascript">
function split() {
var i;
var abc = document.getElementById('tbxabc').value;
for (i = 0; i < abc.length; i++) {
alert(abc[i]);
}
}
</script>
</body>

//For example i enter my name in the Textbox for example sheeban
//In the pop up each and every charachter will pop up

Monday, July 13, 2009

Get the value of Selected Row From Gridview

//Just Before the page load define the variable
string textBoxCompanyName, textBoxContactName, textBoxCustomerID;
//Then on the button click event get the values from the grid
//Here dvCustomerDetail is the name of a Gridview
foreach (GridViewRow row in dvCustomerDetail.Rows)
{
textBoxCustomerID = ((TextBox)row.FindControl("tbxCustomerID")).Text;
textBoxCompanyName = ((TextBox)row.FindControl("tbxCompanyName")).Text;
textBoxContactName = ((TextBox)row.FindControl("tbxContactName")).Text;
}