By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,098 Members | 717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,098 IT Pros & Developers. It's quick & easy.

C# Dataadapter.Update not working for Insert Command - No Errors!

P: 1
I'm writing a little app to get data from a CSV file generated everyday by a vendor and insert it into a Table in our SQL Server database. I can get the data fine, and I can easily merge it with another dataset I pull from the existing table (which is exactly the same except for an Identity Column as PK).

When I call SqlDataAdapter.Update, no error is generated, but it makes no changes as well. I have one row present in the Database and can use DataAdapter.Update to delete it just fine. It just won't insert.

Please Help! Code below:

Expand|Select|Wrap|Line Numbers
  1. using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyPath\\;Extended Properties='text;HDR=Yes;FMT=Delimited'"))
  2.             {
  3.                 string sql = "SELECT * from MyCSV.csv";
  4.                 OleDbDataAdapter csvRdr = new OleDbDataAdapter(sql, cn);
  5.                 DataSet ds = new DataSet();
  6.  
  7.                 cn.Open();
  8.  
  9.                 try
  10.                 {
  11.  
  12.                     csvRdr.Fill(ds, "Table");
  13.                     cn.Close();
  14.                     using (SqlConnection cnn = new SqlConnection("Data Source=DB;Initial Catalog=XX;User ID=usr;Password=pwd"))
  15.                     {
  16.                         string sql_2 = "Select * from DBTable";
  17.                         SqlDataAdapter da = new SqlDataAdapter(sql_2, cnn);
  18.                         SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
  19.                         DataSet ds2 = new DataSet();
  20.                         da.TableMappings.Add("DBTable", "Table");
  21.                         Console.WriteLine(sql_2);
  22.                         cnn.Open();
  23.                         da.Fill(ds2, "Table");
  24.                         ds2.Merge(ds.Tables["Table"]);
  25.                         ds2.AcceptChanges();
  26. //This works              ds2.Tables["Table"].Rows[0].Delete();
  27. //Looks Fine              Console.Write(ds2.GetXml()); 
  28.                         da.Update(ds2, "Table");
  29.                         cnn.Close();
  30.                     }
  31.               }
  32.        )
Thanks.

-Mike
Aug 21 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.