This is a migrated thread and some comments may be shown as answers.

combbobox selectedvaluechanged event not working

1 Answer 93 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
srm
Top achievements
Rank 1
srm asked on 18 Jul 2011, 11:56 AM
I am building an application where users r coming from database in combobox on combobox1_Enter event.
I am also populating from another table rights for the particular user as per
selectedvaluechanged event of the same comboobx in datagridview.The problem comes when I reselect
same user error is"object not set to an instance of an object" this does not happen if I click buton for
populating usersrights in gridview when i select value in combobox.Can anybody help me with this?
here is the code:
private void UsersCombox_Enter(object sender, EventArgs e)
        {
            ds = null;
            UsersCombox.SelectedIndex = -1;
            SqlConnection mycon = new SqlConnection("Data source=DOTNET1;integrated security=true;initial catalog=examcollectiondata");
            SqlDataAdapter adap = new SqlDataAdapter("select * from createuserTbl", mycon);
            ds = new DataSet();
            adap.Fill(ds, "createusertbl");
            UsersCombox.DataSource = ds;

            UsersCombox.ValueMember = "CreateUserTbl.UserName";

            ds.AcceptChanges();


            ds.GetChanges();
            UsersCombox.Refresh();
        }

private void UsersCombox_SelectedValueChanged(object sender, EventArgs e)
        {
           //error when reselecting value in combobox
            SqlConnection mycon = new SqlConnection("data source=dotnet1;initial catalog=ExamCollectionData;integrated security=true");
//error comes in the line below
            SqlDataAdapter adap = new SqlDataAdapter("select DiffForms,selectdata,insertdata,updatedata,deletedata,viewreports from useraccesstbl where users='" + UsersCombox.SelectedValue.ToString() + "'", mycon);

            DataSet ds = new DataSet();
            adap.Fill(ds, "useraccesstbl");

            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "useraccesstbl";

1 Answer, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 21 Jul 2011, 08:25 AM
Hi Srm,

Thank you for writing.

I can suppose that in this line
select DiffForms,selectdata,insertdata,updatedata,deletedata,viewreports from useraccesstbl where users='" + UsersCombox.SelectedValue.ToString()
the SelectedValue is Null.

I would like to suggest to execute the SQL statement only if SelectedValue is not null.
You also can use the SelectedIndexChanged event instead of SelectedValueChanged event and check if the SelectedIndex is bigger than -1.

I hope this helps.

Kind regards,
Peter
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
srm
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or