combbobox selectedvaluechanged event not working

2 posts, 0 answers
  1. srm
    srm avatar
    2 posts
    Member since:
    Jul 2011

    Posted 18 Jul 2011 Link to this post

    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";
  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 21 Jul 2011 Link to this post

    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!

  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top