Tutorial for new developers in Asp.net - connectivity with Sql server for ( vb & C#) and fill the Datagrid or Gridview .

Pratima
CERTIFIED EXPERT
Published:
Updated:
When anyone starts newly development using ASP.net this is very helpful tutorial for connectivity with SQL Server.
 

1. STEP 1[b]

Import following namespaces[]/b] in codebehind page
VB :
imports System.data
imports System.data.sqlclient
C# :
using System.data;
using System.data.sqlclient;
 

2. STEP 2

Create Your Connection string
Example:  Data Source=[Database Server Name];Initial Catalog=[Database Name];User ID=[Database Access UserID];Password=[Database Password]

Declare One Variable and Store above connection string in it Like below
VB :
Dim strConnectionString As String = "connection string here"
C# :
string strConnectionString = "connection string here";
 

3. STEP 3

Declare connection string object
VB:
Dim cnn As New SqlConnection(strConnectionString)
C# :
 SqlConnection cnn = new SqlConnection(strConnectionString);

 

4. STEP 4

Declare SQL command object and provide in query from database which you want to display in datagrid and initialize it with connection object
VB:
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT * FROM table ", cnn)
C# :
  SqlClient.SqlCommand cmd = new SqlClient.SqlCommand("SELECT * FROM table ", cnn);
 

5. STEP 5

Set commandType to sqlcommand object.  As we have given Text set it to text ( If we provide storeprocedure name set it to storeprocedure)
VB:
 cmd.CommandType = CommandType.Text
C# :
  cmd.CommandType = CommandType.Text;

 

6. STEP 6

Declare DataAdapter object and initialize with command object
VB:
Dim dacmd As New SqlClient.SqlDataAdapter(cmd)
C# :
  SqlClient.SqlDataAdapter dacmd = new SqlClient.SqlDataAdapter(cmd);

 

7. STEP 7

Declare Dataset object and fill the data from datadapter
VB:
Dim dscmd As New DataSet
        dacmd.Fill(dscmd)
C# :
     DataSet dscmd = new DataSet();
    dacmd.Fill(dscmd);

 

8. STEP 8

Finally, Set the dataset to Datagrid
VB:
 Datagrid1.DataSource = dsContract.Tables(0)
                Datagrid1.DataBind()
C# :
   Datagrid1.DataSource = dsContract.Tables(0);
    Datagrid1.DataBind();
VB :
                       
                          Dim strConnectionString As String = "connetion string here"
                          Dim cnn As New SqlConnection(strConnectionString)
                          Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT * FROM table ", cnn)
                          cmd.CommandType = CommandType.Text
                          Dim dacmd As New SqlClient.SqlDataAdapter(cmd)
                          Dim dscmd As New DataSet
                          dacmd.Fill(dscmd)
                          Datagrid1.DataSource = dsContract.Tables(0)
                          Datagrid1.DataBind()
                       
                       
                      C#:
                       
                       
                      { 
                          string strConnectionString = "connetion string here"; 
                          SqlConnection cnn = new SqlConnection(strConnectionString); 
                          SqlClient.SqlCommand cmd = new SqlClient.SqlCommand("SELECT * FROM table ", cnn); 
                          cmd.CommandType = CommandType.Text; 
                          SqlClient.SqlDataAdapter dacmd = new SqlClient.SqlDataAdapter(cmd); 
                          DataSet dscmd = new DataSet(); 
                          dacmd.Fill(dscmd); 
                          Datagrid1.DataSource = dsContract.Tables(0); 
                          Datagrid1.DataBind(); 
                      } 

Open in new window

4
7,315 Views
Pratima
CERTIFIED EXPERT

Comments (2)

Tapan PattanaikSenior Engineer

Commented:
It is very good article for beginners. those are new to  Asp.net - connectivity with Sql server.

Thanks
Tapan Pattanaik.
CERTIFIED EXPERT

Author

Commented:
Thank you Tapan

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.