I am new to SQL (started today itself), how to add data of dataset to the database table when the table in database has the first column as automatic generating numbers i.e., (IDENTITY (1,1))
I have DataSet something like this
name age location
x 30 yyy
y 20 ppp
The table in Database is something like this
Id(auto) name age location
-------- no data --------------
First of all I am filling the Dataset from the database table.
Since the first column is autogenerated I cant even add new values to DataSet.
If I say
dsMainDoctors.Tables[0].Rows.Add( /* first column is autogenerated so I am leaving it*/
secondColumn,
thirdColumn,
fourthColumn,
FifthColumn);
Error –> Input string was not in a correct format.Couldn’t store in Doctor_ID Column. Expected type is Int32.
The code I am trying is: (When I given the first column value manually in the DataSet)
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(strQuery, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
conn.Open();
da.Update(ds.Tables[0]); //ERROR :- Update requires a valid InsertCommand when passed DataRow collection with new rows.
conn.Close();
conn.Dispose();
cmd.Dispose();
strQuery --> "delete from [dbo].[" + table_name + "]";
I could have searched this before asking but I dont know what keywords I should use
Please answer in comments if it is very easy question.
Okay, the following code will build that INSERT command properly for you. Also, please note, that if you’re concerned about disposing objects, use the
usingstatement instead because it will callDisposewhen exiting the statement.EDIT
If you’re just wanting to
deletethe data in the database to rebuild it then that’s quite different than what you’re original question was implying. However, below is how simple that would be.