Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > Software > need help regarding DataTables in asp.net 2.0

Reply
Thread Tools

need help regarding DataTables in asp.net 2.0

 
 
riyaz riyaz is offline
Junior Member
Join Date: Sep 2006
Posts: 1
 
      09-16-2006
hi,
i have created a datatable and placed it in session.how can i access individual values of every column.i want to assign these values into labels and textboxes.
thanx
 
Reply With Quote
 
 
 
 
BabuA BabuA is offline
Junior Member
Join Date: Sep 2006
Location: BANGALORE,INDIA
Posts: 6
 
      09-18-2006
Hi riyaz,

following code will help you to read the DataTable from session and assign these values to Labels and the textboxes based on the Control Name .

Code:

//UI:

//Two Labels Namely Label1 and label2
//Two textboxes Namely Textbox1 and textbox2
// one button name: button1 and caption: assign


// Global Varaiable declared in Page:

public static Control Cctrl = null;

// In Page Load I have build the Datatable and places some values and assign it in session;

// Data Table
DataTable dt = new DataTable();

DataColumn dc1 = new DataColumn("ControlName", Type.GetType("System.String"));
DataColumn dc2 = new DataColumn("ControlValue", Type.GetType("System.String"));

dt.Columns.Add(dc1);
dt.Columns.Add(dc2);

// Values Od Data Table
DataRow Drow = dt.NewRow();
Drow["ControlName"] = "Label1";
Drow["ControlValue"] = "Name";
dt.Rows.Add(Drow);

Drow = dt.NewRow();
Drow["ControlName"] = "Label2";
Drow["ControlValue"] = "Age";
dt.Rows.Add(Drow);

Drow = dt.NewRow();
Drow["ControlName"] = "TextBox1";
Drow["ControlValue"] = "Babu A";
dt.Rows.Add(Drow);

Drow = dt.NewRow();
Drow["ControlName"] = "TextBox2";
Drow["ControlValue"] = "22";
dt.Rows.Add(Drow);

// Placing DT to Session.

Session["CData"] = dt;


// In Button click i written the follwing function:

// reading the datatable from session and assign to dt varaiables
DataTable dst = (DataTable)Session["CData"];


for (int i = 0; i < dst.Rows.Count; i++)
{
Cctrl = null;
GetCtrl(dst.Rows[i]["ControlName"].ToString(),this);

if (Cctrl != null)
{
switch (Cctrl.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((System.Web.UI.WebControls.TextBox)Cctrl).Text = dst.Rows[i]["ControlValue"].ToString();
break;
case "System.Web.UI.WebControls.Label":
((System.Web.UI.WebControls.Label)Cctrl).Text = dst.Rows[i]["ControlValue"].ToString();
break;
default:
break;
}
}
}



//To Get ths Control Based on the Control Name

private void GetCtrl(string Cname,Control Parent)
{
foreach (Control Ctrl in Parent.Controls)
{
if (Ctrl.ID != null)
{
if (Ctrl.ID.ToString() == Cname)
{
Cctrl = Ctrl;
}
}

if (Cctrl != null)
{
return;
}
else
{
GetCtrl(Cname, Ctrl);
}
}
}


we can normalize these method also.

I hope this will help you

Babu A
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About datatables Ravindra ASP .Net 1 12-30-2005 12:33 PM
datagrid columns from different datatables help ? strongmace ASP .Net 2 07-06-2004 04:03 PM
Datatable based on two other datatables (inner join) Gene Ariani ASP .Net 0 08-21-2003 10:46 PM
Re: Combining DataTables Kevin Spencer ASP .Net 0 07-21-2003 05:07 PM
set up a datagrid of multiples datatables in 1 dataset Richard Loupatty ASP .Net 2 07-18-2003 08:54 PM



Advertisments