ASP.NET Storing data in SQL Server | Inserting Record to DB

Database Commands

create database SampleDB

create table tabCandRegistration
(
CandName varchar(20),
DateofBirth date,
Age int,
JoinDate date,
EmailID varchar(20),
[Password] varchar(10)
)

insert into tabCandRegistration values
('Pavan','4/21/1997',22,'10/29/2019','pavan@cognizant.com','abc123')

insert into tabCandRegistration values

(@CandName,@DateofBirth,@Age,@JoinDate,@EmailID,@Password)


DataInsert.aspx 

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            height: 29px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <h1>Candidate Registration</h1>
        <div>
            <table style="width: auto;">
                <tr>
                    <td class="auto-style1">Candidate Name</td>
                    <td class="auto-style1">
                        <asp:TextBox ID="txtName" runat="server" MaxLength="20"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Date of Birth</td>
                    <td>
                        <asp:TextBox ID="txtDateOfBirth" runat="server" MaxLength="10"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Enter Age (14 to 59)</td>
                    <td>
                        <asp:TextBox ID="txtAge" runat="server" MaxLength="2"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Join Date</td>
                    <td>
                        <asp:TextBox ID="txtJoinDate" runat="server" MaxLength="10"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Email ID</td>
                    <td>
                        <asp:TextBox ID="txtEmailID" runat="server" MaxLength="20"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">Enter Login Password</td>
                    <td class="auto-style1">
                        <asp:TextBox ID="txtPassword1" runat="server" MaxLength="10"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Confirm Password</td>
                    <td>
                        <asp:TextBox ID="txtPassword2" runat="server" MaxLength="10"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Button ID="btnRegister" runat="server" Text="Register" Width="100px" OnClick="btnRegister_Click" BackColor="#009933" />&nbsp;<asp:Button ID="btnClear" runat="server" Text="Next&gt;&gt;" BackColor="Yellow" OnClick="btnClear_Click" Width="60px" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblMsg" runat="server" Text="Registration status..."></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        &nbsp;</td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

DataInsert.aspx.cs

using System;
using System.Data.SqlClient;

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

        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                //Step1 (Connection estab)
                string conStr = "Data Source=PC377553;Initial Catalog=SampleDB;Integrated Security=True;";
                SqlConnection con = new SqlConnection(conStr);
                con.Open();
                //step2 (Initialize command object with query)
                string cmdText = "insert into tabCandRegistration values(@CandName,@DateofBirth,@Age,@JoinDate,@EmailID,@Password)";
                SqlCommand cmd = new SqlCommand(cmdText, con);
                cmd.Parameters.AddWithValue("@CandName",txtName.Text);
                cmd.Parameters.AddWithValue("@DateofBirth",txtDateOfBirth.Text);
                cmd.Parameters.AddWithValue("@Age",txtAge.Text);
                cmd.Parameters.AddWithValue("@JoinDate",txtJoinDate.Text);
                cmd.Parameters.AddWithValue("@EmailID",txtEmailID.Text);
                cmd.Parameters.AddWithValue("@Password",txtPassword2.Text);
                //step3 -(Execute command object)
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
                //step4 - send ack
                lblMsg.Text = txtName.Text + "'s Record Saved....";
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.ToString();
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtDateOfBirth.Text = "";
            txtAge.Text = "";
            txtJoinDate.Text = "";
            txtEmailID.Text = "";
            txtPassword1.Text = "";
            txtPassword2.Text = "";
            lblMsg.Text = "Enter next record..";
            txtName.Focus();
        }
    }
}


No comments:

Post a Comment