<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

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

Published on
10,732 Points
6,332 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
Comment
2 Comments
LVL 22

Expert Comment

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

Thanks
Tapan Pattanaik.
0
LVL 39

Author Comment

by:Pratima Pharande
Thank you Tapan
0

Featured Post

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Join & Write a Comment

If you, like me, have a dislike for using Online Subscription anti-spam services, then this video series is for you. I have an inherent dislike of leaving decisions such as what is and what isn't spamming to other people or services for me and insis…
I previously published an Experts Exchange video Micro Tutorial that describes how to scan documents to a PDF file using an excellent, free product called Foxit Reader: How to scan to a PDF file with free software (https://www.experts-exchange.co…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month