<

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

Published on
10,900 Points
6,500 Views
4 Endorsements
Last Modified:
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
Enjoy this complimentary article view.

Get unlimited access to our entire library of technical procedures, guides, and tutorials written by certified industry professionals.

Get 7 days free
Click here to view the full article

Using this article for work? Experts Exchange can benefit your whole team.

Learn More
COLLABORATE WITH CERTIFIED PROFESSIONALS
Experts Exchange is a tech solutions provider where users receive personalized tech help from vetted certified professionals. These industry professionals also write and publish relevant articles on our site.
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Learn from the best.