Thursday, June 24, 2010

How to create a Dynamic DataTable:

I have a requirement to create a dynamic data table for the shopping cart. When I search in Google I find some coding . I alter that coding change for my requirement.

Here you can create the data type and column name

private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();

DataColumn myDataColumn;

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Productname";
myDataTable.Columns.Add(myDataColumn);



myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.Decimal");
myDataColumn.ColumnName = "Quantity";
myDataTable.Columns.Add(myDataColumn);


myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.Decimal");
myDataColumn.ColumnName = "Productid";
myDataTable.Columns.Add(myDataColumn);





return myDataTable;
}



private void AddDataToTable(string PrdName, decimal Qty,decimal id, DataTable myTable)
{
DataRow row;

row = myTable.NewRow();


row["Productname"] = PrdName;

row["Quantity"] = Qty;

row["Productid"] = id;


myTable.Rows.Add(row);

}


Remove the duplicates from datatable

private static void RemoveDuplicates(DataTable tbl,

DataColumn[] keyColumns)
{

int rowNdx = 0;

while (rowNdx < tbl.Rows.Count - 1) { DataRow[] dups = FindDups(tbl, rowNdx, keyColumns); if (dups.Length > 0)
{

foreach (DataRow dup in dups)
{

tbl.Rows.Remove(dup);

}

}

else
{

rowNdx++;

}

}

}




private static DataRow[] FindDups(DataTable tbl,

int sourceNdx,

DataColumn[] keyColumns)
{

ArrayList retVal = new ArrayList();



DataRow sourceRow = tbl.Rows[sourceNdx];

for (int i = sourceNdx + 1; i < tbl.Rows.Count; i++) { DataRow targetRow = tbl.Rows[i]; if (IsDup( targetRow,sourceRow, keyColumns)) { retVal.Add(sourceRow); } } return (DataRow[])retVal.ToArray(typeof(DataRow)); } private static bool IsDup(DataRow sourceRow, DataRow targetRow,DataColumn[] keyColumns) { bool retVal = true; foreach (DataColumn column in keyColumns) { retVal = retVal && sourceRow[column].Equals(targetRow[column]); if (!retVal) break; } return retVal; }


/* Here you can pass the values to create a row */

AddDataToTable(strname, txtid, decimal.Parse(strprodid), (DataTable)Session["myDatatable"]);


/* remove the duplicate from datatable */
DataTable Dtcount = new DataTable();

Dtcount = (DataTable)Session["myDatatable"];

DataColumn[] keyColumns = new DataColumn[]
{Dtcount.Columns[2], Dtcount.Columns[2]};

RemoveDuplicates(Dtcount, keyColumns);

Here I can bind the Datatable values in datalist to show the shopping cart.

In that datalist I use text box to update the quantity.

In the txt_TextChanged I write the coding to update the data table.


TextBox tb1 = ((TextBox)(sender));

DataListItem rp1 = ((DataListItem)(tb1.NamingContainer));
string strProductId = rp1.ItemIndex.ToString();

string strUniqeID = datalist1.DataKeys[rp1.ItemIndex].ToString();

/* Here your logic */

Dtcount = (DataTable)Session["myDatatable"];

Dtcount.DefaultView[int.Parse(strProductId)]["Quantity"] = decimal.Parse(strId);

Dtcount.DefaultView[int.Parse(strProductId)]["Price"] = decPrice;

Session["myDatatable"] = Dtcount;

/* Here u can bind the datalist */


/* Delete the particular row in datatable */
/* I write the coding in datalist1_DeleteCommand to delete a particular row */




DataRow dr = ((DataTable)Session["NewDatatable"]).Rows[e.Item.ItemIndex];
dr.Delete();
((DataTable)Session["NewDatatable"]).AcceptChanges();

/* Here u can bind the datalist */

Sunday, May 23, 2010

Keyboard Shortcuts in SQL Server


• Starting the SQL Server management studio Go to Start > Run and type the following command SQLWB
• To see the Object explorer press F8.
• To parse a Query you have written, press Ctrl + F5
• To Execute a Query, press F5 ( before executing you should always parse the query)
• Use F4 for properties window.
• You have options to show the output in Grid (default) or in Text format, Ctrl + D and Ctrl + T respectively) and I you want to output the results to a file use the combination Ctrl + Shift + F
• Ctrl + Shift + Q will display the Query Designer window.
• Display the execution plan using Ctrl + L, It is an advanced topic that we will cover later.
• Use Ctrl + K followed by Ctrl + C to comment out selected lines of code. Use Ctrl + K followed by Ctrl + U to uncomment the selection.
• Use Ctrl + Shift + U to make the selection Uppercase and use Ctrl + Shift + L to change it back to lower case
• Another common requirement is word wrapping for long queries, you may use the combination Ctrl + E followed by Ctrl + W
• Shift + Alt + Enter can be used to toggle between a full screen mode and a normal one.

Saturday, April 10, 2010

How to Access Master page Function into Content Page


In Masterpage  you can write the Function


public void showall()
{
 /* .....Your code goes here */
}

In content page
  in the Design window source add this code

<%@ MasterType VirtualPath="Masterpage Name" %>


after that in codebehind file you can access like this

master.showall();

now you can access the masterpage function in  contentpage..