Tuesday, October 21, 2014

Simple Registration Form in vb.net ASP.Net using stored procedure

Posted by Gnani Palepu  |  No comments

 Simple Registration Form in vb.net ASP.Net using stored procedure  
 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#   
 here you can use import.data.sqlclient  
 Protected Sub RegisterUser(sender As Object, e As EventArgs)  
   Dim userId As Integer = 0  
   Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString  
   Using con As New SqlConnection(constr)  
     Using cmd As New SqlCommand("Insert_User")  
       Using sda As 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()  
       End Using  
     End Using  
     Dim message As String = String.Empty  
     Select Case userId  
       Case -1  
         message = "Username already exists.\nPlease choose a different username."  
         Exit Select  
       Case -2  
         message = "Supplied email address has already been used."  
         Exit Select  
       Case Else  
         message = "Registration successful.\nUser Id: " + userId.ToString()  
         Exit Select  
     End Select  
     ClientScript.RegisterStartupScript([GetType](), "alert", (Convert.ToString("alert('") &   
 message) + "');", True)  
   End Using  
 End Sub  
 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  

5:36 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