Click here to Skip to main content
Rate this: bad
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;
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 16:26pm
Sergey Alexandrovich Kryukov at 7-Apr-13 21:58pm
System.Windows.Forms? Tag: "WinForms".
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.

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
0 Dnyaneshwar@Pune 719
1 Sergey Alexandrovich Kryukov 340
2 CPallini 325
3 Peter Leow 280
4 CHill60 235
0 Sergey Alexandrovich Kryukov 8,963
1 OriginalGriff 6,766
2 Peter Leow 4,262
3 Zoltán Zörgő 3,769
4 Richard MacCutchan 2,588

Advertise | Privacy | Mobile
Web03 | 2.8.150128.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2015
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