Database connection with asp.net using c#


In this demo use the asp component Ajax, grid view, textboxes, buttons, table.

Download This Demo: http://www.4shared.com/file/_7bHhSY7/WebSite1.html

1)      First add the Ajax component .in default.aspx  you find it in the Ajax extensions

                1)      Add “script manager”.

                2)      Add “update panel”

                3)      Within “update panel” add “content template tag”.

                4)      In content template add the following component buttons, textboxes and lable and the grid view and rename that buttons insert, update, delete and view record.

2)       Open the default.cs file and define the following global variables

string qry;
string connection = "Data Source=.;Initial Catalog=info;Integrated Security=True";
 SqlConnection con;
SqlCommand cmd;
SqlDataSource sds = new SqlDataSource();

 3)    Right click on the insert button and write the following code:


qry = "insert into customer values('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')";

        con = new SqlConnection(connection);

        cmd = new SqlCommand(qry, con);

        con.Open();

        cmd.ExecuteNonQuery();

        con.Close();

       Label1.Text = "Record Inserted.";

4)      Right click on the “view record” button and write the following code for display the record in grid view :

qry = "select * from customer";

       sds = new SqlDataSource(connection, qry);

GridView1.DataSource = sds;

GridView1.DataBind();

 

 

2)      For update button right click on the button and write the following code in it:

qry = "update customer set name='" + TextBox2.Text + "',phone='" + TextBox3.Text + "',email='" + TextBox4.Text + "'where id='" + TextBox1.Text + "'";

con = new SqlConnection(connection);

cmd = new SqlCommand(qry, con);

con.Open();
cmd.ExecuteNonQuery();

 

 

3)      For delete the record write the following code:

qry = "delete from customer where id='" + TextBox1.Text + "'";

        con = new SqlConnection(connection);

        cmd = new SqlCommand(qry, con);

        con.Open();

        cmd.ExecuteNonQuery();

        con.Close();

 

Default.aspx code:

  <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .style1
        {
            width: 152px;
        }
        .style2
        {
            width: 644px;
        }
    </style>
</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <table style="width: 100%;">
        <tr>
            <td>
                &nbsp;
                ID</td>
            <td>
                &nbsp;
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Label ID="Label2" runat="server" Text="use id for search"></asp:Label>
            </td>
            <td>
                <asp:Label ID="Label1" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
                name</td>
            <td>
                &nbsp;
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                    ControlToValidate="TextBox2" ErrorMessage="Name is requird"></asp:RequiredFieldValidator>
            </td>
         
        </tr>
        <tr>
            <td>
                &nbsp;
                phone number</td>
            <td>
                &nbsp;
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                    ControlToValidate="TextBox3" ErrorMessage="Phone number is requird"></asp:RequiredFieldValidator>
            </td>
          
        </tr>
        <tr>
            <td>
                &nbsp; E-mail</td>
            <td>
                &nbsp;
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                    ControlToValidate="TextBox4" ErrorMessage="Email is requird"
                    ForeColor="#CC0000"></asp:RequiredFieldValidator>
            </td>
         
        </tr>
        <tr>
            <td>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</td>
            <td align="left">
              
                <asp:Button ID="Button1" runat="server" Text="Insert" onclick="Button1_Click" />
                <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="View record" />
                <asp:Button ID="Button4" runat="server" Text="Update" onclick="Button4_Click" />
                <asp:Button ID="Button3" runat="server" Text="Delete" onclick="Button3_Click" />
                <br />
                <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
                    GridLines="None">
                    <AlternatingRowStyle BackColor="White" />
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>
            </td>
        </tr>
     
    </table>
</ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

 

Default.cs code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

{

    string qry;

    string connection = "Data Source=.;Initial Catalog=info;Integrated Security=True";

    SqlConnection con;

    SqlCommand cmd;

    SqlDataSource sds = new SqlDataSource();

    protected void Button2_Click(object sender, EventArgs e)

    {

        qry = "select * from customer";

        sds = new SqlDataSource(connection, qry);

        GridView1.DataSource = sds;

        GridView1.DataBind();

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        qry = "insert into customer values('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')";

        con = new SqlConnection(connection);

        cmd = new SqlCommand(qry, con);

        con.Open();

        cmd.ExecuteNonQuery();

        con.Close();

        Label1.Text = "Record Inserted.";

    }

    protected void Button4_Click(object sender, EventArgs e)

    {

        qry = "update customer set name='" + TextBox2.Text + "',phone='" + TextBox3.Text + "',email='" + TextBox4.Text + "'where id='" + TextBox1.Text + "'";

        con = new SqlConnection(connection);

        cmd = new SqlCommand(qry, con);

        con.Open();

        cmd.ExecuteNonQuery();

    }

    protected void Button3_Click(object sender, EventArgs e)

    {

        qry = "delete from customer where id='" + TextBox1.Text + "'";

        con = new SqlConnection(connection);

        cmd = new SqlCommand(qry, con);

        con.Open();

        cmd.ExecuteNonQuery();

        con.Close();

    }

}