0

I encountered a weird error:

mycon.Open();

adap = new SqlDataAdapter("SELECT * FROM Employee; Select * from Shift; select * from Has_Shift", mycon);
adap.TableMappings.Add("T1", "Employee");
adap.TableMappings.Add("T2", "Shift");
adap.TableMappings.Add("T3", "Has_Shift");

adap.Fill(ds);

DataRow newRow = ds.Tables["T1"].NewRow();
newRow["Name"] = textBox1.Text;
ds.Tables["T1"].Rows.Add(newRow);
adap.Update(ds);
mycon.Close();

There is an error stating:

Object reference not set to an instance of an object at the DataRow newRow line.

I don't know why is it happening.

13
  • Where are you initializing ds? Commented May 15, 2013 at 6:49
  • @JeremyTodd, adap.Fill(ds); Commented May 15, 2013 at 6:50
  • That doesn't initialize the dataset, it just populates it with data (although I'd expect it to throw an exception if ds were null). Commented May 15, 2013 at 6:52
  • I tried, but the information in the dataset ds is too complicated, I also think the problem lies in the adap.Fill(ds), maybe I am missing some parameters? I initialize Dataset ds = new Dataset() top of this piece of code. Commented May 15, 2013 at 6:55
  • The question is, where is the error pointing to, which line? Commented May 15, 2013 at 6:59

1 Answer 1

0

If you use a batch SQL statement to retrieve multiple tables and fill a DataSet, the first table is named using the table name specified to the Fill method. Subsequent tables are named using the name specified to the Fill method plus a number starting from one and incrementing by one. For example, if you were to run the following code: MSDN

So the code will be

adap = new SqlDataAdapter("SELECT * FROM Employee; Select * from Shift; select * from Has_Shift", mycon);

// second table name will be Employee +1
adap.TableMappings.Add("Employee1", "Shift");
// second table name will be Employee +2
adap.TableMappings.Add("Employee2", "Has_Shift");

// give Table name as below 
adap.Fill(ds, "Employee");

DataRow newRow = ds.Tables["Employee"].NewRow();
newRow["Name"] = textBox1.Text;
ds.Tables["Employee"].Rows.Add(newRow);
adap.Update(ds);
mycon.Close();

Since here we gave "Employee" as table name in Fill method, first table will be "Employee" and second will be "Employee1" and third will be "Employee2"

Since you haven't given any table name your table names will be "Table", "Table1", "Table2" ... You can map them to correct name

adap.TableMappings.Add("Table", "Employee");
adap.TableMappings.Add("Table1", "Shift");
adap.TableMappings.Add("Table2", "Has_Shift");

and rest of the code will be

adap.Fill(ds);

DataRow newRow = ds.Tables["Employee"].NewRow();
newRow["Name"] = textBox1.Text;
ds.Tables["Employee"].Rows.Add(newRow);
adap.Update(ds);
mycon.Close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.