create User Registration Form in ASP.NET using SQL Server Database

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


<!DOCTYPE html>


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

<head runat="server">

    <title></title>

</head>

<body>

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

        <div>

            <table align="center">

                <tr>

                    <td>Full Name</td>

                    <td>

                        <asp:TextBox ID="txtName" runat="server" placeholder="Enter your full name"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>Address</td>

                    <td>

                        <asp:TextBox ID="txtAdd" runat="server" placeholder="Enter your address"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>Gender</td>

                    <td>

                        <asp:RadioButton GroupName="user" ID="RadioButton1" runat="server" Text="Male" OnCheckedChanged="RadioButton1_CheckedChanged"/><br/>

                        <asp:RadioButton GroupName="user" ID="RadioButton2" runat="server" Text="Female" OnCheckedChanged="RadioButton2_CheckedChanged"/><br/>

                        <asp:RadioButton GroupName="user" ID="RadioButton3" runat="server" Text="others" OnCheckedChanged="RadioButton3_CheckedChanged"/>

                    </td>

                </tr>

                <tr>

                    <td>Phone</td>

                    <td>

                        <asp:TextBox ID="txtPhone" runat="server" placeholder="+977-012-345-6789"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>email</td>

                    <td>

                        <asp:TextBox ID="txtEmail" runat="server" placeholder="example@example.com" TextMode="Email"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>username</td>

                    <td>

                        <asp:TextBox ID="txtUser" runat="server" placeholder="Enter username"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>password</td>

                    <td>

                        <asp:TextBox ID="txtPass" runat="server" placeholder="*****" TextMode="Password"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td></td>

                    <td align="right">

                        <asp:Button ID="btnReg" runat="server" Text="Register" OnClick="btnReg_Click"/>

                    </td>

                </tr>

            </table>

        </div>

    </form>

</body>

</html>

ABOVE FILE IS WebForm1.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 userRegistrationForm
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
        string gender;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void EmptyControls()
        {
            txtName.Text = "";
            txtAdd.Text = "";
            //RadioButton1.TabIndex = -1;
            //RadioButton2.TabIndex = -1;
            //RadioButton3.TabIndex = -1;
            txtPhone.Text = "";
            txtEmail.Text = "";
            txtUser.Text = "";
            txtPass.Text = "";
        }

        protected void btnReg_Click(object sender, EventArgs e)
        {
            try
            {
                using(SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
                    string query = "INSERT INTO userreg(name,address,gender,phone,email,username,password) VALUES(@name,@address,@gender,@phone,@email,@username,@password)";
                    SqlCommand cmd = new SqlCommand(query,con);
                    cmd.Parameters.AddWithValue("@name",txtName.Text);
                    cmd.Parameters.AddWithValue("@address",txtAdd.Text);
                    cmd.Parameters.AddWithValue("@gender",gender);
                    cmd.Parameters.AddWithValue("@phone",txtPhone.Text);
                    cmd.Parameters.AddWithValue("@email",txtEmail.Text);
                    cmd.Parameters.AddWithValue("@username",txtUser.Text);
                    cmd.Parameters.AddWithValue("@password",txtPass.Text);
                    cmd.ExecuteNonQuery();
                    Response.Write("<script>alert('user is registered successfully')</script>");
                    EmptyControls();
                }
            }
            catch (SqlException ex)
            {
                Response.Write("SqlException: " + ex.Message);
            }
            catch (Exception ex)
            {
                Response.Write("Exception: " + ex.Message);
            }
        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            gender = "male";
        }

        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            gender = "female";
        }

        protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
        {
            gender = "others";
        }
    }
}
ABOVE FILE IS WebForm1.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=userreg;Integrated Security=True;TrustServerCertificate=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
ABOVE FILE IS Web.config

















CREATE DATABASE userreg;

USE userreg;

CREATE TABLE userreg(
name NVARCHAR(50) NOT NULL,
address NVARCHAR(50) NOT NULL,
gender CHAR(10) NOT NULL,
phone NVARCHAR(50) NOT NULL,
email NVARCHAR(50) NOT NULL,
username NVARCHAR(50) NOT NULL,
password NVARCHAR(50) NOT NULL
);

SELECT * FROM userreg;



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