Sunday, 14 May 2017

How to Insert, Edit, Delete and Show data in database through the connected method

Default.aspx File
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="DemoClassExample._Default" %>
<head>
    <style type="text/css">
        .style2
        {
            width: 100%;
        }
        .style1
        {
            width: 40%;
            margin-left: 2px;
            height: 352px;
        }
        .style3
        {
            width: 971px;
        }
    </style>
</head>
<form id="form1" runat="server">
<table class="style2">
    <tr>
        <td class="style3">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="3000"
                ontick="Timer1_Tick">
            </asp:Timer>
        </td>
    </tr>
    <tr>
        <td align="center" class="style3">
<table bgcolor="#FFFF66" class="style1">
    <tr>
        <td align="center" colspan="3"
            style="font-family: Georgia; font-size: small; font-weight: bold; font-style: normal; font-variant: normal; color: #800000">
            Registration</td>
    </tr>
    <tr>
        <td>
            RegistrationId</td>
        <td>
            <asp:TextBox ID="txtRegId" runat="server"></asp:TextBox>
        </td>
        <td align="center">
            <asp:Button ID="tnRegId" runat="server" BackColor="#CCCCCC" Font-Bold="True"
                ForeColor="Maroon" Text="RegID" onclick="tnRegId_Click" />
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            Name</td>
        <td>
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                ControlToValidate="txtName" ForeColor="Red" SetFocusOnError="True"
                ErrorMessage="Enter Your Name">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            Address</td>
        <td>
            <asp:TextBox ID="txtAddress" runat="server" Height="31px" TextMode="MultiLine"
                Width="126px"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                ControlToValidate="txtAddress" ForeColor="Red" SetFocusOnError="True"
                ErrorMessage="Enter Your Address">#</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            MobileNo</td>
        <td>
            <asp:TextBox ID="txtMobiNo" runat="server"></asp:TextBox>
        </td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            Course</td>
        <td>
            <asp:TextBox ID="txtCourse" runat="server"></asp:TextBox>
        </td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            Sex</td>
        <td>
            <asp:DropDownList ID="ddlGender" runat="server" Font-Bold="True" Height="20px"
                Width="126px">
                <asp:ListItem>----Select----</asp:ListItem>
                <asp:ListItem>Male</asp:ListItem>
                <asp:ListItem Value="Female">Female</asp:ListItem>
            </asp:DropDownList>
        </td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            EmailID</td>
        <td>
            <asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
        </td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            DOB</td>
        <td>
            <asp:TextBox ID="txtDOB" runat="server" TextMode="Date"></asp:TextBox>
        </td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td colspan="3">
            <asp:Label ID="lblMsg" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red"
                ShowMessageBox="True" ShowSummary="False" />
        </td>
    </tr>
    <tr>
        <td align="center">
            <asp:Button ID="btnSave" runat="server" BackColor="#CCCCCC" Font-Bold="True"
                ForeColor="Maroon" onclick="btnSave_Click" Text="Save" />
        </td>
        <td align="center">
            <asp:Button ID="btnUpdate" runat="server" BackColor="#CCCCCC" Font-Bold="True"
                ForeColor="Maroon" Text="Update" onclick="btnUpdate_Click" />
            <asp:Button ID="btnDelete" runat="server" BackColor="#CCCCCC" Font-Bold="True"
                ForeColor="Maroon" Text="Delete" onclick="btnDelete_Click" />
        </td>
        <td align="center">
            <asp:Button ID="btnReset" runat="server" BackColor="#CCCCCC" Font-Bold="True"
                ForeColor="Maroon" onclick="btnReset_Click" Text="Reset" />
        </td>
    </tr>
</table>
        </td>
    </tr>
    <tr>
        <td class="style3">
            <asp:Button ID="btnShow" runat="server" onclick="btnShow_Click" Text="Show"
                BackColor="#CCCCCC" Font-Bold="True" ForeColor="Maroon" />
            <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>
    <tr>
        <td class="style3">
            &nbsp;</td>
    </tr>
</table>
</form>

Default.aspx.cs File
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;

namespace DemoClassExample
{
    public partial class _Default : System.Web.UI.Page
    {
        string StrCon = "Server=PRADEEP-HP;Database=Decofloore2;User Id=User_006;password=user006";
        protected void Page_Load(object sender, EventArgs e)
        {
         
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection SqlCon = new SqlConnection(StrCon);
            string SqlQuery = null;
            SqlQuery = "Insert into ShowOnePage(RegistrationId,Name,Address,MobileNo,Course,Sex,EmailID,DOB) values"
                + "('" + txtRegId.Text.Trim() + "','" + txtName.Text.Trim() + "','" + txtAddress.Text.Trim() + "',"
            + "" + txtMobiNo.Text.Trim() + ",'" + txtCourse.Text.Trim() + "','" + ddlGender.SelectedItem.Text + "','" + txtEmailId.Text.Trim() + "','" + txtDOB.TextMode.ToString() + "'";
            SqlCommand SqlComm = new SqlCommand(SqlQuery, SqlCon);
            SqlCon.Open();
            int RecordInserted;
            RecordInserted = SqlComm.ExecuteNonQuery();
            if (RecordInserted > 0)
            {
                Timer1.Enabled = true;
                lblMsg.Text = "Insert Record Successfully";
            }
            else
            {
                lblMsg.Text = "Records are not Inserted";
            }
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtCourse.Text = "";
            txtAddress.Text = "";
            txtEmailId.Text = "";
            txtMobiNo.Text = "";
            txtName.Text = "";
            txtRegId.Text = "";
            ddlGender.SelectedItem.Text = "";
            lblMsg.Text = "";
        }
        protected void tnRegId_Click(object sender, EventArgs e)
        {
            SqlConnection Sqlcon = new SqlConnection(StrCon);
            string SqlQuery = "Select Name,Address,MobileNo,Course,Sex,EmailID,DOB from ShowOnePage where RegistrationId='" + txtRegId.Text.Trim() + "'";
            SqlCommand Sqlcomm = new SqlCommand(SqlQuery, Sqlcon);
            Sqlcon.Open();
            SqlDataReader dr;
            dr = Sqlcomm.ExecuteReader(CommandBehavior.CloseConnection);
            if (dr.Read())
            {
                txtName.Text = dr["Name"].ToString();
                txtAddress.Text = dr["Address"].ToString();
                txtMobiNo.Text = dr["MobileNo"].ToString();
                txtCourse.Text = dr["Course"].ToString();
                ddlGender.SelectedItem.Text = dr["Sex"].ToString();
                txtEmailId.Text = dr["EmailID"].ToString();
                txtDOB.Text = dr["DOB"].ToString();
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection SqlCon = new SqlConnection(StrCon);
            string StrSql = "Update ShowOnePage set Name='" + txtName.Text.Trim() + "',Address='" + txtAddress.Text + "',MobileNo=" + txtMobiNo.Text + ",Course='" + txtCourse.Text + "',Sex='" + ddlGender.SelectedItem.Text + "',EmailID='" + txtEmailId.Text + "',DOB='" + txtDOB.TextMode.ToString() + "' where RegistrationId='" + txtRegId.Text.Trim() + "'";
            SqlCommand SqlCom = new SqlCommand(StrSql, SqlCon);
            SqlCon.Open();
            int RowAffected;
            RowAffected = SqlCom.ExecuteNonQuery();
            if (RowAffected > 0)
            {
                Timer1.Enabled = true;
                lblMsg.Text = "Update Record Successfully";
            }
            else
            {
                lblMsg.Text = "Record are not Updated";
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            SqlConnection SqlCon = new SqlConnection(StrCon);
            string StrSql = "Delete from ShowOnePage where RegistrationId='" + txtRegId.Text.Trim() + "'";
            SqlCommand SqlCom = new SqlCommand(StrSql, SqlCon);
            SqlCon.Open();
            int RowAffected;
            RowAffected = SqlCom.ExecuteNonQuery();
            if (RowAffected > 0)
            {
                lblMsg.Text = "Delete Record Successfully";
            }
            else
            {
                lblMsg.Text = "Record are not Deleted";
            }
        }
        protected void btnShow_Click(object sender, EventArgs e)
        {
            SqlConnection SqlCon = new SqlConnection(StrCon);
            string SqlQuery = "Select * from ShowOnePage";
            SqlCommand SqlComm = new SqlCommand(SqlQuery, SqlCon);
            SqlCon.Open();
            SqlDataReader dr;
            dr = SqlComm.ExecuteReader(CommandBehavior.CloseConnection);
            GridView1.DataSource = dr;
            GridView1.DataBind();
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Timer1.Enabled = false;
            Response.Redirect("Default.aspx");
        }
    }
}
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos

0 comments

Post a Comment