Click here to Skip to main content
15,887,268 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,

in my web application,i am trying to bind data to data table and then data table to gridview. Code for the same is as follows.
C#
SqlCommand cmdd1 = new SqlCommand("insert into leave_rec values('" + name + "','" + date1.Text + "','" + type1 + "','" + type + "')", conn1);
cmdd1.ExecuteNonQuery();
SqlCommand cmdd2 = new SqlCommand("select date,leave,ltype from leave_rec where date='" + date1.Text + "'and name='" + name + "'", conn1);
SqlDataAdapter da = new SqlDataAdapter(cmdd2);
Session["dt"]=null;
DataTable dtable = (DataTable)Session["dt"];
string dat = date1.Text;
string hf = type1;
string lt = type;
dtable.Rows.Add(new object[] { dat,hf,lt });
dtable.AcceptChanges();
GridView1.DataSource = dtable;
GridView1.DataBind();
Session["dt"] = dtable;

In above code,i am getting error on the highlighted line that Object reference not set to instance of an object. I kindly request you all to help me in this regard.
Posted
Updated 28-Feb-13 0:10am
v2
Comments
Orcun Iyigun 28-Feb-13 6:15am    
Why would you need to do this:
<pre lang="c#">Session["dt"]=null;
DataTable dtable = (DataTable)Session["dt"];</pre>
instead of creating a new instance?

Please try
C#
DataColumn col1 = new DataColumn("date");
DataColumn col2 = new DataColumn("leave");
DataColumn col3 = new DataColumn("lttype");
col1.DataType = System.Type.GetType("System.String");
col2.DataType = System.Type.GetType("System.String");
col3.DataType = System.Type.GetType("System.String");
dtable.Columns.Add(col1);
dtable.Columns.Add(col2);
dtable.Columns.Add(col3);
DataRow dr = dtable.NewRow();
dr["date"] = dat;
dr["leave"] = hf;
dr["lttype"] = lt;
dtable.Rows.Add(dr);

Good luck,
OI
 
Share this answer
 
v2
Comments
Thanks7872 28-Feb-13 6:23am    
thanks for your quick reply.i have modified code the way you suggested but it now gives the same error at first line DATAROW DR=DTABLE.NEWROW(); .........?can you help me out?
Orcun Iyigun 28-Feb-13 6:39am    
DataTable dtable = new DataTable(); ??? as I asked earlier which you ignored. I am again asking have you created a new instance?
Thanks7872 28-Feb-13 6:43am    
due to some technical fault i saw that comment now.my bad.well,i havent created instance and i modified code like this
SqlDataAdapter da = new SqlDataAdapter(cmdd2);

DataTable dtable = new DataTable();
DataRow dd = dtable.NewRow();
dd["date"] = date1.Text;
dd["leave"] = type1;
dd["ltype"] = type;
dtable.Rows.Add(dd);
that means,i have removed session["dt"]=null and directly defined new datatable....
can you suggest me creating instance?ERROR: colomn 'date' does not belong to table
Orcun Iyigun 28-Feb-13 6:53am    
check my solution again
Thanks7872 28-Feb-13 7:16am    
it works like a charm.!!!!! thanks for your great support....thanks alot.
Try below...

You need to declare datarow object before using it..
C#
DataRow dr = null;


DataTable dtable = new DataTable();
dr = dtable.NewRow();


hope it helps you..
 
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