.

Wednesday, August 3, 2011

Sample Nested Grid

private static string connString = "Data Source=RxDotnet;Initial Catalog=iCATCloud;User ID=dotnet;Password=dotnet;";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Category", conn);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
dt1.DataSource = ds;
dt1.DataBind();
conn.Close();
}
}
protected void dt1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox chkData = (CheckBox)e.Item.FindControl("dtlchkBox");
int i = e.Item.ItemIndex;
string CurrentId = dt1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Item where CategoryId=" + CurrentId, conn);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
GridView gv1 = (GridView)e.Item.FindControl("gv1");
gv1.DataSource = ds;
gv1.DataBind();
conn.Close();
}
}
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string CurrentItemId = ((Label)e.Row.FindControl("lblItemId")).Text;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Model where ItemId=" + CurrentItemId, conn);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
GridView gv2 = (GridView)e.Row.FindControl("gv2");
gv2.DataSource = ds;
gv2.DataBind();
conn.Close();
}
}
protected void btnCheckAll_Click(object sender, EventArgs e)
{
for (int cat = 0; cat < dt1.Items.Count; cat++)
{
CheckBox dtlchkBox = (CheckBox)dt1.Items[cat].FindControl("dtlchkBox");
dtlchkBox.Checked = true;
GridView gv1 = (GridView)dt1.Items[cat].FindControl("gv1");
for (int i = 0; i < gv1.Rows.Count; i++)
{
//if (i == 0)
//{
// CheckBox gv1All = (CheckBox)gv1.HeaderRow.FindControl("gv1All");
// gv1All.Checked = true;
//}
CheckBox gvbox = (CheckBox)gv1.Rows[i].FindControl("gvbox");
gvbox.Checked = true;
GridView gv2 = (GridView)gv1.Rows[i].FindControl("gv2");
for (int mod = 0; mod < gv2.Rows.Count; mod++)
{
if (mod == 0)
{
CheckBox gv2All = (CheckBox)gv2.HeaderRow.FindControl("gv2All");
gv2All.Checked = true;
}
CheckBox gv2box = (CheckBox)gv2.Rows[mod].FindControl("gv2box");
gv2box.Checked = true;
}
}
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
 
for (int cat = 0; cat < dt1.Items.Count; cat++)
{
CheckBox dtlchkBox = (CheckBox)dt1.Items[cat].FindControl("dtlchkBox");
if (dtlchkBox.Checked)
{
Response.Write("DataList Id : " + dt1.DataKeys[cat].ToString());
Response.Write("
");
}
else
{
GridView gv1 = (GridView)dt1.Items[cat].FindControl("gv1");
for (int i = 0; i < gv1.Rows.Count; i++)
{
CheckBox gvbox = (CheckBox)gv1.Rows[i].FindControl("gvbox");
if (gvbox.Checked)
{
Response.Write("Grid 1 (Delete Item) : " + ((Label)gv1.Rows[i].FindControl("lblItemName")).Text);
Response.Write("
");
}
else
{
GridView gv2 = (GridView)gv1.Rows[i].FindControl("gv2");
for (int mod = 0; mod < gv2.Rows.Count; mod++)
{
if (mod == 0)
{
CheckBox gv2All = (CheckBox)gv2.HeaderRow.FindControl("gv2All");
if (gv2All.Checked)
{
Response.Write("Grid 2 (Delete All Model) : ");
Response.Write("
");
break;
}
}
CheckBox gv2box = (CheckBox)gv2.Rows[mod].FindControl("gv2box");
if (gv2box.Checked)
{
Response.Write("Grid 2 (Delete Model ) : " + ((Label)gv2.Rows[mod].FindControl("lblModname")).Text);
Response.Write("
");
}
}
}//*********
}
}//*********
}
}

Friday, July 29, 2011

Advantages and Disadvantages of LINQ

LINQ (Language Integrated Query) is a Microsoft programming model and methodology that gives a formal query capabilities into Microsoft .NET-based programming languages.

It offers a compact, expressive, and intelligible syntax for manipulating data. The real value of LINQ comes is to apply the same query to an SQL database, a DataSet, an array of objects in memory and to many other types of data as well. It requires the presence of specific language extensions.

LINQ may be used to access all types of data, whereas embedded SQL is limited to addressing only databases that can handle SQL queries.

Below are the advantages and Disadvantages of LINQ


Advantages:


It is a cleaner and typesafety.
It is checked by the compiler instead of at runtime
It can be debugged easily.
It can be used against any datatype - it isn't limited to relational databases, you can also use it against XML, regular objects.
It can able to query not just tables in a relational database but also text files and XML files.

Disadvantages:


LINQ sends the entire query to the DB hence takes much network traffic but the stored procedures sends only argument and the stored procedure name. It will become really bad if the queries are very complex.
Preformance is degraded if we don't write the linq query correctly.
If you need to make changes to the way you do data access, you need to recompile, version, and redeploy your assembly.

.