Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
its a windows application., i used checkedlist to show in the form dynamically., written code for mouse click even., here if user click more than one checkbox message should show like "already 1 items choosed" so the use will uncheck the previous item and then they will choose another 1., but in my case user can select more than 1 items since message box shown.,

C#
void ch9_MouseClick(object sender, EventArgs e)
       {
           try
           {


               gn.cnopen();
               string get_max_items = "select max_items from menu_group where group_name='icecream'";
               cmd = new SqlCommand(get_max_items, gn.cn());
               int max_items = Convert.ToInt32(cmd.ExecuteScalar());

               int count = ch9.CheckedItems.Count;

               if (count >= max_items)
               {
                   MessageBox.Show("Already Choosed--" + max_items.ToString() + "--Items");
                   return;
               }
           }
           catch (SqlException err)
           {
               MessageBox.Show(err.Message);
           }
       }
Posted
Comments
Erik Rude 16-Nov-12 11:26am    
I'm not sure your question and your code are well aligned.
Looks to me like you're opening a connection to a database asking how many menuitems thare are in the Icecream group and then you're counting how many checkboxes you've got checked in ch9. if more than the number of menus then you show the message.

There are many issues in your code.

First when you open a connection you need to close it as well (or at least that is good practice)

Are you sure that select max_items from menu_group where group_name='icecream' returns the value you think it should do? It may, but if you really want a count you could do something like
select count(*) from menu_group where group_name='icecream'

Have you tried debugging your function so you can inspect the values (and hence better understand the behaviour)?

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