locked
Populate the checkboxes, radiobuttonlist and textbox data from SQL Server table in ASPX C#. RRS feed

  • Question

  • User-34860367 posted

    Hello all,

    I got SQL Server table that stored the checkboxes, radiobuttonlist, textbox values.

    For example, table named tblData with the column and data following:

    ID, CHKBOX1, CHKBOX2, CHKBOX3, RADIOBUTTONLIST1, RADIOBUTTONLIST2, TEXTBOX

    1, false, false, true, true, 0, sample text 1

    2,  false, true, false, true, 1, sample text 2

    3, true, false, true, false, 2, sample text 3

    I used the Session["ID"] and need to populate the data when page loading based on  column ID (Primary Key) .

    Any help is much appreciated. Thanks in advance. 

    Friday, November 18, 2016 3:59 PM

Answers

  • User-1838255255 posted

    Hi avt2k7,

    According to your description, I think you want query data by id, I make a sample use datatable select method, it can get data where Id=1, then show them in controls. Here is the sample code:

    protected void Page_Load(object sender, EventArgs e)
            {
                //assumed data saved in database
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                                new DataColumn("Check1", typeof(bool)),
                                new DataColumn("Check2",typeof(bool)) });
                dt.Rows.Add(1, true, false);
                dt.Rows.Add(2, true, true);
                dt.Rows.Add(3, false, false);
                //add it in session
                Session.Add("sessiondt", dt);
    
                //get data from session
                DataTable dts = (DataTable)Session["sessiondt"];
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                DataTable dts = (DataTable)Session["sessiondt"];
    
                DataRow[] foundRows = dts.Select("Id=1");
                TextBox1.Text = foundRows[0][0].ToString();
                CheckBox1.Checked = Convert.ToBoolean(foundRows[0][1]);
                CheckBox2.Checked = Convert.ToBoolean(foundRows[0][2]);
            }

    Result:

    If this reply can solve your problem, please provide more details about how to populate data by Id.

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 21, 2016 8:56 AM