.Net Programming Practices

UserRegistration
connection String
	<connectionStrings>
		<add name="conn" connectionString="Data Source=desktop-sg2aj84;Initial Catalog=StudentExam;User ID=sa;Password=admin@12" providerName="System.Data.SqlClient" />
	</connectionStrings>

Page Design
<form id="form1" runat="server">
     <div>
         <table border="0" style="background-colorburlywood">
             <tr>
                 <th colspan="3">Registration
                 </th>
             </tr>
             <tr>
                 <td>User Name
                 </td>
                 <td>
                     <asp:TextBox ID="txtUserName" runat="server" />
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red" ControlToValidate="txtUserName" runat="server" />
                 </td>
             </tr>
             <tr>
                 <td>Password
                 </td>
                 <td>
                     <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red" ControlToValidate="txtPassword" runat="server" />
                 </td>
             </tr>
             <tr>
                 <td>Confirm Password
                 </td>
                 <td>
                     <asp:TextBox ID="txtConfirmPassword" TextMode="Password" runat="server" />
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red" runat="server" ControlToValidate="txtConfirmPassword" />
                     <asp:CompareValidator ErrorMessage="Password Do not Match." ForeColor="Red" ControlToCompare="txtPassword" runat="server" ControlToValidate="txtConfirmPassword"/>
                 </td>
             </tr>
             <tr>
                 <td>Email
                 </td>
                 <td>
                     <asp:TextBox ID="txtEmail" runat="server" />
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red" runat="server" ControlToValidate="txtEmail" />
                     <asp:RegularExpressionValidator ForeColor="Red" ErrorMessage="Invalid Email" runat="server" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>
                 </td>
             </tr>
             <tr>
                 <td>
                    
                 </td>
                 <td>
                     <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
                     <asp:Button ID="btnDate" runat="server" Text="DateSave" OnClick="btnDate_Click"/>
 
                 </td>
                 <td>
                 </td>
             </tr>
             <tr>
                 <td>
                     <asp:Calendar ID="Calendar1" Visible="false" runat="server"  OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
                 </td>
                 <td>
                     <asp:TextBox ID="txtDate" TextMode="Date" runat="server"></asp:TextBox>
                 </td>
             </tr>
 
         </table>
         <asp:GridView ID="grdUserData" OnRowDataBound="grdUserData_RowDataBound" runat="server"/>
 
     </div>
 </form>

Submit

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int userid = 0;
    string con = ConfigurationManager.ConnectionStrings["conn"].ToString();
    using (SqlConnection constring = new SqlConnection(con))
    {
        using (SqlCommand cmd = new SqlCommand("userregistration"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim());
                cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
                cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                cmd.Parameters.AddWithValue("@Date", txtDate.Text.Trim());
                cmd.Connection = constring;
                constring.Open();
                userid = Convert.ToInt32(cmd.ExecuteScalar());
                constring.Close();
 
            }
        }
        string message = string.Empty;
        switch (userid)
        {
            case 1:
                message = "Username already exists.";
                break;
            case 2:
                message = "Email Id already exists";
                break;
            default:
                message = "Registration Successfull.user ID:" + userid.ToString();
                break;
 
        }
        Response.Write("<script LANGUAGE='JavaScript' >alert('" + message + "')</script>");
 
    }
 
}
procedure
USE [StudentExam]
GO
/****** Object:  StoredProcedure [dbo].[userregistration]    Script Date: 30-Sep-20 10:21:41 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[userregistration]

@UserName varchar(50),
@Password varchar(50),
@Email varchar(50),
@Date date
As

BEGIN
SET NOCOUNT ON ;
IF EXISTS (SELECT USERID FROM Users WHERE USERNAME=@UserName)
BEGIN 
SELECT 1;
END
ELSE IF EXISTS(SELECT USERID FROM USERS WHERE EMAIL=@Email)
BEGIN
SELECT 2;
END
ELSE
BEGIN
INSERT INTO Users (userName,Password,Email,createdDate,LastLoginDate) 
VALUES(@UserName,@Password,@Email,Getdate(),@Date) 
select SCOPE_IDENTITY()
END
END 
Date insertion By Query
public void DateUpdate()
   {      
       string con = ConfigurationManager.ConnectionStrings["conn"].ToString();
       DateTime date = Convert.ToDateTime(txtDate.Text);
       using (SqlConnection constring = new SqlConnection(con))
       {
           SqlCommand cmd = new SqlCommand("insert into dateData (dateOfReg,userName,email) values ('" + date +"','" + txtUserName.Text + "','" + txtEmail.Text + "')", constring);
           cmd.CommandType = CommandType.Text;
           cmd.Connection = constring;
           constring.Open();
           cmd.ExecuteNonQuery();
           constring.Close();
 
 
       }
 
   }
Grid Binding
public void Gridfill()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter sa = new SqlDataAdapter();
        string con = ConfigurationManager.ConnectionStrings["conn"].ToString();
        using (SqlConnection constring=new SqlConnection(con))
        {
            SqlCommand cmd = new SqlCommand("select * from dateData", constring);
            cmd.CommandType = CommandType.Text;
            sa =new SqlDataAdapter(cmd);
            sa.Fill(dt);
            grdUserData.DataSource = dt;
            grdUserData.DataBind();
 
        }
    }

status Row colour Change

protected void grdUserData_RowDataBound(object sender, GridViewRowEventArgs e)
     {
         if(e.Row.RowType==DataControlRowType.DataRow)
         {
             string username = e.Row.Cells[1].Text;
             if(username=="zzz")
             {
                 e.Row.BackColor = System.Drawing.Color.Red;
             }
         }
     }

No comments:

Post a Comment