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 8823235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:18:08+00:00 2026-06-14T06:18:08+00:00

I am new to SQL, In my project I am trying to fill the

  • 0

I am new to SQL, In my project I am trying to fill the DataSet from the DataBase table and then inserting new rows in DataSet. After doing this, again I am filling the Database table with the DataSet data. Before filling I am clearing or deleting the Database table data. Well I achieved this by using the below code:

Database Table --> myTable
DataSet --> ds

OnButtonClick

   string strQuery = "delete from [dbo].[myTable]"
   SqlConnection conn = new SqlConnection(strConn);
   SqlCommand cmd = new SqlCommand(strQuery, conn);
   SqlCommand command;
   SqlDataAdapter da = new SqlDataAdapter(cmd);
   using (cmd)
   {
      using (conn)
      {
         conn.Open();
         cmd.ExecuteNonQuery();                    //deleting database table data
         foreach (DataRow dr in ds.Tables[0].Rows) //Inserting new data into the Database table
         {
             command = new SqlCommand(InsertQuery, conn);
             command.ExecuteNonQuery();
         }
         conn.Close();
      }
   }

The above code is working fine but when there is any exception in command sqlCommand then it is deleting the database table completely and not filling the DataSet. Any suggestions to modify this code? please help.

UPDATE:

            string strQuery = "delete from [dbo].[myTable]";
            using (SqlCommand cmd = new SqlCommand(strQuery, conn))
            {
                using (SqlCommand cmdReset = new SqlCommand("DBCC CHECKIDENT('myTable', RESEED, 0)", conn))
                {
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    conn.Open();
                    SqlTransaction sqlTransaction = conn.BeginTransaction();
                    cmd.Transaction = sqlTransaction;
                    try
                    {
                        cmd.ExecuteNonQuery();                   //deleting database table data
                        cmdReset.ExecuteNonQuery();               //Resetting Identity of first column
                        foreach (DataRow dr in ds.Tables[0].Rows) //Inserting new data into the Database table
                        {
                            command = new SqlCommand(query.createDoctorRow(InsertQuery, conn);
                            command.ExecuteNonQuery();
                        }
                        sqlTransaction.Commit();
                        conn.Close();
                    }
                    catch (Exception e)
                    {
                        sqlTransaction.Rollback();
         /*ERROR*/      throw;  
                    }
                }
            }

ERROR –> “ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.”

  • 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-14T06:18:09+00:00Added an answer on June 14, 2026 at 6:18 am

    Use a transaction to run the sql

    string strQuery = "delete from [dbo].[myTable]";
    
    using (SqlConnection conn = new SqlConnection(strConn))
    {
      using (cmd = new SqlCommand(strQuery, conn))
      {
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         conn.Open();
         SqlTransaction sqlTransaction = conn.BeginTransaction();
         cmd.Transaction = sqlTransaction;
         try
         {
             cmd.ExecuteNonQuery();                    //deleting database table data
             foreach (DataRow dr in ds.Tables[0].Rows) //Inserting new data into the Database table
             {
                 command = new SqlCommand(InsertQuery, conn);
                 command.ExecuteNonQuery();
             }
             sqlTransaction.Commit();
             conn.Close();
         }
         catch(Exception e)
         {
             sqlTransaction.Rollback();
             throw;
         }
      }
    

    }

    Transactions form an all or nothing scenario. Either everything in the transaction works and is committed to the database, or the whole lot is cancelled as if nothing happened.

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

Sidebar

Related Questions

I'm coming from a MS SQL Server background. Working on a new project using
I am trying to connect a database with a java project. After reading some
Okay, this is a new one. I'm trying to debug my project, which I've
I have a C# project where I am trying to autofill a SQL table
We are using Linq-to-SQL with SQL Server as an ORM on our new project.
Question: The new SQL Server 2008 database returns me values formatted English (date/float). Is
I'm relatively new SQL queries, and I'm trying to join two tables in order
I need to create an entirely new Sql Server 2008 database and want to
I'm new to SQL. I have a simple problem with getting the results from
I'm new to SQL. I have created a new database in SQL Server Management

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.