Sqladapter Insert

D

DaveL

I have 2 rows in a datatable

When i Adapter.Update(mytable)
i get Update requires valid update command error
When In Fact I am Inserting

SqlAdapter ad = new SqlDataAdapter()
ad.InsertCommand=new SqlCommand();
string sInsert="Insert into mytable (field1,field2) values(@field1,@field2)
..
..ad.InsertCommand.Parameters.Add("@Field1", SqlDbType.Int, 0, "Field1");

ad.InsertCommand.Parameters.Add("@FileId2", SqlDbType.Int, 0, "Field2");

ad.InsertCommand.Connection=oConn;

ad.Update(MyTable) // crashes with requires valid update command



// if i do this

for (int rows = 0;rows<mytalble.Rows.Count;Rows++)

{

ad.InsertCommand.Parameters["@Field1"].Value=MyTable.Rows[x]["Field1"])

ad.InsertCommand.Parameters["@Field2"].Value=MyTable.Rows[x]["Field2"])

ad.InsertCommand.ExecuteNonQuery() // Works just Fine inserts 2
Rows



}



any idea why the update requires a Update command

Thanks Dave
 
P

Peter Bromberg [C# MVP]

I don't see in your sample where you are assigning the "sInsert" string to
the CommandText property of the InsertCommand. You also need to set the
CommandType to CommandType.Text.
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top