Registration Form in Asp.Net C# with Sql Server Database

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


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style>

        .auto-style1{

            width:34%;

        }

        .auto-style2 {

            width: 101px;

        }

        .auto-style3 {

            width: 101px;

            height: 23px;

        }

        .auto-style4 {

            height: 23px;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:Label ID="Label1" runat="server" Font-Bold="true" Text="Registration Form" Font-Size="XX-Large"></asp:Label>

            <br/>

            <br/>

            <br/>

            <asp:Label ID="Label2" runat="server" Text="Registration" Font-Size="Large" ForeColor="#FF9900"></asp:Label>

            <br/>

            <table cellpadding="1" cellspacing="2" class="auto-style1" bgcolor="#FFCC99">

                <tr>

                    <td class="auto-style2">Name</td>

                    <td>

                        <asp:TextBox ID="TextBox1" runat="server" Width="164px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style2">Gender</td>

                    <td>

                        <asp:DropDownList ID="DropDownList1" runat="server" Width="164px">

                            <asp:ListItem Value="Male">Male</asp:ListItem>

                            <asp:ListItem Value="Female">Female</asp:ListItem>

                        </asp:DropDownList>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style2">Contact</td>

                    <td>

                        <asp:TextBox ID="TextBox2" runat="server" Width="164px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style2">Email Address</td>

                    <td>

                        <asp:TextBox ID="TextBox3" runat="server" Width="164px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style3">City</td>

                    <td class="auto-style4">

                        <asp:TextBox ID="TextBox4" runat="server" Width="164px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style2"></td>

                    <td>

                        <asp:Button ID="Button1" runat="server" Text="Register" BackColor="#66FF33" Height="37px" OnClick="Button1_Click" Width="105px"/>

                        <br/>

                        <br/>

                        <asp:Label ID="Label3" runat="server" Text="Registration" Font-Size="Large" ForeColor="#FF0066"></asp:Label>

                    </td>

                </tr>

            </table>

        </div>

    </form>

</body>

</html>

ABOVE FILE IS RegistrationForm.aspx
















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

namespace Registration_Form_Sql
{
    public partial class RegistrationForm : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void EmptyControls()
        {
            TextBox1.Text = "";
            DropDownList1.SelectedIndex = -1;
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "" || DropDownList1.SelectedIndex == -1 || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "")
            {
                Response.Write("<script type='text/javascript'>alert('All fields are required !!')</script>");
            }
            else
            {
                try
                {
                    using (SqlConnection con = new SqlConnection(cs))
                    {
                        con.Open();
                        string query = "INSERT INTO NewRegister(uName,gender,contact,email,city) VALUES(@uName,@gender,@contact,@email,@city);";
                        SqlCommand cmd = new SqlCommand(query, con);
                        cmd.Parameters.AddWithValue("@uName", TextBox1.Text);
                        cmd.Parameters.AddWithValue("@gender", DropDownList1.SelectedItem.ToString());
                        cmd.Parameters.AddWithValue("@contact", TextBox2.Text);
                        cmd.Parameters.AddWithValue("@email", TextBox3.Text);
                        cmd.Parameters.AddWithValue("@city", TextBox4.Text);
                        int a = cmd.ExecuteNonQuery();
                        if (a > 0)
                        {
                            Response.Write("<script type='text/javascript'>alert('Successfully Inserted !!')</script>");
                            Label3.Text = "New Registration Successfully Saved";
                            EmptyControls();
                        }
                        else
                        {
                            Response.Write("<script type='text/javascript'>alert('Insertion Failed !!')</script>");
                        }
                    }
                }
                catch (SqlException ex)
                {
                    Response.Write("SqlException : " + ex.Message);
                }
                catch (Exception ex)
                {
                    Response.Write("Exception : " + ex.Message);
                }
            }
        }
    }
}
ABOVE FILE IS RegistrationForm.aspx.cs

















<?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>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
<connectionStrings>
<add name="dbcs" connectionString="Data Source=DESKTOP-77M6N4G\SQLEXPRESS;Initial Catalog=RegistrationFormData;Integrated Security=True;TrustServerCertificate=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
ABOVE FILE IS Web.config


















CREATE DATABASE RegistrationFormData;

USE RegistrationFormData;

CREATE TABLE NewRegister(
uName NVARCHAR(50) NOT NULL,
gender NVARCHAR(50) NOT NULL,
contact NVARCHAR(50) NOT NULL,
email NVARCHAR(50) NOT NULL PRIMARY KEY,
city NVARCHAR(50) NOT NULL
);

SELECT * FROM NewRegister;





Comments

Popular posts from this blog

Create a User Registration Form in ASP.NET using SQL Server, Visual Studio 2022 & Bootstrap

Create a Simple Login Form in ASP.NET using Visual Studio 2022

SqlCommand Class ADO.Net | ExecuteNonQuery | ExecuteReader | ExecuteScalar