Monday 9 November 2015

Insert, Update And Delete in ASP.NET



Data Base Design




create database Form

use Form

create table Employee
(
[ID] int identity primary key,
[Name] varchar (25),
[Email] varchar (25),
[Password] varchar (25),
);

select * from Employee


Insert, Update And Delete in ASP.NET






<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication6.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="txtUpdate" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
        <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
        <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
   
    </div>
    </form>
</body>
</html>



Insert, Update And Delete Coding in ASP.NET


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;
using System.Data.Sql;
using System.Configuration;

namespace WebApplication6
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        string path = ConfigurationManager.ConnectionStrings["FormConnectionString"].ConnectionString;
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(path);
            SqlCommand cmd = new SqlCommand("insert into Employee values('" + txtName.Text + "','" + txtEmail.Text + "', '" + txtPass.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();

            txtName.Text = "";
            txtEmail.Text = "";
            txtPass.Text = "";
        }

        string Update = ConfigurationManager.ConnectionStrings["FormConnectionString"].ConnectionString;
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Update);
            SqlCommand cmd = new SqlCommand("update Employee set Name='"+txtName.Text+"', Email='"+txtEmail.Text+"', Password='"+txtPass.Text+"' where id = '"+txtUpdate.Text+"' ", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            txtUpdate.Text = "";
            txtName.Text = "";
            txtEmail.Text = "";
            txtPass.Text = "";

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Update);
            SqlCommand cmd = new SqlCommand("delete from Employee where id = '" + txtUpdate.Text + "' ", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            txtUpdate.Text = "";
            txtName.Text = "";
            txtEmail.Text = "";
            txtPass.Text = "";
        }

       
    }
}


No comments:

Post a Comment