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

    }

}

 

Advertisements

4 thoughts on “Database connection with asp.net using c#

  1. Sir I am working on the Project “Intranet Mailing System” on asp.net I am trying to implement the spam mail section I am confused on the criteria for the spam mails and how to implement it . sir please guide me.

  2. Believing that you are actually starving, it releases chemicals
    that actually make it harder to lose weight in an effort to conserve energy.
    Authorities inside the area will need to have an intensive understanding of laboratory and
    labeling techniques, infection security precautions, appropriate blood attract approaches
    to the elderly and for infants and even more. The institution has persistently offered ideal programs and
    services for those struggling to achieve certain levels of body weight.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s