0

I'm doing this exactly (see Using SqlDataAdapter to insert a row) but it doesn't give me the .ADD method

Here is my code:

var sqlQuery = "select * from CT_DETIMP where 0 = 1";
SqlDataAdapter tableAdapter = new SqlDataAdapter(sqlQuery, myConnection);
DataSet dataSet = new DataSet();
tableAdapter.Fill(dataSet);

var impRow = dataSet.Tables["CT_DETIMP"].NewRow();
impRow["TRANSCD"] = tranCode;
impRow["MISC"] = misc;
impRow["ADDENDA"] = addenda;

dataSet.Tables["CT_DETIMP"].Add(impRow);  // THIS IS WHERE THE ERROR IS 

new SqlCommandBuilder(tableAdapter);
tableAdapter.Update(dataSet);

I get the error DataTable does not contain a reference for ADD. I'd be happy to do this another way but I can't use table.Adapter.Insert because I need my data to go into specific fields.


So I changed dataSet.Tables["CT_DETIMP"].Add(impRow); to dataSet.Tables["CT_DETIMP"].Rows.Add(impRow); and now I am getting a null error on this line: var impRow = dataSet.Tables["CT_DETIMP"].NewRow();


This code worked:

' SqlConnection myConnection = new SqlConnection(connectionString); myConnection.Open();

        var sqlQuery = "select * from CT_detimp where 0 = 1";
        SqlDataAdapter tableAdapter = new SqlDataAdapter(sqlQuery, myConnection);

        SqlCommandBuilder cb = new SqlCommandBuilder(tableAdapter);

        DataSet dataSet = new DataSet("CT_DETIMP");
        tableAdapter.Fill(dataSet,"CT_DETIMP");

        DataTable detailTable = dataSet.Tables["CT_DETIMP"];
        DataRow impRow = detailTable.NewRow();'
4
  • stackoverflow.com/questions/13138615/… Commented Jun 24, 2016 at 18:29
  • Pretty sure it is .Rows.Add Commented Jun 24, 2016 at 18:29
  • also DataTable is the underlying of DataSet for example dataSet.Tables[0].Rows.Add Commented Jun 24, 2016 at 18:30
  • MethodMan -- that was the trick --> rows.add Commented Jun 24, 2016 at 18:47

2 Answers 2

1

Change the line with the error to this:

dataSet.Tables["CT_DETIMP"].Rows.Add(impRow);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh no. Now I'm getting a null error on 'var impRow = dataSet.Tables["CT_DETIMP"].NewRow();' Any clue?
1

You need to add to the DataTable.Rows collection

dataSet.Tables["CT_DETIMP"].Rows.Add(impRow);

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.