Click here to Skip to main content
16,017,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I just want to know the concept of commit Transaction and Begin Transaction in Asp.net with c# language, i have no idea about the requested concept,

i have a certain code in c# given below by this i have no idea where i declare commit transaction and begin transaction. please draw a code given below.

my code is:

C#
public void dial(string traid, string trakey, string account, string ttref)
{
    try
    {
        string[] result = new string[4];
        SqlConnection sqlconn_cms = new SqlConnection(sqlconn_cmsstr);

        {
            if (sqlconn_cms.State == ConnectionState.Closed)
            {

                sqlconn_cms.Open();
            }
        }
        SqlCommand sqlCmd = new SqlCommand("ccTranscationchecktest", sqlconn_cms);
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("@traid", traid);
        sqlCmd.Parameters.AddWithValue("@trakey", trakey);
        sqlCmd.Parameters.AddWithValue("@account", account);
        sqlCmd.Parameters.AddWithValue("@transref", ttref);
        sqlCmd.ExecuteNonQuery();
        sqlconn_cms.Close();
    }
    catch (Exception ex)
    {
    }
}

if you drop an solution for me its very useful for me. So please give a solution for that.

thanks in advance
Posted
Updated 16-Dec-13 23:42pm
v2

Transactions are handled by the database engine, hence BEGIN TRANSACTION and COMMIT TRANSACTION should be part of the stored procedure.
 
Share this answer
 
Comments
thatraja 17-Dec-13 6:02am    
Agree he should use transactions inside the sp. 5!
CPallini 17-Dec-13 6:14am    
Thank you.
Check the below link which gives fair idea about the transactions

Using Transactions in ADO.NET[^]

Hope this helps you ...
 
Share this answer
 
Because your code only performs a single operation - it executes a stored procedure once and once only - it's difficult to really show you what happens with a Transaction.

If instead we do two table updates that depend on each other it should be clearer.
We have two tables:
Table1:
ID     Int
Data   NVARCHAR(50)

Table2:
ID     INT, Identity (assigned by SQL because we don't care what it is, just that it is unique)
Tab1ID INT, Foreign key to ID in Tab1
Data   NVARCHAR(50)

The data in Table2 has a foreign key: the ID of the record in Table1.
Think of it as an Invoice (Table1) and the individual items the customer bought (multiple Table2 entries).
If there is no record with that ID in table 1, then the Tabel2 data is invalid. So if the first query fails, we don't want to try and write the second table data. That's easy:
C#
private void TransDemo(int ID, string dataForTab1, string dataForTab2)
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        try
            {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Table1 (Id, Data) VALUES (@ID, @DT", con))
                {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@DT", dataForTab1);
                cmd.ExecuteNonQuery();
                }
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Table2 (Tab1Id, Data) VALUES (@ID, @DT", con))
                {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@DT", dataForTab2);
                cmd.ExecuteNonQuery();
                }
            }
        catch (Exception ex)
            {
            MessageBox.Show(ex.Message);
            }
        }
    }
The try...catch block catches the problem, and prevents the second table being written. But what happens if the first insert works, but the second fails? That's also bad - because the first table data has been written, but it isn't "connected" to the second table.
You end up with an invoice for no items - and your company loses money!

This is where a Transaction comes in: You start a transaction before you do any database changes, and if there is a problem you undo the changes (called a "rollback") and throw them all away so your database remains consistent. If it all works, then you approve all the changes (called a "commit") and then and only then is the actual database altered.
C#
private void TransDemo(int ID, string dataForTab1, string dataForTab2)
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        SqlTransaction trans = con.BeginTransaction();
        try
            {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Table1 (Id, Data) VALUES (@ID, @DT", con))
                {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@DT", dataForTab1);
                cmd.ExecuteNonQuery();
                }
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Table2 (Table1Id, Data) VALUES (@ID, @DT", con))
                {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@DT", dataForTab2);
                cmd.ExecuteNonQuery();
                }
            trans.Commit();
            }
        catch (Exception ex)
            {
            trans.Rollback();
            MessageBox.Show(ex.Message);
            }
        }
    }
If you get a problem with either update, the Rollback throws away all the changes, so it doesn't matter where the problem was - it can be in the first or second insert but the database is always consistent and right.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900