Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my error : Object reference not set to an instance of an object.

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();

            }
                              
                
                string query = "SELECT DESCRIPTIO FROM stock ORDER by DESCRIPTIO ASC";
                da = new MySqlDataAdapter(query, conn);

                DataSet ds = new DataSet();

                da.Fill(ds, "stock");

                comboBox_Supp1.DataSource = ds.DefaultViewManager;
                comboBox_Supp1.DisplayMember = "stock.descriptio";
                MySqlCommand cmd = new MySqlCommand("SELECT ITEM_CODE FROM stock WHERE DESCRIPTIO = '" + comboBox_Supp1.Text + "'", conn);

                var varTemp = cmd.ExecuteScalar().ToString();
                if (varTemp != null)
                {
                     textBox1.Text = varTemp.ToString();
                }
                                  

            conn.Close();
      
        }


===EDIT: Please use code tags===
CodingK
Posted
Updated 24-Nov-14 14:28pm
v2
Comments
[no name] 24-Nov-14 20:31pm    
Please use code tags when posting code, as this helps keep readable alignment of your code.

I suggest you also read up on ToString and when to use it. Search MSDN for ToString and cmd.ExecuteScalar()
syed shanu 24-Nov-14 21:14pm    
Which line did you get the error.use break point and check for each line of code and find where exactly you get the error.
PIEBALDconsult 24-Nov-14 21:38pm    
That is a total mish-mash of concepts; you really need to study the classes you are trying to use before you hurt yourself.

cmd.ExecuteScalar() returns a null Object if there are no rows returned: "The first column of the first row in the result set, or a null reference ". So you need to check the if this returns null before using toString().
Also ExecuteScalar only gives you the first column, sure you don't need ExecuteReader?
 
Share this answer
 
0) Do not use string concatenation to form an SQL statement; use a parameterized statement.
1) You are overusing ToString; learn to cast.
2) I suspect that ExecuteScalar is returnnig null, and then ToString is causing the Exception
"or a null reference (Nothing in Visual Basic) if the result set is empty"

var varTemp = cmd.ExecuteScalar().ToString();
 
Share this answer
 
apart from the answers provided by others, I think you are doing extra work to get ITEM_CODE, you can simply change the sql statement as
C#
string query = "SELECT DESCRIPTIO, ITEM_CODE FROM stock ORDER by DESCRIPTIO ASC";

then set both DisplayMember and ValueMember
C#
comboBox_Supp1.DisplayMember = "stock.descriptio";
comboBox_Supp1.ValueMember  = "stock.item_code";

then you can set get the combobox selected item value(ITEM_CODE) or value(ITEM_CODE) of any item by index
C#
textBox1.Text = comboBox_Supp1.Items[0].Value.ToString();

or
C#
textBox1.Text = comboBox_Supp1.SelectedValue.ToString();
//for above you need to select item 
 
Share this answer
 
C#
private void comboBox_Supp1_SelectedIndexChanged(object sender, EventArgs e)
       {

           if (Classes.OpenConn.conn.State == ConnectionState.Closed)
           {
               Classes.OpenConn.conn.Open();
           }

           Com.CommandText = "SELECT ITEM_CODE FROM stock WHERE DESCRIPTIO = '" + comboBox_Supp1.Text + "'";
           reader = Com.ExecuteReader();
           while (reader.Read())
           {
               textBox1.Text = reader["ITEM_CODE"].ToString();
           }
           reader.Close();
           Com.Dispose();
       }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900