Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:20:46+00:00 2026-06-12T13:20:46+00:00

I am trying to do following Insert Command .But it is not working neither

  • 0

I am trying to do following Insert Command .But it is not working neither showing any exception. Can anybody tell me where I am doing wrong thing?

using System;
using System.Data;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Security.Principal;

public partial class StoredProcedures
{
   [Microsoft.SqlServer.Server.SqlProcedure]
   public static void USP_tempSP()
   {   
       DataTable objDataTable = new DataTable();
       using (SqlConnection objConn = new SqlConnection("context connection=true"))
       {
           objConn.Open();
           SqlCommand objCmd = new SqlCommand();
           objCmd.CommandType = CommandType.Text;

           objCmd.Connection = objConn;
           objCmd.CommandText = "select * from temp"; 
           /*table Structure
            *CREATE TABLE [dbo].[temp]([empname] [varchar](20)  NULL,[createdon] [datetime] NULL,[emp_id] [varchar](4) NULL
           ) 
           */
           SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCmd.CommandText, objConn);
           objDataAdapter.Fill(objDataTable);

           SqlCommand objCmd1 = new SqlCommand();
           objCmd1.CommandType = CommandType.Text;
           objCmd1.UpdatedRowSource = UpdateRowSource.None;
           objCmd.CommandText = "Insert into tempCopy " +
                               " values(@empname,@createdon)";
           /*table Structure
            *CREATE TABLE [dbo].[tempCopy]([empname] [varchar](20)  NULL,[createdon] [datetime] NULL
            ) 
           */
           objCmd.Parameters.Add("@empname", SqlDbType.VarChar, 20, objDataTable.Columns["empname"].ColumnName);
           objCmd.Parameters.Add("@createdon", SqlDbType.DateTime, 8, objDataTable.Columns["createdon"].ColumnName);

           SqlDataAdapter adpt = new SqlDataAdapter();
           adpt.InsertCommand = objCmd;
           adpt.UpdateBatchSize = objDataTable.Rows.Count;
           try
           {
               int recordsInserted = adpt.Update(objDataTable);
           }
           catch(Exception ex)
           {
               //
           }

           objConn.Close();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:20:47+00:00Added an answer on June 12, 2026 at 1:20 pm

    I really think this is because you are using the update method on an table which is tracking it’s own changes. The problem is that nothing has changed. Set AcceptChangesDuringFill = false so that the rows are marked as added to the table.

           objConn.Open();
           /* This doesn't really do anything.
           SqlCommand objCmd = new SqlCommand();
           objCmd.CommandType = CommandType.Text;
    
           objCmd.Connection = objConn;
           objCmd.CommandText = "select * from temp"; */
           /*table Structure
            *CREATE TABLE [dbo].[temp]([empname] [varchar](20)  NULL,[createdon] [datetime] NULL,[emp_id] [varchar](4) NULL
           ) 
           */
           SqlDataAdapter objDataAdapter = new SqlDataAdapter("select * from temp", objConn);
           //Make sure we don't mark the new rows as unchanged
           objDataAdapter.AcceptChangesDuringFill = false;
           objDataAdapter.Fill(objDataTable);
    
           SqlCommand objCmd1 = new SqlCommand();
           objCmd1.CommandType = CommandType.Text;
           objCmd1.UpdatedRowSource = UpdateRowSource.None;
           //This is supposed to be objCmd1 !!
           objCmd1.CommandText = "Insert into tempCopy " +
                               " values(@empname,@createdon)";
           /*table Structure
            *CREATE TABLE [dbo].[tempCopy]([empname] [varchar](20)  NULL,[createdon] [datetime] NULL
            ) 
           */
           //This is supposed to be objCmd1 !!
           objCmd1.Parameters.Add("@empname", SqlDbType.VarChar, 20, objDataTable.Columns["empname"].ColumnName);
           objCmd1.Parameters.Add("@createdon", SqlDbType.DateTime, 8, objDataTable.Columns["createdon"].ColumnName);
    
           SqlDataAdapter adpt = new SqlDataAdapter();
           //This is supposed to be objCmd1 !!
           adpt.InsertCommand = objCmd1;
           adpt.UpdateBatchSize = objDataTable.Rows.Count;
           try
           {
               int recordsInserted = adpt.Update(objDataTable);
           }
           catch(Exception ex)
           {
               //
           }
    
           objConn.Close();
    

    I think this should work, you need to be more careful with your variable naming. objCmd is fairly meaningless. If the command has been cmdSelectData and cmdInsertData it would have been much clearer. Also pretty much everything is an object so obj just clutters up the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert a string in MySQL table. But the following is
I am trying to download flurry exception logs using the following command. curl --cookie
How to insert data into table in sqlite iPhone? I am trying following, but
While I was trying the following SQL command , I got sql error. INSERT
I am trying to execute following query: Insert into pligg_links_temp select * from pligg_links
I am trying to run the following query to insert a number of nodes
While trying to insert data to my SQl db i get the following error
Am trying to insert XML into XML Column.. getting following error: . Msg 6819,
I got the following issue while trying to insert multiple entries into a MySQL
I'm trying to insert some text midway through some HTML content using the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.