Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, im trying to load record from my db to a combobox.. sometimes loads all record but sometimes it shows an error.. i dont know if the combobox event is the problem or my method.. pls help newbie..

error message:

InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index


What I have tried:

//my method
void loadSubject(ComboBox cbo)
        {
            try
            {
                SqlConnection con = new SqlConnection(cs.getConnectionString());
                con.Open();
                DataTable dt = new DataTable();
                string query = "SELECT subjectcode,description FROM tblsubject WHERE description LIKE '%' + @1 + '%' ORDER BY description ASC";
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.Parameters.AddWithValue("@1",cbosubject.Text);
                SqlDataReader dr = cmd.ExecuteReader();
                dt.Load(dr);
                cbo.DisplayMember = "description";
                cbo.ValueMember = "subjectcode";
                cbo.DataSource = dt;
            }
            catch (Exception)
            {
                publicFunctions.mBox(3, "error occured");
            }
        }


//my combobox event
private void cbosubject_TextChanged(object sender, EventArgs e)
        {
            loadSubject(cbosubject);
        }
Posted
Updated 31-Jan-18 19:19pm
Comments
Karthik_Mahalingam 29-Jan-18 22:46pm    
are you using listview?
akosisugar 30-Jan-18 2:13am    
no listview..
akosisugar 30-Jan-18 2:15am    
im typing and searching inside a combobox then loads all inside of the same combobox
Karthik_Mahalingam 30-Jan-18 2:16am    
change the value @1 to @val
akosisugar 30-Jan-18 6:56am    
same problem. my project stops when combobox is clicked..

1 solution

I suspect the issue lies in the text changed event. You are changing the Combobox's itemsource in that event which will then cause a change in the text, triggering the event or causing some other error.
If you need the functionality the way you have set it up, I recommend you use a text box for the typing / triggering of text changed and a separate list box that holds the list form the loadsubject call. Note that you'll probably find a performance hit by querying the database on every change in text...
 
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