Hello,

I want to check the username & password of the user with the already stored database and then login. Iam using asp.net(vb) with sql server as database. Please help ME

Imports System.Data.SqlClient
Imports System.Data



Partial Class MasterPage
    Inherits System.Web.UI.MasterPage
    Dim con As New SqlConnection("server=localhost;database=reg1;integrated security=yes;")
    
    Dim ds As dataset
    Dim da As SqlDataAdapter
    Dim dr As SqlDataReader


    Dim MyDataGrid As New DataGrid()


    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        con.Open()
        Session.Add("Username", TextBox1.Text)
        Session.Add("Password", TextBox2.Text)

        Response.Redirect("default.aspx")

        'Dim a, b As String

        'a = TextBox1.Text
        'b = TextBox2.Text
        If a = "&Username" And b = "&Password" Then



            da = New SqlDataAdapter("select * from retable", con)
            ds = New DataSet
            da.Fill(ds, "retable")
            MyDataGrid.DataSource = ds.Tables("retable").DefaultView
            MyDataGrid.DataBind()


        Else
            Label3.Text = "pls enter correct pwd"
        End If

        con.Close()

by following this code i get logged in but it didn't check with database

Recommended Answers

All 7 Replies

this will help you for lgoin form. on button click write this code

con = New SqlConnection(cons)
        con.Open()
        str = "Select [UserName], [Password] from [UserLogin] "
        cmd = New SqlCommand(str, con)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader()
If dr.HasRows = True Then
            While (dr.Read())
                If dr(1).ToString() = Trim(txtuname.Text) And dr(2).ToString() = Trim(txtpass.Value) Then
Session.Add("Username", TextBox1.Text)     
Session.Add("Password", TextBox2.Text)
Exit While
                End If
            End While
            Response.Redirect("Home.aspx")
            Exit Sub

        ElseIf dr.HasRows = False Then
            MsgBox("Invalid User Name OR Passward")
            cmd = Nothing
            con.Close()
            Response.Redirect("MHome.aspx")
       End If

if this will solve your ans then mark your thread as solved. :)

I believe we don't need to fetch all the rows from UserLogin table and then check the username and passworkd user has entered. Instead we pass the entered username and password in Where clause and save the execution time. and always at the end of operation close datareader.

Hello pritesh
Thanks for your help. i get the error message "The ConnectionString property has not been initialized." while execute the code you have given

if you got your solution mark your thread as solved

Hello
I didn't get the result. While executing i got the error message as "The ConnectionString property has not been initialized." Please help me

Hello
I didn't get the result. While executing i got the error message as "The ConnectionString property has not been initialized." Please help me

write over here your connection string
where i had written
con=new SQlConnection(cons)

con=new Sqlconnection("Data Source=ABC;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=1234")

for your database or use this Link for detail of connectionstring

check for the right connection path :)

i used to have that problem before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.