Using a DataSet to insert data in a table througth a sproc
-
I am trying to insert data in a table, using a sproc. I want to do this because the current code is doing this through a foreach loop. I want to get rid of it. Here is the code I made (this is test code, not the real one):
using System;
using System.Data;
using System.Data.SqlClient;namespace DataSetTest
{
class Program
{
static void Main(string[] args)
{
DataSet ds = GetDataSet();
SqlConnection conn = new SqlConnection("Server=DEVRDABTS01\\MSDEVPARA;database=sandbox; Integrated Security=SSPI");
conn.Open();SqlCommand comm = new SqlCommand("usp\_DatasetTest\_Insert", conn); comm.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(); da.InsertCommand = comm; da.Update(ds); //**<---- Error here** } public static DataSet GetDataSet() { DataSet returnDS = new DataSet(); DataTable dt = returnDS.Tables.Add("DatasetTest"); dt.Columns.Add("dateValue", typeof(DateTime)); dt.Columns.Add("clientId", typeof(string)); dt.Columns.Add("stringValue", typeof(string)); dt.Columns.Add("dontInsertIfNull", typeof(string)); DataRow dr; for (int i = 0; i < 10; ++i) { dr = dt.NewRow(); dr\[0\] = DateTime.Now; dr\[1\] = i.ToString(); dr\[2\] = "some string"; dr\[3\] = (i % 2) == 0 ? "" : "this is not null"; //dr.SetAdded(); dt.Rows.Add(dr); } return returnDS; } }
}
I get the following exception at the line marked above: System.InvalidOperationException was unhandled Message="Update unable to find TableMapping['Table'] or DataTable 'Table'." Source="System.Data" StackTrace: at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at DataSetTest.Program.Main(String[] args) in c:\projets\Visual Studio 2005\Projects\DataSetTest\DataSetTest\Program.cs:line 21 at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at Sys
-
I am trying to insert data in a table, using a sproc. I want to do this because the current code is doing this through a foreach loop. I want to get rid of it. Here is the code I made (this is test code, not the real one):
using System;
using System.Data;
using System.Data.SqlClient;namespace DataSetTest
{
class Program
{
static void Main(string[] args)
{
DataSet ds = GetDataSet();
SqlConnection conn = new SqlConnection("Server=DEVRDABTS01\\MSDEVPARA;database=sandbox; Integrated Security=SSPI");
conn.Open();SqlCommand comm = new SqlCommand("usp\_DatasetTest\_Insert", conn); comm.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(); da.InsertCommand = comm; da.Update(ds); //**<---- Error here** } public static DataSet GetDataSet() { DataSet returnDS = new DataSet(); DataTable dt = returnDS.Tables.Add("DatasetTest"); dt.Columns.Add("dateValue", typeof(DateTime)); dt.Columns.Add("clientId", typeof(string)); dt.Columns.Add("stringValue", typeof(string)); dt.Columns.Add("dontInsertIfNull", typeof(string)); DataRow dr; for (int i = 0; i < 10; ++i) { dr = dt.NewRow(); dr\[0\] = DateTime.Now; dr\[1\] = i.ToString(); dr\[2\] = "some string"; dr\[3\] = (i % 2) == 0 ? "" : "this is not null"; //dr.SetAdded(); dt.Rows.Add(dr); } return returnDS; } }
}
I get the following exception at the line marked above: System.InvalidOperationException was unhandled Message="Update unable to find TableMapping['Table'] or DataTable 'Table'." Source="System.Data" StackTrace: at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at DataSetTest.Program.Main(String[] args) in c:\projets\Visual Studio 2005\Projects\DataSetTest\DataSetTest\Program.cs:line 21 at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at Sys