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. Web Development
  3. ASP.NET
  4. Transaction problem in asp.net

Transaction problem in asp.net

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasesql-serversysadmin
3 Posts 3 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.
  • P Offline
    P Offline
    pranavcool
    wrote on last edited by
    #1

    Hi, I am am using commit and rollback transaction in asp.net. due to some reasons transaction is not working.In the following code i am deleting the record and then inserting new record.If the transaction fails the transaction will be rollback.but it is not rolling back the transaction. The following is the code:-

    public static void insertdata()
    {
    string strfilename = "~\\Documents\\" + ViewState["MissionCat"];
    string strexcelConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strfilename) + "; Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";
    string strsqlConnectionString = ConfigurationManager.ConnectionStrings["conn"].ToString();

        using (OleDbConnection connection = new OleDbConnection(strexcelConnectionString))
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand("Select \[Mission\] ,\[Civilian\] ,\[Military\] ,\[Police\],\[Unknown\] ,\[Non-UN\] ,\[Total\] FROM \[Sheet1$\]", connection);
                //truncate the table before inserting new data.
                SqlConnection cnntrunc = new SqlConnection(strsqlConnectionString);
                //open the connection.
                cnntrunc.Open();
                //begin the transaction.
                SqlTransaction myTrans = cnntrunc.BeginTransaction();  //New a transaction                        
                SqlCommand truntble = new SqlCommand();
                truntble.Transaction = myTrans;
                truntble.Connection = cnntrunc;
                try
                {
                    //Create DbDataReader to Data Worksheet.
                    using (DbDataReader dr = command.ExecuteReader())
                    {
                        truntble.CommandText = "DELETE FROM MissionCatReport";
                        truntble.ExecuteNonQuery();
                        //commit the transaction.
                        myTrans.Commit();
                        //Bulk Copy to SQL Server.
                        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(strsqlConnectionString))
                        {
                            bulkCopy.DestinationTableName = "MissionCatReport";
                            bulkCopy.WriteToServer(dr);
                        }
                    }
                }
                catch (OleDbException ex)
                {
                    myTrans.Rollback();
                    throw ex;
                }
                catch (Inv
    
    P C 2 Replies Last reply
    0
    • P pranavcool

      Hi, I am am using commit and rollback transaction in asp.net. due to some reasons transaction is not working.In the following code i am deleting the record and then inserting new record.If the transaction fails the transaction will be rollback.but it is not rolling back the transaction. The following is the code:-

      public static void insertdata()
      {
      string strfilename = "~\\Documents\\" + ViewState["MissionCat"];
      string strexcelConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strfilename) + "; Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";
      string strsqlConnectionString = ConfigurationManager.ConnectionStrings["conn"].ToString();

          using (OleDbConnection connection = new OleDbConnection(strexcelConnectionString))
              {
                  connection.Open();
                  OleDbCommand command = new OleDbCommand("Select \[Mission\] ,\[Civilian\] ,\[Military\] ,\[Police\],\[Unknown\] ,\[Non-UN\] ,\[Total\] FROM \[Sheet1$\]", connection);
                  //truncate the table before inserting new data.
                  SqlConnection cnntrunc = new SqlConnection(strsqlConnectionString);
                  //open the connection.
                  cnntrunc.Open();
                  //begin the transaction.
                  SqlTransaction myTrans = cnntrunc.BeginTransaction();  //New a transaction                        
                  SqlCommand truntble = new SqlCommand();
                  truntble.Transaction = myTrans;
                  truntble.Connection = cnntrunc;
                  try
                  {
                      //Create DbDataReader to Data Worksheet.
                      using (DbDataReader dr = command.ExecuteReader())
                      {
                          truntble.CommandText = "DELETE FROM MissionCatReport";
                          truntble.ExecuteNonQuery();
                          //commit the transaction.
                          myTrans.Commit();
                          //Bulk Copy to SQL Server.
                          using (SqlBulkCopy bulkCopy = new SqlBulkCopy(strsqlConnectionString))
                          {
                              bulkCopy.DestinationTableName = "MissionCatReport";
                              bulkCopy.WriteToServer(dr);
                          }
                      }
                  }
                  catch (OleDbException ex)
                  {
                      myTrans.Rollback();
                      throw ex;
                  }
                  catch (Inv
      
      P Offline
      P Offline
      padmanabhan N
      wrote on last edited by
      #2

      is it throwing any error message... how do you say that it is not working.....

      Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

      1 Reply Last reply
      0
      • P pranavcool

        Hi, I am am using commit and rollback transaction in asp.net. due to some reasons transaction is not working.In the following code i am deleting the record and then inserting new record.If the transaction fails the transaction will be rollback.but it is not rolling back the transaction. The following is the code:-

        public static void insertdata()
        {
        string strfilename = "~\\Documents\\" + ViewState["MissionCat"];
        string strexcelConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strfilename) + "; Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";
        string strsqlConnectionString = ConfigurationManager.ConnectionStrings["conn"].ToString();

            using (OleDbConnection connection = new OleDbConnection(strexcelConnectionString))
                {
                    connection.Open();
                    OleDbCommand command = new OleDbCommand("Select \[Mission\] ,\[Civilian\] ,\[Military\] ,\[Police\],\[Unknown\] ,\[Non-UN\] ,\[Total\] FROM \[Sheet1$\]", connection);
                    //truncate the table before inserting new data.
                    SqlConnection cnntrunc = new SqlConnection(strsqlConnectionString);
                    //open the connection.
                    cnntrunc.Open();
                    //begin the transaction.
                    SqlTransaction myTrans = cnntrunc.BeginTransaction();  //New a transaction                        
                    SqlCommand truntble = new SqlCommand();
                    truntble.Transaction = myTrans;
                    truntble.Connection = cnntrunc;
                    try
                    {
                        //Create DbDataReader to Data Worksheet.
                        using (DbDataReader dr = command.ExecuteReader())
                        {
                            truntble.CommandText = "DELETE FROM MissionCatReport";
                            truntble.ExecuteNonQuery();
                            //commit the transaction.
                            myTrans.Commit();
                            //Bulk Copy to SQL Server.
                            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(strsqlConnectionString))
                            {
                                bulkCopy.DestinationTableName = "MissionCatReport";
                                bulkCopy.WriteToServer(dr);
                            }
                        }
                    }
                    catch (OleDbException ex)
                    {
                        myTrans.Rollback();
                        throw ex;
                    }
                    catch (Inv
        
        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        I don't see any code to commit the transaction ?

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        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