Registration page using SqlParameter

In this blog we will know how to do a registration page using SqlParameter.

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Registration._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

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

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

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

    <div>

    <asp:Label ID="Label1" runat="server" Text="RegistrationNumber" Width='150'></asp:Label>

        <asp:TextBox ID="RegistrationText" runat="server"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

            ControlToValidate="RegistrationText" ErrorMessage="RequiredFieldValidator">RegistrationNumber

        Required</asp:RequiredFieldValidator>

        <br />

        <asp:Label ID="Label2" runat="server" Text="Make" Width='150'></asp:Label>

        <asp:TextBox ID="MakeText" runat="server"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"

            ControlToValidate="MakeText" ErrorMessage="RequiredFieldValidator">Make

        Required</asp:RequiredFieldValidator>

        <br />

        <asp:Label ID="Label3" runat="server" Text="Model" Width='150'></asp:Label>

        <asp:TextBox ID="ModelText" runat="server"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"

            ControlToValidate="ModelText" ErrorMessage="RequiredFieldValidator">Model

        Required</asp:RequiredFieldValidator>

        <br />

        <asp:Label ID="Label4" runat="server" Text="FuelTypes" Width='150'></asp:Label>

        <asp:TextBox ID="FuelTypeText" runat="server"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"

            ControlToValidate="FuelTypeText" ErrorMessage="RequiredFieldValidator">Fuel

        type Required</asp:RequiredFieldValidator>

        <br />

        <asp:Label ID="Label5" runat="server" Text="Capacity" Width='150'></asp:Label>

        <asp:TextBox ID="CapacityText" runat="server"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"

            ControlToValidate="CapacityText" ErrorMessage="RequiredFieldValidator">Capacity

        Required</asp:RequiredFieldValidator>

        <br />

        <asp:Label ID="Label6" runat="server" Text="Power" Width='150'></asp:Label>

        <asp:TextBox ID="BhpText" runat="server"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"

            ControlToValidate="BhpText" ErrorMessage="RequiredFieldValidator">Power

        Required</asp:RequiredFieldValidator>

        <br />

        <asp:Label ID="Label7" runat="server" Text="Mileage" Width='150'></asp:Label>

        <asp:TextBox ID="MileageText" runat="server"></asp:TextBox>

   

        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"

            ControlToValidate="MileageText" ErrorMessage="RequiredFieldValidator">Mileage

        Required</asp:RequiredFieldValidator>

   

    </div>

    <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" Text="Register" />

    </form>

</body>

</html>

 

 

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

namespace Registration

{

    public partial class _Default : System.Web.UI.Page

    {

        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlCommand com;

        string str;

 

        protected void btnadd_Click(object sender, EventArgs e)

        {

            ExecuteInsert(RegistrationText.Text, MakeText.Text, ModelText.Text, FuelTypeText.Text, CapacityText.Text, BhpText.Text, MileageText.Text);

            Response.Write("Record was successfully added!");

            clear();

 

        }

        private void ExecuteInsert(string RegistrationNumber, string Make, string Model, string FuelType, string Capacity, string Power, string Mileage)

        {

            SqlConnection con = new SqlConnection(connStr);

            str = "INSERT INTO Vehicles (RegistrationNumber, Make, Model, FuelType, Capacity, Power, Mileage) VALUES (@RegistrationNumber, @Make, @Model, @FuelType, @Capacity, @Power, @Mileage)";

            try

            {

                con.Open();

                com = new SqlCommand(str, con);

                SqlParameter[] param = new SqlParameter[7];

                param[0] = new SqlParameter("@RegistrationNumber", SqlDbType.NVarChar, 10);

                param[1] = new SqlParameter("@Make", SqlDbType.NVarChar, 30);

                param[2] = new SqlParameter("@Model", SqlDbType.NVarChar, 30);

                param[3] = new SqlParameter("@FuelType", SqlDbType.NVarChar, 10);

                param[4] = new SqlParameter("@Capacity", SqlDbType.Int, 4);

                param[5] = new SqlParameter("@Power", SqlDbType.Int, 4);

                param[6] = new SqlParameter("@Mileage", SqlDbType.Int, 8);

                param[0].Value = RegistrationNumber;

                param[1].Value = Make;

                param[2].Value = Model;

                param[3].Value = FuelType;

                param[4].Value = Capacity;

                param[5].Value = Power;

                param[6].Value = Mileage;

 

                for (int i = 0; i < param.Length; i++)

                {

                    com.Parameters.Add(param[i]);

                }

 

                com.CommandType = CommandType.Text;

                com.ExecuteNonQuery();

 

            }

 

            catch (System.Data.SqlClient.SqlException ex)

            {

 

                string msg = "Insert Error:";

                msg += ex.Message;

                throw new Exception(msg);

            }

 

            finally

            {

                con.Close();

            }

        }

        void clear()

        {

            RegistrationText.Text = "";

            MakeText.Text = "";

            ModelText.Text = "";

            FuelTypeText.Text = "";

            CapacityText.Text = "";

            BhpText.Text = "";

            MileageText.Text = "";

        }

    }

}

 

 

Thanks for reading