Saturday, 8 October 2016

2. Login and Database code in VB.NET (VBDOTNET)

1. Default.aspx (First Page created by IDE)



Here in this figure not the name of the file (Default.aspx) and Source and Design Items.
Currently the ' Source' tab is enabled.

See the effect of 'Design Tab'


2. Now We are going to include 'Login' tool here.

Here see the 'Tool Box' option in the left side
and Select 'Login' option and see the 'Login tool' . Drag this tool to your design



See the Dragged 'Login Tool'. So we can save a lot of time typing and designing the Login Box which contains username and password options.


See the line 'Type the Code here' .
At that place we should write the code.

The new class will look like [Code to be copied are highlighted in Blue for convenience]

Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Public constr As String
    Public ds As New DataSet()
    Public da As SqlClient.SqlDataAdapter
    Public mySqlConn1 As New SqlClient.SqlConnection(constr)
    Public sql, sql1, code, asd, type, cartno As String

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
        'Type the code here.
   
        Dim conn As New SqlConnection
        Dim constr As String
        constr = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\AUM HUP C#\HUPEGov\App_Data\Database.mdf;Integrated Security=true;User Instance=true"

        conn.ConnectionString = constr
        conn.Open()
        sql = "select * from HUPTable where HUPName='" & Login1.UserName.ToString & "' and HUPPass='" & Login1.Password.ToString & "'"

        Dim comm As New SqlCommand(sql, conn)



        Dim dr As SqlDataReader
        dr = comm.ExecuteReader
        Dim a
        If dr.Read() = True Then
            a = 1
        Else
            a = 0
        End If
        dr.Close()
        If a = 1 Then
            Session("Userid") = Login1.UserName.ToString
            'sql = "delete cart where uname='" & Session("Userid") & "'"
            'Dim cmd As New SqlCommand(sql, conn)
            'cmd.ExecuteNonQuery()
            Response.Redirect("Main.aspx")
        Else
            MsgBox("Enter Valid User Name and Password!")

        End If
        conn.Close()
    End Sub
End Class


NB: The path of Database will change according to your settings.

Also Add a new Form 'Main.aspx'



3. Creation of Database 

Follow the given options



See the Database Tab in the given figure below


Here Right click the Table and select 'Add New Table'


Give a suitable Name to the Table


As the next step we are going to add a record in the table.
So go to the 'Database Tab' and Expand the Table Option and Select the HUPTable
Right Click the Table and select 'Show Table Data' and type the username as 'HUP' and password as 'HUPA' and save.


No comments:

Post a Comment