Object reference not set to an instance of an object

I am trying to bind a reportViewer in c# 2010 windows application from Access 2007 query, the query is "select * from mytable where id=@id". I added parameters using Add and AddWithValue but both throws an error that "Object reference not set to an instance of an object." How can fix the problem?

this.qReportTableAdapter.Adapter.SelectCommand.Parameters.AddWithValue("@id",nInt.ToString());

or this.qReportTableAdapter.Adapter.SelectCommand.Parameters.Add("@id",OleDbType.Integer,1).Value=nInt;

this.qReportTableAdapter.Fill (this.dsReport.qReport);
this.reportViewer1.RefreshReport ();  

Please help.

Thanks ayha
LVL 7
ayha1999Asked:
Who is Participating?
 
Kalpesh ChhatralaSoftware ConsultantCommented:
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
Hi Ahya,

Check any one Object has null value that's why you get error.

Kalpesh
0
 
ayha1999Author Commented:
the value is already provided as "nInt"
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Kalpesh ChhatralaSoftware ConsultantCommented:
may be qReportTableAdapter object is null.
0
 
ayha1999Author Commented:
where is the right place to add the parameter?  If I remove 'where' condition from the query and ádd parameter'from the code, it works without error.

thanks
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
"select * from mytable where id= " + nInt;

Don't use Parameter direct set value into your Query. like above
0
 
ayha1999Author Commented:
my query is "select * from mytable where id=@id"

then in the code

this.qReportTableAdapter.Adapter.SelectCommand.Parameters.AddWithValue("@id",nInt.ToString());
0
 
ayha1999Author Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.