need help regarding DataTables in asp.net 2.0

Discussion in 'Software' started by riyaz, Sep 16, 2006.

  1. riyaz

    riyaz

    Joined:
    Sep 10, 2006
    Messages:
    1
    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
     
    riyaz, Sep 16, 2006
    #1
    1. Advertising

  2. riyaz

    BabuA

    Joined:
    Sep 12, 2006
    Messages:
    6
    Location:
    BANGALORE,INDIA
    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["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["ControlValue"].ToString();
    break;
    case "System.Web.UI.WebControls.Label":
    ((System.Web.UI.WebControls.Label)Cctrl).Text = dst.Rows["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
     
    BabuA, Sep 18, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2FuanU=?=

    information about asp ,oracle,asp.net certification?

    =?Utf-8?B?c2FuanU=?=, May 18, 2005, in forum: Microsoft Certification
    Replies:
    1
    Views:
    711
    Bob Christian
    May 18, 2005
  2. Zabron Muyambo

    Easy Moving from ASP to ASP.NET???

    Zabron Muyambo, Oct 26, 2004, in forum: MCSD
    Replies:
    7
    Views:
    550
    Zabron Muyambo
    Oct 27, 2004
  3. thehobbit
    Replies:
    0
    Views:
    3,946
    thehobbit
    Nov 22, 2006
  4. imman

    session from asp to asp.net

    imman, Feb 20, 2008, in forum: General Computer Support
    Replies:
    0
    Views:
    957
    imman
    Feb 20, 2008
  5. london1919
    Replies:
    1
    Views:
    3,098
    Lawrence Garvin \(MVP\)
    Dec 8, 2008
Loading...

Share This Page