<

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

Published on
10,610 Points
6,210 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

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Join & Write a Comment

Basic Overview of office 365 user portal
Learn how to collaborate with office 365 Office Online

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month