Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am using a dropdown list and checkboxlist control. when i select any item from dropdownlist, checkboxlist items should be shown selected respectively from the database.

Please help me.

Thanks.
Posted
Updated 28-Nov-17 20:30pm
Comments
Chandan Aryan 29-Mar-12 4:00am    
Pls let us know that u want full code for it or u r having some problem in it!
singh420 29-Mar-12 4:07am    
Actually i want that at the time of item selection in dropdown list, the check boxlist item should be shown checked if they are checked earlier.

Fetching the Value from the DataBase and Showing the Selected Value in CheckBoxList


C#
protected void btnGetRecor_Click(object sender, EventArgs e)
 {
 try
 {
 getCountry();
 getSelectedCountry();
 }
 catch (Exception ex)
 {
 lblerror.Text = ex.Message;
 }
 }
 public void getSelectedCountry()
 {
 IDataReader dr;
 string[] s = new string[50];
 SqlConnection con = new SqlConnection();
 con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:Fech the value from Database and show in selectedtCheckedListBox\\multicheckboxvalue\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True";
 SqlCommand com = new SqlCommand();
 com.Connection = con;
 com.CommandText = "FetchCountry";
 com.CommandType = CommandType.StoredProcedure;
 con.Open();
 dr= com.ExecuteReader();
 while (dr.Read())
 {
 s = dr["countryname"].ToString().Split(',');
 
}
 int length = s.Length;
 for (int i = 0; i <= s.Length - 1; i++)
 {
 string cntry = s[i];
 for (int j = 0; j <= CheckBoxList2.Items.Count - 1; j++)
 {
 if (CheckBoxList2.Items[j].Text == s[i])
 {
 CheckBoxList2.Items[j].Selected = true;
 break;
 }
 }
 }
 }
 
Share this answer
 
v3
hey there,

this is my code from filling a combobox, i suspect it should be the same for a checkboxlist :)

C#
/////////// Load combo boxes with car index/////////////////////


         string connectionString1 = "Provider= ...etc"

            string sqlstring1 = "select distinct (car_index) from sinter_car  where status = 'Active' order by car_index";

           OleDbConnection connection1 = new OleDbConnection(connectionString1);

            OleDbCommand command1 = new OleDbCommand(sqlstring1,Connection1);

            try
            {

             connection1.Open();

                OleDbDataReader reader1 = command1.ExecuteReader();

                while

                    (reader1.Read())
                {
                    comboBox1.Items.Add(reader1["car_index"]);
                                   
                }

                reader1.Close();            }

            catch (Exception)
            {
                MessageBox.Show("An exception error has occured!", "Exception Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

           connection1.Close();
 
Share this answer
 
public void BindChecklist()
{
CheckBoxList1.DataSource = DB.ReviewCategories.Where(p => p.Deleted == true);
CheckBoxList1.DataTextField = "CategoryName";
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.DataBind();
}

foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
PlaceUserReviewCategory catobj = new PlaceUserReviewCategory();
catobj.UserID = ((User)Session["USER"]).ID;
catobj.PlaceID = PlaceID;
catobj.ReviewCategoryID = Convert.ToInt32(li.Value);

}

}



Kishor makwana
insightsoftech
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900