Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
In my project, there is a combo box "status" with the three values active, in active and working. As per the requirement the selected value should represent as a integer, so I wrote code so that the index of the selected value will store. Now the problem is, when I tried to select the value from the combo box while updating the status, it should be shown as value in the listview. That if I select 0 it should show active, 1 as in-active and 2 as working. The code so far I used to get the values is below, please help me getting the values to that integers. The code is:

C#
private void btnUpdateSupport_Click(object sender, EventArgs e)
        {
            SqlConnection con = Helper.getconnection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            string SupportName = txtSupportName.Text;
            string SupportDesignation = txtSupportDesignation.Text;
            //string SupportStatus = txtSupportStatus.Text;
            string SupportStatus = cbSupportStatus.SelectedItem.ToString();
            //string SupportStatus = SqlCommand();
            int i = 0;
            string s = "Active";
            // string result = int.TryParse(s, out i);

            if (cbSupportStatus.SelectedItem != null)
            {
                int x = int.Parse(cbSupportStatus.SelectedItem.ToString());
            }
            else
            { //Value is null }

                cmd.CommandText = "Update Status1 set Designation='" + SupportDesignation + "', Status='" + SupportStatus + "' where Name= '" + SupportName + "' ";
                MessageBox.Show(cmd.CommandText);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

            }
        }
Posted
Comments
Prasad Khandekar 27-Aug-13 8:19am    
Hello Karthik,

You can use selectedIndex property to retrieve the index of the selected item and use the same to select the item as well. You can use SelectedText to retrieve the textual representation of the selected item. See this (http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx) MSDN documentation.

Regards,
karthik reddy mereddy 27-Aug-13 8:27am    
Thank you, I am able to retrieve the index and the store the same in the database, here the problem I have is I want to assign the text( value of the combo box before insering,, i.e active or in active or working) to that selected integer. If I select 0 from combo box, then it should show as Active in the list view, 1 as In active and 2 as working.
Prasad Khandekar 27-Aug-13 8:38am    
Use SelectedText property to get the text value of selected item.

Regards,
Sergey Alexandrovich Kryukov 27-Aug-13 11:01am    
No, no. This is possible, but... You are having the same controversy. For a comprehensive approach, please see my answer.
—SA
BillWoodruff 27-Aug-13 19:45pm    
Prasad, I suggest you post this as a solution; it deserves, I feel, to be upvoted. You are correct: by using the SelectedIndex and SelectedText Properties of the ComboBox, Karthik can handle any case.

Karthik, I suggest you study Prasad's reply very carefully. Keep in mind that that every Item in a ComboBox is an "object" which has both a "text representation," and an index. When you set the ComboBox's Items to strings: you "get" the "text representation" for "free."

1 solution

You are doing a wrong thing with SelectedItem, wrong in principle. You don't have to store strings in a combo box or a list box. You can store any objects. That way, you will avoid unsupportable, inefficient and unreliable parsing of the item's text to integer or anything else. Instead, you would simply parse the item as integer (but you should be sure that you really store integers).

In this approach, it often pays off to store instances of some struct or class with all the information you need to the functionality. The only problem is: what will be shown in the UI if you store some objects of your choice. The answer is simple: whatever ToString() (without parameters) returns. So, the solution is also simple: for your element type, override System.Object.ToString() the way it would show what you need. This is a simple and robust technique.

—SA
 
Share this answer
 
v2
Comments
CPallini 27-Aug-13 11:23am    
5.
Sergey Alexandrovich Kryukov 27-Aug-13 11:42am    
Thank you, Carlo.
—SA

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