Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
104 views

Registration Form

This tutorial discusses creating a registration form in ASP.NET using VB.NET. It involves three main steps: 1) designing the user interface with text boxes and a button, 2) preparing the database and defining a registration table, and 3) connecting the registration page to the database. The code provided checks for empty fields, defines a connection string, inserts the form data into the registration table using SQL queries, and redirects on completion.

Uploaded by

Bilal Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Registration Form

This tutorial discusses creating a registration form in ASP.NET using VB.NET. It involves three main steps: 1) designing the user interface with text boxes and a button, 2) preparing the database and defining a registration table, and 3) connecting the registration page to the database. The code provided checks for empty fields, defines a connection string, inserts the form data into the registration table using SQL queries, and redirects on completion.

Uploaded by

Bilal Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASP.NET Tutorial using VB.

NET

Creating Registration from Using ASP.NET


In this tutorial we will be creating registration form for the website. Before starting the tutorial let us discuss the tools and specification which we will be using in this tutorial. We will be using .NET framework 4.0 along with VB.NET. For the databases we will be using MYSQL / WAMP Server. Our tutorial will be followed in the following steps: 1. First we will prepare the interface and put the text fields and button. These text fields will help us in entering the data into the database 2. Then we will prepare the database and define the table accordingly 3. Connect the registration page with the database

Step 1:
Make sure you are in design mode. In order to make sure look the bottom of your Visual Studio IDE, there will be Design, Split and Source Mode. Click Design mode in order to see the graphical mode so that you can drag and drop the items from the tool bar. Now go to HTML in the toolbar and double click on table. The table will appear in the design and the code as well. Add total of six rows to the table. Now go to the Standard in the toolbar again and drag and drop five textboxes and one button. After dragging the text boxes and button, the design will look something like image given below:

We have to put total six text boxes and a button on the web page the names and the ID information are given below: Textbox Information

Authour: Ch. Bilal Ahmad Khan

Page 1

ASP.NET Tutorial using VB.NET


S.No Heading 1 First Name 2 Last Name 3 Email 4 User Name 5 Password Button Information S.No Button ID 1 btnregister Textbox ID txtfirstname txtlastname txtemail txtusername txtpassword Button Name Register

The code generated should look like the ASP.NET code is given below. If you do not want to drag and drop the textboxes and button, simply copy paste the code given below and this should do the similar job. But make sure that the properties of all the textboxes and the button are set accordingly.
<%@ Page Title="" Language="VB" MasterPageFile="~/Layout.master" AutoEventWireup="false" CodeFile="register.aspx.vb" Inherits="register" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <style type="text/css"> .style1 { width: 431px; } .style2 { width: 149px; } .style3 { width: 149px; font-weight: bold; } .style7 { width: 1263px; height: 21px; } .style8 { width: 1263px; height: 24px; } .style13 { width: 1263px; height: 503px; } </style> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> </asp:Content>

Authour: Ch. Bilal Ahmad Khan

Page 2

ASP.NET Tutorial using VB.NET


<asp:Content ID="Content4" runat="server" contentplaceholderid="ContentPlaceHolder3"> <table style="width:100%;"> <tr> <td class="style7"> &nbsp;</td> <td class="style2"> &nbsp;</td> <td> &nbsp;</td> </tr> <tr> <td class="style8"> &nbsp;</td> <td class="style3"> First Name</td> <td> <asp:TextBox ID="txtfirstname" runat="server" Width="194px"></asp:TextBox> </td> </tr> <tr> <td class="style8"> &nbsp;</td> <td class="style3"> Last Name</td> <td> <asp:TextBox ID="txtlastname" runat="server" Width="194px"></asp:TextBox> </td> </tr> <tr> <td class="style8"> &nbsp;</td> <td class="style3"> Email</td> <td> <asp:TextBox ID="txtemail" runat="server" Width="194px"></asp:TextBox> </td> </tr> <tr> <td class="style8"> &nbsp;</td> <td class="style3"> User Name</td> <td> <asp:TextBox ID="txtusername" runat="server" Width="194px"></asp:TextBox> </td> </tr> <tr> <td class="style8"> &nbsp;</td> <td class="style3"> Password</td> <td> <asp:TextBox ID="txtpassword" runat="server" Width="194px" TextMode="Password"></asp:TextBox> </td>

Authour: Ch. Bilal Ahmad Khan

Page 3

ASP.NET Tutorial using VB.NET


</tr> <tr> <td class="style8"> &nbsp;</td> <td class="style3"> &nbsp;</td> <td> <asp:Button ID="btnregister" runat="server" Text="Register" Width="87px" /> </td> </tr> </table> </asp:Content>

Step 2:
Now it is time to set the database. Download and install the WAMP server. WAMP Server has MYSQL in it. If you do not want to download the WAMP Server you can also download MYSQL Workbench. In order to link the database with ASP.NET using Visual Basic, you need to download the MYSQL Connector for .NET and install it. It is necessary if you are installing either WAMP Server or MYSQL Workbench. Once the database has been installed. Make database in the MYSQL Workbench or WAMP Server. In this example we are using MYSQL in WAMP Server Once the database is made, make a table named as registration. Name the fields of the database as under:

Step 3:
Once everything is installed, perform the following steps in order to import the MYSQL your VB code. Go to Website in the file menu and select Add Reference

Authour: Ch. Bilal Ahmad Khan

Page 4

ASP.NET Tutorial using VB.NET

This will open up the window, press the .NET tab and select MYSQL Data in the list given below and then click OK

Authour: Ch. Bilal Ahmad Khan

Page 5

ASP.NET Tutorial using VB.NET


Now double click the Register button that you had created and at the top write the following code:
Imports MySql.Data.MySqlClient

If you have successfully imported MYSQL in Visual Studio, Visual Studio should itself give you the intellisence hints. Go inside the method Protected Sub btnregister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnregister.Click and write the first piece of code First we need to make sure that no textbox should be left empty by the user registering with our website. So write the code given below:
If txtfirstname.Text = "" And txtlastname.Text = "" And txtemail.Text = "" And txtusername.Text = "" And txtpassword.Text = "" Then Response.Redirect("register_fail.aspx") End If

According to the code given above if any of the textboxes are left empty the code will redirect the page to the to another page named as register_fail.aspx Next you need to define the variable which can handle the connection string. The connection string will be responsible for the connection determination between the code and the database whenever the code runs. So define the connections string and its variable. The code is given below:
Dim sqlconnection As New MySqlConnection sqlconnection.ConnectionString = "server=localhost; database=database_name; user id=root; password=write_your_password_for_database;"

Once the connection with the database has been established, define the command variable. This variable will eventually insert the data using SQL INSERT command into database. Use the code below to define the command variable below:
Dim sqlcommand As New MySqlCommand

Now open the connection by writing:


sqlconnection.Open()

Now tell the command variable that which connection will be using. Remember we had created the connections string above and a connection variable as well. When we tell which connection will be using this means that the command variable will automatically see the database, check the password, connect to the database and insert all the information into the respective table. In order to tell which connection should the command variable use write the following code:
sqlcommand.Connection = sqlconnection

Authour: Ch. Bilal Ahmad Khan

Page 6

ASP.NET Tutorial using VB.NET


Use the INSERT command now and insert all the data entered in the textboxes. We can translate the code given below as, INSERT the data into the table named registration where the fields/ columns in the table registration are firstname, lastname, email, username, password. Next the VALUES which are to be entered into these fields/ columns are " txtfirstname.Text ", txt.lastname.Text, txtemail.Text, txtusername.Text, txtpassword. So this means that anything which is written in the text boxes is now inserted into the database.
sqlcommand.CommandText = "INSERT INTO registeration (firstname,lastname,email,username,password,catagory) VALUES ('" & txtfirstname.Text & "','" & txtlastname.Text & "','" & txtemail.Text & "','" & txtusername.Text & "','" & txtpassword.Text & "', '" & drpselectlogintype.Text & "')"

Now execute the query given in the previous step by writing the code:

sqlcommand.ExecuteNonQuery()

And close the connection once the data has been inserted by writing

sqlconnection.Close()

If you also want that the page should be redirected after instering the data write down the redirect code as below:
Response.Redirect(Default.aspx)

If you want to see the complete VB.NET code, please select copy and paste it and hopefully it will work.

Imports MySql.Data.MySqlClient Partial Class register Inherits System.Web.UI.Page Protected Sub btnregister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnregister.Click If txtfirstname.Text = "" And txtlastname.Text = "" And txtemail.Text = "" And txtusername.Text = "" And txtpassword.Text = "" Then Response.Redirect("register_fail.aspx") End If Dim sqlconnection As New MySqlConnection sqlconnection.ConnectionString = "server=localhost; database=database_name; user id=root; password=write_your_password_for_database;"

Authour: Ch. Bilal Ahmad Khan

Page 7

ASP.NET Tutorial using VB.NET

Dim sqlcommand As New MySqlCommand sqlconnection.Open() sqlcommand.Connection = sqlconnection sqlcommand.CommandText = "INSERT INTO registration (firstname,lastname,email,username,password) VALUES ('" & txtfirstname.Text & "','" & txtlastname.Text & "','" & txtemail.Text & "','" & txtusername.Text & "','" & txtpassword.Text & "')" sqlcommand.ExecuteNonQuery() sqlconnection.Close() Response.Redirect("Default.aspx") End Sub End Class

Authour: Ch. Bilal Ahmad Khan

Page 8

You might also like