Wednesday, February 18, 2015

Simple binding sqldata table to gridview in c# and vb net

Posted by Gnani Palepu  |  No comments


 simple binding sqldata table to gridview in c# and vb net  
 -----------------------------------------------------------  
 just follow 3 steps  
 ----------------------  
 reference  
 -------------:http://www.aspsnippets.com/Articles/Connect-Bind-GridView-to-MySql-database-in-ASPNet-using-C-and-VBNet.aspx  
 step 1:  
 ----------  
 <connectionStrings>  
   <addname="constr"connectionString="Data Source=localhost;port=3306;Initial Catalog=SampleDB;User Id=mudassar;password=pass@123"/>  
 </connectionStrings>  
 step 2:  
 ------------  
  C#  
 using System.Data;  
 using System.Configuration;  
 using MySql.Data.MySqlClient;  
  VB.Net  
 Imports System.Data  
 Imports System.Configuration  
 Imports MySql.Data.MySqlClient  
 step 3  
 ------------  
  C#  
 protected void Page_Load(object sender, EventArgs e)  
 {  
   if (!this.IsPostBack)  
   {  
       string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
       using (MySqlConnection con = new MySqlConnection(constr))  
       {  
         using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM Customers"))  
         {  
           using (MySqlDataAdapter sda = new MySqlDataAdapter())  
           {  
             cmd.Connection = con;  
             sda.SelectCommand = cmd;  
             using (DataTable dt = new DataTable())  
             {  
               sda.Fill(dt);  
               GridView1.DataSource = dt;  
               GridView1.DataBind();  
             }  
           }  
         }  
       }  
   }  
 }  
 vb.net  
 ------  
  VB.Net  
 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load  
   If Not Me.IsPostBack Then  
     Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString  
     Using con As New MySqlConnection(constr)  
       Using cmd As New MySqlCommand("SELECT * FROM Customers")  
         Using sda As New MySqlDataAdapter()  
           cmd.Connection = con  
           sda.SelectCommand = cmd  
           Using dt As New DataTable()  
             sda.Fill(dt)  
             GridView1.DataSource = dt  
             GridView1.DataBind()  
           End Using  
         End Using  
       End Using  
     End Using  
   End If  
 End Sub  

1:56 AM Share:

0 comments:

Get updates in your email box
Complete the form below, and we'll send you the best coupons.

Deliver via FeedBurner
Proudly Powered by Blogger.
back to top