Registration Form
Registration Form
NET
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
Page 1
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>
Page 2
Page 3
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
Page 4
This will open up the window, press the .NET tab and select MYSQL Data in the list given below and then click OK
Page 5
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 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
Page 6
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;"
Page 7
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
Page 8