Introduction
In this article, I will explain how to insert data into multiple tables using a MySQL Transaction in ASP.NET.
So, let's proceed with the following procedure:
- ASP.NET web page
- Grid View Data Control , MySQL Database and MySQL Transaction
Now, open the MySQLAdmin Page then select "Create a New Table” > "View" >”Table Structure for Table 'student ' ”.
- CREATE TABLE IF NOT EXISTS `student` (
- `SID` int(100) NOT NULL AUTO_INCREMENT,
- `Name` varchar(100) NOT NULL,
- `Address` varchar(500) NOT NULL,
- `Email` varchar(100) NOT NULL,
- `Mobile` varchar(25) NOT NULL,
- PRIMARY KEY (`SID`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
Table structure for table 'courses'
- CREATE TABLE IF NOT EXISTS `courses` (
- `Course` varchar(100) NOT NULL,
- `Name` varchar(100) NOT NULL,
- `Amount` int(11) NOT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Open your instance of Visual Studio 2012 and create a new ASP.NET Web application. Name the project “MySqlTransactionApplication", as shown in the following figure:
Now design your Student.aspx View design part; use the following code:
Student.aspx
Now, in the code behind file "Student.aspx.cs" use the following code.
Student.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using MySql.Data.MySqlClient;
- namespace MySqlTransactionApplication
- {
- public partial class Student : System.Web.UI.Page
- {
- #region MySqlConnection Connection and Page Lode
- MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
- protected void Page_Load(object sender, EventArgs e)
- {
- try
- {
- if (!Page.IsPostBack)
- {
- btnSubmit.Attributes.Add("onclick", "javascript:return validationCheck()");
- BindStudentGridView(); BindCoursesGridView();
- }
- }
- catch (Exception ex)
- {
- ShowMessage(ex.Message);
- }
- }
- #endregion
- #region show message
-
-
-
-
- void ShowMessage(string msg)
- {
- ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('" + msg + "');</script>");
- }
-
-
-
- void clear()
- {
- txtName.Text = string.Empty; txtAddress.Text = string.Empty; txtMobile.Text = string.Empty; txtEmail.Text = string.Empty;
- ddlCourse.SelectedIndex = 0; txtAmount.Text = string.Empty; txtName.Focus();
- }
- #endregion
- #region bind data to GridViewStudent and GridViewCourses
- private void BindStudentGridView()
- {
- try
- {
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- MySqlCommand cmd = new MySqlCommand("Select * from Student ORDER BY SID DESC", conn);
- MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- adp.Fill(ds);
- GridViewStudent.DataSource = ds;
- GridViewStudent.DataBind();
- }
- catch (MySqlException ex)
- {
- ShowMessage(ex.Message);
- }
- finally
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- }
- }
- }
- private void BindCoursesGridView()
- {
- try
- {
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- MySqlCommand cmd = new MySqlCommand("Select * from courses", conn);
- MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- adp.Fill(ds);
- GridViewCourses.DataSource = ds;
- GridViewCourses.DataBind();
- }
- catch (MySqlException ex)
- {
- ShowMessage(ex.Message);
- }
- finally
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- }
- }
- }
- #endregion
- #region Transaction
-
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- MySqlTransaction tr = null;
- try
- {
- conn.Open();
- tr = conn.BeginTransaction();
- MySqlCommand cmd = new MySqlCommand();
- cmd = new MySqlCommand("Insert into student (Name,Address,Mobile,Email ) values (@Name,@Address,@Mobile,@Email)", conn,tr);
- cmd.Parameters.AddWithValue("@Name", txtName.Text);
- cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
- cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);
- cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
- cmd.ExecuteNonQuery();
- cmd = new MySqlCommand("Insert into courses (Course,Name,Amount ) values (@Course,@Name1,@Amount )", conn,tr);
- cmd.Parameters.AddWithValue("@Name1", txtName.Text);
- cmd.Parameters.AddWithValue("@Course",ddlCourse.SelectedValue.ToString());
- cmd.Parameters.AddWithValue("@Amount",txtAmount.Text);
- cmd.ExecuteNonQuery();
- tr.Commit();
- cmd.Dispose();
- ShowMessage("Student Course Enrollment Successfully......!");
- clear();
- BindStudentGridView(); BindCoursesGridView();
- }
- catch (MySqlException ex)
- {
- tr.Rollback();
- ShowMessage(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- #endregion
-
- protected void btnCancel_Click(object sender, EventArgs e)
- {
- clear();
- }
- }
- }
Now run the page, it will look like the following.
Now, Student information Enter and Submit, it will look like the following:
Now, show in the Message box “Student Course Enrollment Successfully”. And show the data to GridViewStudent and GridViewCourses.
Now, open the MySQLAdmin Page then show the Student and Courses table data
I hope this article is useful. If you have any other questions then please provide your comments below.