Using MySql With C#.Net and GridView Paging

In this article, I'm explaining how to add data to the database and how to show the data in a Gridview in C# with MySQL with Paging. This is a very useful article for whoever uses a MySQL Database with C# for the first time.
Steps:
  1. Create a Web Application like "Gridviewincsharpmysql.aspx".
  2. Copy the MySql.Data.dll from the MySQL website and paste it into the Bin folder of your application.
  3. Create a table, Emp_master. 
The Aspx page has three tables tblone, tbladd, and tblgv.
 
The .aspx code and aspx.cs code is available in the Downloads Zip. 
 
In the page_Load Event, I checked the value of the Session object for a null value. If it is null then I assigned the value "1" to the session object.
 
After that, I checked the postback method, if not postback until that movement I called the Select Method. The Select method has the two methods First(0) and gvwithpaging(gview, qry()) 
 
First(0) shows the tables tblone, tbladd, and tblgv depending on the method parameter value.
 
gvwithpaging(gview, qry()) is to bind the values to the gridview. The gvwithpaging method has DatasetBinding(out ds, some). 
 
At the time of Databinding the gview_rowDataBound event is fired. When the page changes, gview_PageIndexChanging is fired. 
 
When we try to refresh the page, the last data entered will again be entered into the database. To overcome that we are changing the session object value from stage to stage. Finally, the PreRender Event is fired. 
 
In the web.config file I added my applications settings like this: 
 
You have to change the values where XXXXX are shown, with your Database connection values.
  1. <appSettings>  
  2.     <add key="Mysql" value="Data Source=XXXXX;User ID=XXXXXX; password=XXXXX; Database=XXXXX"></add>  
  3. </appSettings>  
This is the Code-behind page code
  1. public partial class Gridviewincsharpmysql: System.Web.UI.Page {  
  2.     protected void Page_Load(object sender, EventArgs e) {  
  3.         if (Session["RefreshCtr"] == null) {  
  4.             Session["RefreshCtr"] = 1;  
  5.             lblRefresh.Text = Session["RefreshCtr"].ToString();  
  6.         }  
  7.         if (!IsPostBack) {  
  8.             Select();  
  9.         }  
  10.     }  
  11.     public void Select() {  
  12.         first(0);  
  13.         gvwithpaging(gview, qry());  
  14.     }  
  15.     private string qry() {  
  16.         string myquery = "";  
  17.         myquery = "SELECT Emp_Id, Emp_FName, Emp_LName,  Emp_Gender, Emp_P_City, Emp_P_Country,Emp_P_PIN,Emp_Phone, mp_Bank_Name, PAN_NO FROM test.emp_master";  
  18.         return myquery;  
  19.     }  
  20.     public void gvwithpaging(GridView gv, string some) {  
  21.         DataSet ds = null;  
  22.         if (gv.PageIndex == 0) {  
  23.             int myindex = 0;  
  24.             gv.PageIndex = myindex;  
  25.         }  
  26.         DatasetBinding(out ds, some);  
  27.         gv.DataSource = ds;  
  28.         gv.DataBind();  
  29.     }  
  30.     public void DatasetBinding(out DataSet dset, string myqry) {  
  31.         MySqlConnection myconn = new MySqlConnection(ConfigurationManager.AppSettings["Mysql"]);  
  32.         myconn.Open();  
  33.         MySqlCommand mycomm = new MySqlCommand();  
  34.         mycomm.Connection = myconn;  
  35.         mycomm.CommandType = CommandType.Text;  
  36.         mycomm.CommandText = myqry;  
  37.         MySqlDataAdapter myadapter = new MySqlDataAdapter(mycomm);  
  38.         DataSet dts = new DataSet();  
  39.         myadapter.Fill(dts);  
  40.         dset = dts;  
  41.         mycomm.Dispose();  
  42.         myconn.Close();  
  43.     }  
  44.     public void gview_RowDatabound(object sender, GridViewRowEventArgs e) {  
  45.         int page = gview.PageIndex + 1;  
  46.         int count = gview.PageCount;  
  47.     }  
  48.     public void first(int a) {  
  49.         if (a == 0) {  
  50.             tblgv.Visible = true;  
  51.             tbladd.Visible = true;  
  52.             tblone.Visible = false;  
  53.         }  
  54.         if (a == 1) {  
  55.             tblgv.Visible = false;  
  56.             tbladd.Visible = false;  
  57.             tblone.Visible = true;  
  58.         }  
  59.     }  
  60.     protected void gview_PageIndexChanging(object sender, GridViewPageEventArgs e) {  
  61.         gview.PageIndex = e.NewPageIndex;  
  62.         gvwithpaging(gview, qry());  
  63.         int pagenumber = gview.PageIndex + 1;  
  64.         Response.Write("You are at page number " + pagenumber);  
  65.     }  
  66.     public void ConnectionString() {  
  67.         int Emp_Id = Convert.ToInt32(TextBox1.Text);  
  68.         string Emp_FName = TextBox4.Text;  
  69.         string Emp_LName = TextBox5.Text;  
  70.         string Emp_Gender = TextBox6.Text;  
  71.         string Emp_P_City = TextBox7.Text;  
  72.         string Emp_P_Country = TextBox8.Text;  
  73.         string Emp_P_PIN = TextBox9.Text;  
  74.         string Emp_Phone = TextBox10.Text;  
  75.         string Emp_Bank_Name = TextBox11.Text;  
  76.         string PAN_NO = TextBox12.Text;  
  77.         MySqlConnection myconn = new MySqlConnection(ConfigurationManager.AppSettings["Mysql"]);  
  78.         myconn.Open();  
  79.         MySqlCommand mycomm = new MySqlCommand();  
  80.         mycomm.Connection = myconn;  
  81.         mycomm.CommandType = CommandType.Text;  
  82.         string myqry = "insert into test.emp_master(Emp_Id, Emp_FName, Emp_LName, Emp_Gender, Emp_P_City, Emp_P_Country, Emp_P_PIN, Emp_Phone, Emp_Bank_Name, PAN_NO)values('" + Emp_Id + "','" + Emp_FName + "','" + Emp_LName + "','" + Emp_Gender + "','" + Emp_P_City + "','" +  
  83.                        Emp_P_Country + "','" + Emp_P_PIN + "','" + Emp_Phone + "','" + Emp_Bank_Name + "','" + PAN_NO + "')";  
  84.         mycomm.CommandText = myqry;  
  85.         MySqlDataAdapter myadapter = new MySqlDataAdapter(mycomm);  
  86.         DataSet ds = new DataSet();  
  87.         myadapter.Fill(ds);  
  88.         mycomm.Dispose();  
  89.         myconn.Close();  
  90.     }  
  91.     protected void Addnew(object sender, EventArgs e) {  
  92.         first(1);  
  93.         Clearall();  
  94.     }  
  95.     protected void Button1_click(object sender, EventArgs e) {  
  96.         if (lblRefresh.Text == Session["RefreshCtr"].ToString()) {  
  97.             Session["RefreshCtr"] = Session["RefreshCtr"].ToString() + 1;  
  98.             ConnectionString();  
  99.             Select();  
  100.         }  
  101.         first(0);  
  102.     }  
  103.     private void Clearall() {  
  104.         TextBox1.Text = "";  
  105.         TextBox4.Text = "";  
  106.         TextBox5.Text = "";  
  107.         TextBox6.Text = "";  
  108.         TextBox7.Text = "";  
  109.         TextBox8.Text = "";  
  110.         TextBox9.Text = "";  
  111.         TextBox10.Text = "";  
  112.         TextBox11.Text = "";  
  113.         TextBox12.Text = "";  
  114.     }  
  115.     private void Page_PreRender(object sender, EventArgs e) {  
  116.         lblRefresh.Text = Session["RefreshCtr"].ToString();  
  117.     }  

When we run this we will see this.
 
mysqlGridview.gif
 
Click AddNew Button

This table will appear,
 
mysqlGridview2.gif

You may face these errors:

1. Overload method Gridview_pageiindexchanging matches delegate.

Solution:

Declare pageiindexchanging event from the design page. If we write the onclick property from the source page of the aspx file then the Pageindexchanging will not fire.


Similar Articles