Thread: Updating a datarow in dataset

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    215

    Updating a datarow in dataset

    Im trying to update a data row in a data set but my program keeps on crashing at this one line, i was wandering if someone could help me out with the problem

    heres the code

    Code:
    void updateDB()
    		{	
    			for(int i=0; i < export_log.Tables[0].Rows.Count; i++)
    			{
    				
    				export_log.Tables[0].Rows[i]["export_id"] = 9999;
    				dc.updateDataRow(export_log.Tables[0].Rows[i]);
    			}
    
    		}
    
    
    	public void updateDataRow(DataRow dr)
    		{
    
    
    
    			try
    			{
    
    				string table = "ExportLog";
    				String query = "select * from " + table;
    				DataSet ds = new DataSet();
    				SqlDataAdapter da = new SqlDataAdapter();
    
    				SqlCmd.CommandText = query;
    				SqlCmd.Connection = ObjConn;
    			
    				SqlCommandBuilder builder = new SqlCommandBuilder(da);
    				da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    
    					
    ds.Tables[table].Rows.Add(dr); // code crashes here because of null exception
    				da.Fill(ds);
    		
    				da.Update(ds,table);
    			}
    			catch(SqlException ex)
    			{
    				Console.WriteLine("Database error");
    			}
    
    		}
    Additional information: Object reference not set to an instance of an object.

    Thats the error i get when I use the debugger.
    Last edited by osal; 08-08-2005 at 02:43 PM.

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    215
    this is the error i get when i check the debugger:

    Additional information: Object reference not set to an instance of an object.

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    My guess ( since there´s little information given ) :

    Code:
    DataSet ds = new DataSet();
    ds.Tables[table].Rows.Add(dr);
    Are you sure there are Tables in the object ds ?

    My second guess is:

    Code:
    public void updateDataRow(DataRow dr)
    ds.Tables[table].Rows.Add(dr);
    Are you sure that the Datarow object that is being passed as a parameter ( in updateDateRow signature ) is actually referencing to something ?

    A quick way to check these things is to do something like

    Code:
    public void updateDataRow(DataRow dr) {
      if(dr.Equals(null)) {
    	Console.WriteLine("dr -> null");
      }
      //rest of code goes here.
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what do you do to avoid loading large dataset again and again
    By patiobarbecue in forum C++ Programming
    Replies: 12
    Last Post: 04-19-2009, 01:13 AM
  2. [C#] howto compare combobox-item with DataSet
    By Jelte in forum C# Programming
    Replies: 3
    Last Post: 08-26-2008, 06:25 PM
  3. DataSet, DataTable, and DataGrids
    By TheMajorRager in forum C# Programming
    Replies: 0
    Last Post: 08-23-2005, 11:54 AM
  4. datatables from a dataset
    By student2005 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 12:47 PM
  5. updating database
    By datainjector in forum C# Programming
    Replies: 2
    Last Post: 07-11-2003, 01:01 AM