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

Connect DotNet Application With DataBase

Article:
Viewed:  961 
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 www.codesg.com. 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

SqlTable.png
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Connect Application With Data Base</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="1" cellspacing="1" border="1px" width="80%" align="center">
            <tr>
                <td>
                    <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" />
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html> 

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)
            BindData();
    } 

    private void BindData()
    {

       con =
new SqlConnection(connect);
        cmd.CommandText = "SELECT * FROM [User]";
        da = new SqlDataAdapter(cmd);
        cmd.Connection = con;
        da.Fill(ds);
        con.Open();
        cmd.ExecuteNonQuery();
        MyGrid.DataSource = ds;
        MyGrid.DataBind();
    }
 
    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:

ConnectAppWithDataBase.png

Image 2.

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