Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Connect DotNet Application With DataBase

Viewed:  1550 
Posted On:  21/12/2012 23:37:11 
In this Article we will learn how we can make a connection with DataBase. 

Hi this is my first article here on In this article I am trying to show how we can connect our ASP.NET application with SqlServer database. I am new in .net, I am also learning so if any thing left please let me know.

This is my Sql DataTable

Image 1.

Below is my ASPX code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Connect Application With Data Base</title>
    <form id="form1" runat="server">
        <table cellpadding="1" cellspacing="1" border="1px" width="80%" align="center">
                    <asp:GridView ID="MyGrid" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
                        OnPageIndexChanged="MyGrid_PageIndexChanged" OnSorting="MyGrid_Sorting" Width="100%">
                        <RowStyle BackColor="#EFF3FB" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#2461BF" />
                        <AlternatingRowStyle BackColor="White" />

Now this is my ASPX.cs code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
    string connect = @"Data Source=.;Initial Catalog=MyData; Uid=sa; Pwd=India@123";
    SqlConnection con;
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)

    private void BindData()

       con =
new SqlConnection(connect);
        cmd.CommandText = "SELECT * FROM [User]";
        da = new SqlDataAdapter(cmd);
        cmd.Connection = con;
        MyGrid.DataSource = ds;
    protected void MyGrid_PageIndexChanged(object sender, EventArgs e)
    protected void MyGrid_Sorting(object sender, GridViewSortEventArgs e)

When you run the application then output will be:


Image 2.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us