Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please,Help

when insert data error show "
object reference not set to an instance of an object
".What can i dow for solve the problem.my code is

try
       {

           SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblItemEntry WHERE Item_Name='" + comboBox1.SelectedItem.ToString() + "'", con);
           DataTable dt = new DataTable();
           sda.Fill(dt);
           foreach (DataRow item in dt.Rows)
           {
               textBox1.Text = item["Item_ID"].ToString();
           }
       }
       catch(Exception ex)
       {
           MessageBox.Show(ex.Message);
       }


What I have tried:

try
            {

                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblItemEntry WHERE Item_Name='" + comboBox1.SelectedItem.ToString() + "'", con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                foreach (DataRow item in dt.Rows)
                {
                    textBox1.Text = item["Item_ID"].ToString();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
Posted
Updated 20-Jan-17 12:14pm
Comments
F-ES Sitecore 20-Jan-17 4:19am    
This is only one of the reasons you shouldn't use "select *". Your code is also open to SQL injection attacks so look into parameterised queries. Also when you get an error always say what line it occurs on so people aren't wasting their time guessing what your problem is.

You should report all the details of the error message (e.g. what is the offending line?).

Quote:
extBox1.Text = item["Item_ID"].ToString();
if the "Item_ID" field doesn't exitst (for instance, if it is mistyped) then such an exception may arise.


Note, you should prefer parametric queries to the string concatenation approach.
 
Share this answer
 
1. If the error in
(SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblItemEntry WHERE Item_Name='" + comboBox1.SelectedItem.ToString() + "'", con);) line then conncetion properties is not intilise properly.

2. If the error occoured in side foreach loop.

textBox1.Text = item["Item_ID"].ToString();

then it may be the ITEM_ID column name is not there the data row or ITEM_ID value may be null.

When the value is null and we try to convert it to string by help of function .ToString() then the Object reference issue.. occur.

Note: User Convert.Tostring(item["Item_ID"]) instead of tem["Item_ID"].ToString(); because Convert.Tostring() can handle null value.
 
Share this answer
 
may be
comboBox1.SelectedItem.ToString()
would be null.
please set a debug point and debug and see where the value is null.
 
Share this answer
 
Comments
Methoun Ahmed 21-Jan-17 1:23am    
I am already use comboBox1.SelectedItem.ToString()
First remove the try/catch structure, and the error message will tell you where it crash, use debugger and set a breakpoint on offending line and inspect variables.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

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

  Print Answers RSS


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