Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
I am trying to implement autocomplete combobox from database as in the following code. My objective is to get the value of selected item. Although it is returning the required value but with an error "Object reference not set to an instance of an object". Please help me. Thanks.
 
private void Form_Load(object sender, EventArgs e)
        {
            cboTests.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cboTests.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            LoadTest();
            
        }
 
private void LoadTest()
       {
           try
           {
               con.Open();
               string query = ("SELECT tCode,tName FROM tblData ORDER BY tName");
               SqlCommand cmd = new SqlCommand(query, con);
               
               DataTable dt = new DataTable();
               dt.Load(cmd.ExecuteReader());
 
               foreach (DataRow drRow in dt.Rows)
               {
                   string empInfo = drRow["tName"].ToString();
                   cboTests.Items.Add(empInfo);
               }
 
               foreach (string itm in cboTests.Items)
               {
                   cboTests.AutoCompleteCustomSource.Add(itm);
 
               }
 
               cboTests.DataSource = dt;
               cboTests.DisplayMember = "tName";
               cboTests.ValueMember = "tCode";
              
               
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error:" + ex.Message.ToString());
           }
           finally { con.Close(); }
       }
 
private void cboTests_SelectedIndexChanged(object sender, EventArgs e)
       {
 
           DataRow selectedDataRow = ((DataRowView)cboTests.SelectedItem).Row;          ///Here i am getting the error
           int selectedValue = Convert.ToInt32(selectedDataRow["tCode"]);
 
           MessageBox.Show("Selected Id is: " + selectedValue);
       }
 

 

Posted 7-Apr-13 16:26pm
Comments
Sergey Alexandrovich Kryukov at 7-Apr-13 21:58pm
   
System.Windows.Forms? Tag: "WinForms".
—SA
apurba001 at 7-Apr-13 22:01pm
   
Actually it is not given here....
Sergey Alexandrovich Kryukov at 7-Apr-13 22:42pm
   
Excuse me...
Tag the UI library you use, always.
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Add a check if SelectedItem is not null.
 
This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.
 
Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].
 
The code has different problems. The exception handling is too local. This is how you can catch all non-specific exception on top of UI thread, in the main event-oriented cycle:
Catching an Exception[^].
 
Avoid Convert use int.Parse or int.TryParse. Avoid hard-coding of immediate constants, explicitly define constants.
 
Good luck,
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100