ASP.NET using web.config file contains the Database connection string

web.config file


<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on how to configure your ASP.NET application, please visit https://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
 
  <connectionStrings>
    <add name="SampleDBConStr" 
         connectionString="Data Source=PC377553;
          Initial Catalog=SampleDB;
          Integrated Security=True;"/>
  </connectionStrings>
 
  <appSettings>
   <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
 
  <system.codedom>
    <compilers>
    </compilers>
  </system.codedom>
 
</configuration>

DataInsertVersion2.aspx

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

<!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>


sp_SaveCandidateRecord stored procedure

create procedure sp_SaveCandidateRecord
(
@CandName varchar(20),
@DateofBirth date,
@Age int,
@JoinDate date,
@EmailID varchar(20),
@Password varchar(10)
)
as
begin
insert into tabCandRegistration values(@CandName,@DateofBirth,@Age,@JoinDate,@EmailID,@Password)
end

DataInsertVersion2.aspx.cs

using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace WebDemo01
{
    public partial class DataInsertVersion2 : 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 = ConfigurationManager
                .ConnectionStrings["SampleDBConStr"].ConnectionString;
                   
                SqlConnection con = new SqlConnection(conStr);
                con.Open();
                //step2 (Initialize command object with query)

                SqlCommand cmd = 
                new SqlCommand("sp_SaveCandidateRecord", con);
                cmd.CommandType = CommandType.StoredProcedure;

                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