简体   繁体   中英

Visual Basic Database login - code will not work

I already have the username and password for a user in a database, I want to create a log in from two text boxes to connect to the database, here is what I have so far but it will not work

Partial Class Pages_Login


Inherits System.Web.UI.Page
Protected Sub btnlogin_Click(sender As Object, e As EventArgs) Handles   btnlogin.Click

Dim patientNo As String
Dim password As String
Dim bAuthethicated As Boolean
patientNo = txtuser.Text
password = txtpassword.Text
bAuthethicated = CheckUser(patientNo, password)

If bAuthethicated Then
    lblresult.Text() = "Student Number and Password are correct"

Else
    lblresult.Text() = "Incorrect Student Number and/or Password"


End If

End Sub

Dim cmdstring As String = "SELECT COUNT(*) FROM Patient  Where      Username=@STUDNO AND Password =@PASSWORD"

   conn As New SqlConnection("Data Source=.\SQLEXPRESS;
   AttachDbFilename=|DataDirectory|\surgery.mdf;IntegratedSecurity=True; UserInstance=True")

cmd= New SqlCommand(cmdstring, conn)
cmd.Parameters.Add("@PATIENTNO", SqlDbType.nchar).Value = patientNo
cmd.Parameters.Add("@PASSWORD", SqlDbType.nchar).Value = password
conn.Open()

found = CInt(cmd.ExecuteScalar)

End Using
Return (found > 0)
End Function

建造错误

A few build errors have appeared

Can canyone please help

Kind regards

There were one or two things wrong there.. I think this should get your code to compile without any errors

Partial Class Pages_Login
Inherits System.Web.UI.Page
Protected Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click

    Dim patientNo As String
    Dim password As String
    Dim bAuthethicated As Boolean
    patientNo = txtuser.Text
    password = txtpassword.Text
    bAuthethicated = CheckUser(patientNo, password)

    If bAuthethicated Then
        lblresult.Text() = "Student Number and Password are correct"

    Else
        lblresult.Text() = "Incorrect Student Number and/or Password"


    End If

End Sub
Public Function CheckUser(patientNo As String, password As String)
    Dim found As Boolean

    Dim cmdstring As String = "SELECT COUNT(*) FROM Patient  Where Username=@STUDNO AND Password =@PASSWORD"

    Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\surgery.mdf;IntegratedSecurity=True; UserInstance=True")

    Dim cmd = New SqlCommand(cmdstring, conn)
    cmd.Parameters.Add("@PATIENTNO", SqlDbType.NChar).Value = patientNo
    cmd.Parameters.Add("@PASSWORD", SqlDbType.NChar).Value = password
    conn.Open()

    found = CInt(cmd.ExecuteScalar)


    Return (found > 0)
End Function

End Class
Imports System.Data.SqlClient
Imports System.Data

Partial Class Pages_Login
    Inherits System.Web.UI.Page


    Protected Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click

        Dim patientNo As String
        Dim password As String
        Dim bAuthethicated As Boolean
        patientNo = txtuser.Text
        password = txtpassword.Text
        bAuthethicated = CheckUser(patientNo, password)

        If bAuthethicated Then
            lblresult.Text() = "Student Number and Password are correct"

        Else
            lblresult.Text() = "Incorrect Student Number and/or Password"


        End If

    End Sub

    Public Function CheckUser(patientNo As String, password As String) As Integer
        Dim cmdstring As String = "SELECT COUNT(*) FROM Patient  Where Username=@STUDNO AND Password =@PASSWORD"
        Dim found = 0
        Using conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\surgery.mdf;IntegratedSecurity=True;UserInstance=True")

            Dim cmd = New SqlCommand(cmdstring, conn)
            cmd.Parameters.Add("@PATIENTNO", SqlDbType.NChar).Value = patientNo
            cmd.Parameters.Add("@PASSWORD", SqlDbType.NChar).Value = password
            conn.Open()

            found = CInt(cmd.ExecuteScalar)

        End Using
        Return (found > 0)
    End Function
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM