|
-
Aug 4th, 2005, 11:13 PM
#1
Thread Starter
Hyperactive Member
Adding new Row problem
Can anybody see why this add new row isn't working?
Code:
public void dg_Insert(Object s, DataGridCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("wineList.xml"));
DataTable dt = new DataTable("wines");
DataRow dr;
dt = ds.Tables["wines"];
// add new wine
dr = dt.NewRow();
dr["name"] = ((TextBox)e.Item.FindControl("txtNewName")).Text;
dr["coo"] = ((TextBox)e.Item.FindControl("txtNewCoo")).Text;
dr["BT"] = ((TextBox)e.Item.FindControl("txtNewBT")).Text;
dr["ml"] = ((TextBox)e.Item.FindControl("txtNewMl")).Text;
dr["type"] = ((TextBox)e.Item.FindControl("txtNewType")).Text;
dr["id"] = GetNextNodeID() + 1;
dt.Rows.Add(dr);
DataGridWine.DataSource = ds.Tables["wines"];
DataGridWine.DataBind();
ds.WriteXml(Server.MapPath("wineList.xml"));
}
}
I get an error at this line: dt.Rows.Add(dr);
Code:
System.NullReferenceException: Object reference not set to an instance of an object.
-
Aug 8th, 2005, 11:12 PM
#2
Fanatic Member
Re: Adding new Row problem
you have to set intance of dr. this is the datarow right? i dont kno c# but it has somthing to do with something like this: DataRow dr = new datarow();
If a post has helped you then Please Rate it!
-
Aug 9th, 2005, 09:31 AM
#3
Thread Starter
Hyperactive Member
Re: Adding new Row problem
Hi,
I have tried using Data dr = new DataRow();
This still doesn't work.
-
Aug 9th, 2005, 09:57 PM
#4
Fanatic Member
Re: Adding new Row problem
this one works for me i dont kno how to translate to c#
Code:
Dim ds As New DataSet
Dim dt As New DataTable
Dim dr As DataRow
ds.ReadXml(Server.MapPath("xml\wineList.xml"))
dt = New DataTable("wines")
dt = ds.Tables("wines")
' add new wine
dr = dt.NewRow()
dr("name") = TextBox1.Text
dr("coo") = TextBox2.Text
dr("BT") = TextBox3.Text
dr("ml") = TextBox4.Text
dr("type") = TextBox5.Text
dr("id") = 1
dt.Rows.Add(dr)
DataGrid1.DataSource = ds.Tables("wines")
DataGrid1.DataBind()
If a post has helped you then Please Rate it!
-
Aug 10th, 2005, 12:06 AM
#5
Re: Adding new Row problem
Couldn't you get rid of the dt variable all together?
Something like this:
Code:
public void dg_Insert(Object s, DataGridCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("wineList.xml"));
DataRow dr;
// add new wine
dr = ds.Tables("wines").NewRow();
dr["name"] = ((TextBox)e.Item.FindControl("txtNewName")).Text;
dr["coo"] = ((TextBox)e.Item.FindControl("txtNewCoo")).Text;
dr["BT"] = ((TextBox)e.Item.FindControl("txtNewBT")).Text;
dr["ml"] = ((TextBox)e.Item.FindControl("txtNewMl")).Text;
dr["type"] = ((TextBox)e.Item.FindControl("txtNewType")).Text;
dr["id"] = GetNextNodeID() + 1;
ds.Tables("wines").Rows.Add(dr);
DataGridWine.DataSource = ds.Tables["wines"];
DataGridWine.DataBind();
ds.WriteXml(Server.MapPath("wineList.xml"));
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|