Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Using a DataSet to insert data in a table througth a sproc

Using a DataSet to insert data in a table througth a sproc

Scheduled Pinned Locked Moved Database
csharpdatabasevisual-studiosysadminsecurity
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Le centriste
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • L Le centriste

      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

      G Offline
      G Offline
      goldoche
      wrote on last edited by
      #2

      Could this be your problem? http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c43c.aspx#q783q[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups