locked
Error - The Type 'System.Data.SqlClient.SqlTransaction' has no Constructors defined RRS feed

  • Question

  • User-1499457942 posted

    Hi

      On the below line i get above error

    using (SqlTransaction tran = new SqlTransaction())

    using (SqlTransaction tran = new SqlTransaction())
                        {
                            try
                            {
    
                                    String sql = "UPDATE [Cust] SET [Tmp]=@ST where [No_]=@Customer";
                                    using (cmd = new SqlCommand(sql, con, tran))
                                    {
                                        cmd.CommandType = CommandType.Text;
                                        cmd.Parameters.AddWithValue("@Customer", Request.QueryString["cu"].ToString());
                                        cmd.Parameters.AddWithValue("@ST", txtN.Text.Trim());
                                        con.Open();
                                        cmd.ExecuteNonQuery();
                                        cmd.Dispose();
                                        con.Close();
                                    }
                                    tran.Commit();
                                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
                                }
                            }
                            catch (Exception ex)
                            {
                                tran.Rollback();
                                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"" + ex.Message.ToString() + "\");", true);
                            }
                        }
                    }
    

    Thanks

    Thursday, June 28, 2018 6:37 AM

Answers

All replies

  • User409696431 posted

    There is no "new" for SqlTransaction.  An SqlTransaction object is created by calling BeginTransaction.   See the sample code at: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction(v=vs.110).aspx?f=255&MSPPError=-2147217396&cs-save-lang=1&cs-lang=csharp#code-snippet-1

    using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
    
            SqlCommand command = connection.CreateCommand();
            SqlTransaction transaction;
    
            // Start a local transaction.
            transaction = connection.BeginTransaction("SampleTransaction");
    
            // Must assign both transaction object and connection
            // to Command object for a pending local transaction
            command.Connection = connection;
            command.Transaction = transaction;
    
    ......


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 6:50 AM
  • User-1171043462 posted

    using (SqlTransaction tran = new SqlTransaction())

    Change to

     using (SqlTransaction sqlTransaction = con.BeginTransaction())
                

    Thursday, June 28, 2018 8:24 AM