populate checkboxes with database.

Posted by amby on Stack Overflow See other posts from Stack Overflow or by amby
Published on 2010-04-15T15:31:46Z Indexed on 2010/04/15 15:33 UTC
Read the original article Hit count: 273

Filed under:
|

Hi, I have to poulate checkboxes with data coming from database but no checkbox is showing on my page. please give me correct way to do that. in C# file page_load method i m doing this:

    public partial class dbTest1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string Server = "al2222";
        string Username = "hshshshsh";
        string Password = "sjjssjs";
        string Database = "database1";

        string ConnectionString = "Data Source=" + Server + ";";
        ConnectionString += "User ID=" + Username + ";";
        ConnectionString += "Password=" + Password + ";";
        ConnectionString += "Initial Catalog=" + Database;
        string query = "Select * from Customer_Order where orderNumber = 17";

        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (!IsPostBack)
                    {
                        Interests.DataSource = dr;
                        Interests.DataTextField = "OptionName";
                        Interests.DataValueField = "OptionName";
                        Interests.DataBind();
                    }
                }
                conn.Close();
                conn.Dispose();
            }
        }
        }
    }

and in .aspx using this:

<asp:CheckBoxList ID="Interests" runat="server"></asp:CheckBoxList>

please tell me correct way to do that.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#