fill asp:CheckBoxList from database

1.2k views Asked by At

i have problems filling my checkboxlist from a sql in c#. The list is empty when it have been loaded.

I know it would probably be easier with a connection to web.config, but i have decieded not to have web.config in this project. It would be helpfull if anyone could see what i have done wrong.

This is my code:

string connetionString = "Data Source=[DATA-SOURCE];Initial Catalog=[CATALOG];User ID=[USER-ID];Password=[PASSWORD]";
    SqlConnection cnn = new SqlConnection(connetionString);

    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandText = "SELECT ModuleID, ModuleName, InternalName, Active FROM dbo.Zodiac_System_Modules WHERE Active = 1 ORDER BY ModuleName ASC";
        cmd.Connection = cnn;
        cnn.Open();
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            while (sdr.Read())
            {
                ListItem item = new ListItem();
                item.Text = sdr["ModuleName"].ToString();
                item.Value = sdr["InternalName"].ToString();
                chkModules.Items.Add(item);
            }
        }
        cnn.Close();
    }
1

There are 1 answers

4
Codeek On BEST ANSWER

You Are Missing CommandType This:

cmd.CommandType=CommandType.Text;