Click here to Skip to main content
14,975,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SqlConnection cn;
SqlTransaction st;
st=cn.BeginTransaction;



V/s

SqlConnection cn;
SqlTransaction st=new SqlTransaction();


Is it similar?

What I have tried:

I had searched on google but not understand and confused.

private void btbsend_Click(object sender, EventArgs e)
        {
                MySqlCommand genvoucher,df;
                MySqlTransaction genvouchrt=new MySqlTransaction();
                MySqlTransaction dft = new MySqlTransaction();
                try
                {
                    cn.Open();

                    string fvno;
                    dateTimePicker1.Value = DateTime.Now;
                    dateTimePicker2.Value = DateTime.Now;
                    var tdat = dateTimePicker2.Value;
                    var ttime = dateTimePicker1.Value;
                    fvno = textBox1.Text;
                    genvoucher = new MySqlCommand("insert into cutting(vno,bno,nsize,machine,vdate,operator,stime,shift,status) values(@vno,@bno,@size,@machine,@date,@opt,@start,@shift,@status)", cn, genvouchrt);
                    genvoucher.Parameters.AddWithValue("@vno", fvno);
                    genvoucher.Parameters.AddWithValue("@size", textBox3.Text);
                    genvoucher.Parameters.AddWithValue("@bno", comboBox3.SelectedItem.ToString());
                    genvoucher.Parameters.AddWithValue("@machine", comboBox1.SelectedItem.ToString());
                    genvoucher.Parameters.AddWithValue("@date", tdat);
                    genvoucher.Parameters.AddWithValue("@opt", comboBox4.SelectedItem.ToString());
                    genvoucher.Parameters.AddWithValue("@shift", shift);
                    genvoucher.Parameters.AddWithValue("@start", ttime);
                    genvoucher.Parameters.AddWithValue("@status", "Open");
                    genvoucher.ExecuteNonQuery();
                    genvouchrt.Commit();

                    df = new MySqlCommand("insert into vouchers values(@vno,@dept,@shift,@task,@stats,@remarkn,@remarkv)", cn, dft);
                    df.Parameters.AddWithValue("@vno", textBox1.Text);
                    df.Parameters.AddWithValue("@dept", dept);
                    df.Parameters.AddWithValue("@shift", shift);
                    df.Parameters.AddWithValue("@task", "Cutting");
                    df.Parameters.AddWithValue("@stats", "Open");
                    df.Parameters.AddWithValue("@remarkn", "");
                    df.Parameters.AddWithValue("@remarkv", "");
                    df.ExecuteNonQuery();
                    dft.Commit();

                    MessageBox.Show("Voucher Opened Successfully!");
                    cn.Close();

                    this.Close();
                }
            catch (Exception eli)
            {
                MessageBox.Show("Can't Generate Voucher" + Environment.NewLine + eli);
                try
                {
                    genvouchrt.Rollback();
                    dft.Rollback();
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Rollback Exception"+ex.Message);
                }
            }
        }
Posted
Updated 16-Sep-17 22:20pm
v2
Comments
Graeme_Grant 17-Sep-17 0:32am
   
What is your question? Be specific: this is what I am currently doing; this is the code that I am using; this is the error that I have encountered; etc...
[no name] 17-Sep-17 0:35am
   
my question is if i use second part is it wrong?
Graeme_Grant 17-Sep-17 0:40am
   
This information NEEDS TO BE in your question. That is why it is called a "Question".

Also, hit "reply" to the comment that you want to "reply to", not "Have a question or Comment". This way the author will know that you are communicating with them. You have already asked 15+ questions, so you should know this already.
[no name] 17-Sep-17 0:39am
   
i have updated my question and added code.
Graeme_Grant 17-Sep-17 0:41am
   
see answer below.
Graeme_Grant 17-Sep-17 0:50am
   
As per the documentation, you open a connection, begin a transaction, do some work, commit the transaction or roll it back - that is it in a nutshell. Read the documentation for more information.

It is unclear what you want. GOOGLE SEARCH is your friend. So, here are a couple of documentation links, found with GOOGLE SEARCH, that also include code examples:
* SqlTransaction Class (System.Data.SqlClient)[^]
* Transaction and Bulk Copy Operations | Microsoft Docs[^]

Here is a tutorial found right here on CodeProject:
* A Beginner's Tutorial for Understanding Transactions and TransactionScope in ADO.NET[^]

And a GOOGLE SEARCH for more tutorials: SqlTransaction beginner examples C#[^]
   
v2
Comments
Wendelius 17-Sep-17 6:24am
   
Good links!
You asked if the following are similar:
C#
SqlConnection cn;
SqlTransaction st;
st=cn.BeginTransaction;

V/s
C#
SqlConnection cn;
SqlTransaction st=new SqlTransaction();

However, the question does not quite make sense since the latter one does not compile. In other words, you cannot instantiate a SqlTransaction class directly. This is because the constructor is defined as internal.

If you try to compile the latter code you'll get an error
Severity Code   Description
-------- ------ ----------------------------------------------------------------------
Error    CS1729 'SqlTransaction' does not contain a constructor that takes 0 arguments	

Probably the reason for preventing direct instantiation of SqlTransaction class is that during BeginTransaction call, several checks are carried out and assignments made, for example:
- default isolation level is set to read committed if not specified
- connection is validated
- the connection is restored if needed and broken
- and so on...
   
Comments
Graeme_Grant 17-Sep-17 4:29am
   
The code was added after I posted my solution. Well done for answering the coding problem. 5'd :)
[no name] 17-Sep-17 5:09am
   
really worthy answer.
Wendelius 17-Sep-17 6:22am
   
Thank you.
Wendelius 17-Sep-17 6:22am
   
Thanks

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