Creating a Login in ASP.NET using a MySQL table July 3, 2008
Posted by tuse in : ASP.NET, Databases , 2 commentsToday we will see how to create a simple login page for your ASP.NET website. Add a new WebForm to your Website. For readabilty, name this as login.aspx
Drag a Login control from the toolbox (Yes, ASP.NET comes with a built-in Login control)
This Login control is really smart and is able to do the validations itself (i.e. checking if username and password are not entered etc..)
What needs to be configured for this control is the manner in which the user authentication will occur.
We will authenticate a user using the data stored in a MySQL database in the ‘login’ table which holds the username and password (We created this table and so should you before trying this out).
Write the following code for the ‘Login1_Authenticate’ method. Double click on the Login control to goto the code window.
' Import the ODBC namespace for MySQL Connection
Imports System.Data.Odbc
Partial Class login
Inherits System.Web.UI.Page
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
Dim cn As New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=root;Password=;")
cn.Open()
Dim cmd As New OdbcCommand("Select * from login where username=? and password=?", cn)
'Add parameters to get the username and password
cmd.Parameters.Add("@username", OdbcType.VarChar)
cmd.Parameters("@username").Value = Me.Login1.UserName
cmd.Parameters.Add("@password", OdbcType.VarChar)
cmd.Parameters("@password").Value = Me.Login1.Password
Dim dr As OdbcDataReader
' Initialise a reader to read the rows from the login table.
' If row exists, the login is successful
dr = cmd.ExecuteReader
If dr.HasRows Then
e.Authenticated = True
' Event Authenticate is true
End If
End Sub
End Class
Now suppose that you have other webpages in your websites and you wish to grant access to these only if a user has authenticated himself. This essentially means that the login page should appear to any anonymous users trying to access the webpage. To do this, we need to change the web.config XML file associated with the website (It is a configuration file which is added by default to all ASP.NET websites). This file can be found from the Solution Explorer (Keyboard Shortcut - Ctrl+Alt+L).
Make the following changes to the authentication tag that already exists in the file to make it look like the following-
Add a authorization tag just after the authentication to make sure anonymous users are denied access to your webpages (Anonymous users are identified by the question mark)
Now when you try to request any page in your website, it shows the login.aspx to authenticate you before allowing you to view its contents.
Connecting to Databases in VB.NET June 29, 2008
Posted by prasu in : Databases, VB.NET , 6 commentsIn most of the practical applications made using VB.NET we need to access a database to retrieve or manipulate the data. To access the data stored in a database from VB.NET we need the following objects-
- Connection: The connection object tells VB.NET how to get the data (i.e the database and the driver to connect to it) and also the database user credentials. This initialization is made in the Connection String
- Container: We need a object to locally hold the data we retrieve from the database. This object is known as the DataSet
- Bridge:We need a bridge between the Connection and the DataSet. This object is known as the DataAdapter.
- Command: The SQL Command which determines the kind of data we are trying to retrieve from the database is given in the Command Object.
A Connection string has to be specified so that the program can connect to the required database.
The data is stored in the DataSet via DataAdapters. This is done through a method called Fill, which takes two arguments The DataSet object and the Table name
To bind the data present in the DataSet to a DataGridView (a control where the data will be echoed on the form), the DataSet and the tables are to be specified.
Now to code a simple program to access the data from a table to DataGridView
- Add a windows form.
- Drag a DataGridView control from the toolbox to the form.
- Double click on the form. you will go to the Form_Load method. Now type the code as given.
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'cn is the Connection Object with the Connection String to establish a database connection using the connection string in the constructor
Dim cn As New Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=me;Password=xxx;")
cn.Open() 'Open the connection in the form load event
Dim cmd As New Odbc.OdbcCommand 'Command Object
Dim adp As Odbc.OdbcDataAdapter 'Data Adapter
Dim ds As New DataSet 'Data Set
cmd.Connection = cn 'Associate the 'cn' connection with the Command Object
cmd.CommandText = "Select * from mytable" 'The SQL Command
adp = New Odbc.OdbcDataAdapter(cmd) 'Data Adapter- Associate it with the Command
adp.Fill(ds, "mytable") 'The fill method. This stores data from the DB into the dataset
Me.DataGridView1.DataSource = ds 'The source of data for the grid view control is the dataset
Me.DataGridView1.DataMember = "mytable" 'Specify data from which table is needed
cn.Close() 'Close the connection after data access is done
End Sub
End Class
As you can by now figure out from the Connection String (ps-read earlier posts), we have used a MySQL database connected to the VB.NET Application using the ODBC Connector.
This is how your output should look like-

For those of you looking to develop applications dealing with databases, VB.NET is the tool to create an elegant front-end.
