Click here to Skip to main content
13,055,083 members (64,148 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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;
private void LoadTest()
               string query = ("SELECT tCode,tName FROM tblData ORDER BY tName");
               SqlCommand cmd = new SqlCommand(query, con);
               DataTable dt = new DataTable();
               foreach (DataRow drRow in dt.Rows)
                   string empInfo = drRow["tName"].ToString();
               foreach (string itm in cboTests.Items)
               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 15:26pm
System.Windows.Forms? Tag: "WinForms".
apurba001 7-Apr-13 22:01pm
Actually it is not given here....
Excuse me...
Tag the UI library you use, always.

1 solution

Rate this: bad
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,

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2017
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