Tuesday, October 21, 2014

Simple Registration Form in c# ASP.Net using stored procedure

Posted by Unknown  |  No comments


 if you want more related search please click here  
 please help to other  
 http://scriptquery.blogspot.in/  

 Simple Registration Form in ASP.Net using jquery  
 just fallow these three steps simple  
 1)step1) HTML  
 -------------  
 <table border="0" cellpadding="0" cellspacing="0">  
   <tr>  
     <th colspan="3">  
       Registration  
     </th>  
   </tr>  
   <tr>  
     <td>  
       Username  
     </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" runat="server" TextMode="Password" />  
     </td>  
     <td>  
       <asp:CompareValidator ErrorMessage="Passwords do not match." ForeColor="Red" ControlToCompare="txtPassword"  
         ControlToValidate="txtConfirmPassword" runat="server" />  
     </td>  
   </tr>  
   <tr>  
     <td>  
       Email  
     </td>  
     <td>  
       <asp:TextBox ID="txtEmail" runat="server" />  
     </td>  
     <td>  
       <asp:RequiredFieldValidator ErrorMessage="Required" Display="Dynamic" ForeColor="Red"  
         ControlToValidate="txtEmail" runat="server" />  
       <asp:RegularExpressionValidator runat="server" Display="Dynamic" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"  
         ControlToValidate="txtEmail" ForeColor="Red" ErrorMessage="Invalid email address." />  
     </td>  
   </tr>  
   <tr>  
     <td>  
     </td>  
     <td>  
       <asp:Button Text="Submit" runat="server" OnClick="RegisterUser" />  
     </td>  
     <td>  
     </td>  
   </tr>  
 </table> //end of the html design  
 stored procedure for this  
 step 2  
 --------  
 here i am explaining in c#   
 using System.Data;  
 using System.Configuration;  
 using System.Data.SqlClient;  
 protected void RegisterUser(object sender, EventArgs e)  
 {  
   int userId = 0;  
   string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
   using (SqlConnection con = new SqlConnection(constr))  
   {  
     using (SqlCommand cmd = new SqlCommand("Insert_User"))  
     {  
       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.Connection = con;  
         con.Open();  
         userId = Convert.ToInt32(cmd.ExecuteScalar());  
         con.Close();  
       }  
     }  
     string message = string.Empty;  
     switch (userId)  
     {  
       case -1:  
         message = "Username already exists.\\nPlease choose a different username.";  
         break;  
       case -2:  
         message = "Supplied email address has already been used.";  
         break;  
       default:  
         message = "Registration successful.\\nUser Id: " + userId.ToString();  
         break;  
     }  
     ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);  
   }  
 }  
 step last is stored procedure  
 ------------------------------:  
 CREATE PROCEDURE [dbo].[Insert_User]  
    @Username NVARCHAR(20),  
    @Password NVARCHAR(20),  
    @Email NVARCHAR(30)  
 AS  
 BEGIN  
    SET NOCOUNT ON;  
    IF EXISTS(SELECT UserId FROM Users WHERE Username = @Username)  
    BEGIN  
       SELECT -1 -- Username exists.  
    END  
    ELSE IF EXISTS(SELECT UserId FROM Users WHERE Email = @Email)  
    BEGIN  
       SELECT -2 -- Email exists.  
    END  
    ELSE  
    BEGIN  
       INSERT INTO [Users]  
            ([Username]  
            ,[Password]  
            ,[Email]  
            ,[CreatedDate])  
       VALUES  
            (@Username  
            ,@Password  
            ,@Email  
            ,GETDATE())  
       SELECT SCOPE_IDENTITY() -- UserId           
    END  
 END  

 if you want more related search please click here  
 please help to other  
 http://scriptquery.blogspot.in/  


5:32 AM Share:

0 comments:

Get updates in your email box
Complete the form below, and we'll send you the best coupons.

Deliver via FeedBurner
Proudly Powered by Blogger.
back to top