Results 1 to 5 of 5

Thread: Adding new Row problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    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.

  2. #2
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    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!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: Adding new Row problem

    Hi,

    I have tried using Data dr = new DataRow();

    This still doesn't work.

  4. #4
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    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!

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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
  •  



Click Here to Expand Forum to Full Width