CRUD(CREATE,RETRIEVE,UPDATE,DELETE) IN ADO.NET

This article shows how to do Create, Retrieve, Update, Delete in ASP.NET web application. Follow these below steps:

Step-1: Create Table

First, create a table "dbo.Tbl_Student" in the database.

CREATE TABLE [dbo].[Tbl_Student] (
    [Student_Id]      INT           IDENTITY (1, 1) NOT NULL,
    [Student_Name]    VARCHAR (50)  NULL,
    [Student_Age]     INT           NULL,
    [Student_Address] VARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);

Step-2: Create a new ASP.Net web application

Now create the one sample application "Demo_Asp_DotNet" as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master page).
  3. Provide the web site a name such as  "Demo_Asp_DotNet" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" - "WebForm1.aspx page".
  5. Drag and drop required controls on the <form> section of the WebForm1.aspx page.

Step-3: Write ConnectionString in web.config

Then Join Database with ASP.Net Webpage using ConnectionString. Write your connectionstring in web.config
<connectionStrings>
    <add name="connection" connectionString="DataSource=severname;
InitialCatlog=databasename;Uid=; password=password;Integrated Security=True" />
</connectionStrings>

e.g:

<connectionStrings>
    <add name="connection" connectionString="Data Source=DBS\SQLINT;
Initial Catalog=Database1;User ID=induction;Password=***********;
Integrated Security=True" />
</connectionStrings>

Step-4: Write Code in Design Page 

Write this below code in "WebForm1.aspx" design page.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 37%;
            height: 50px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table align="center" class="auto-style1" style="border-style: solid">
            <tr>
                <td style="width: 50%; text-align: right;">Search By Id : </td>
                <td style="width: 50%">
                    <asp:TextBox ID="txtid" runat="server" AutoPostBack="true" OnTextChanged="txtid_TextChanged"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 50%; text-align: right;">&nbsp;</td>
                <td style="width: 50%">&nbsp;</td>
            </tr>
            <tr>
                <td style="width: 50%; text-align: right;">Name : </td>
                <td style="width: 50%">
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 50%; text-align: right;">Age : </td>
                <td style="width: 50%">
                    <asp:TextBox ID="txtage" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 50%; text-align: right;">Address : </td>
                <td style="width: 50%">
                    <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 50%; text-align: right;">
                    <asp:Button ID="btnsave" runat="server" OnClick="btnsave_Click" Text="Save" Width="72px" />
                </td>
                <td style="width: 50%">
                    <asp:Button ID="btnreset" runat="server" OnClick="btnreset_Click" Text="Reset" Width="72px" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Step-5: Write Code in .cs page or Back-end Page

Write this below code in "WebForm1.aspx.cs" page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace Demo_Asp_DotNet
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connection"].ToString());
        SqlDataAdapter da;
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public void clear()
        {
            txtaddress.Text = "";
            txtage.Text = "";
            txtname.Text = "";
            txtid.Text = "";
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            if (btnsave.Text == "Save")
            {
                da = new SqlDataAdapter("insert into Tbl_Student values('" + txtname.Text + "'," + Convert.ToInt32(txtage.Text) + ",'" + txtaddress.Text + "')", con);
                dt = new DataTable();
                da.Fill(dt);
                Response.Write("<script>alert('Successfully Saved...')</script>");
                clear();
            }
            else if(btnsave.Text=="Update")
            {
                da = new SqlDataAdapter("Update Tbl_Student set Student_Name='" + txtname.Text + "',Student_Age=" + Convert.ToInt32(txtage.Text) + ",Student_Address='" + txtaddress.Text + "' where Student_Id=" + Convert.ToInt32(txtid.Text) + "", con);
                dt = new DataTable();
                da.Fill(dt);
                Response.Write("<script>alert('Successfully Updated...')</script>");
                clear();
                btnsave.Text = "Save";
                btnreset.Text = "Reset";
            }
        }
        protected void btnreset_Click(object sender, EventArgs e)
        {
            if (btnreset.Text == "Reset")
            {
                clear();
            }
            else if(btnreset.Text=="Delete")
            {
                da = new SqlDataAdapter("Delete from Tbl_Student where Student_Id=" + Convert.ToInt32(txtid.Text) + "", con);
                dt = new DataTable();
                da.Fill(dt);
                Response.Write("<script>alert('Successfully Deleted...')</script>");
                clear();
            }
        }
        protected void txtid_TextChanged(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("Select * from Tbl_Student where Student_Id=" + Convert.ToInt32(txtid.Text) + "", con);
            dt = new DataTable();
            da.Fill(dt);
            if(dt.Rows.Count>0)
            {
                txtname.Text = dt.Rows[0]["Student_Name"].ToString();
                txtage.Text = dt.Rows[0]["Student_Age"].ToString();
                txtaddress.Text = dt.Rows[0]["Student_Address"].ToString();
            }
            btnsave.Text = "Update";
            btnreset.Text = "Delete";
        }
    }
}

Output:



asp.net, asp net, ado net,ado.net,c#, create data in sql server in asp.net,Simple CRUD Operation In ASP.NET, CRUD operations in Silverlight using ADO.NET Data Service,CRUD Operations using ADO.Net and C# in ASP.Net,CRUD with ADO.NET,What is Create, Retrieve, Update and Delete (CRUD) using ADO.Net and C# in ASP.Net,Retrieve, Insert, Update & Delete Using ADO.NET

Post a Comment

0 Comments