Используйте Trim()
и Length
методы или String.IsNullOrWhiteSpace()
(.net framework 4) для проверки пустой строки или строки нулевой длины.
If TextBox1.Text.Trim().Length = 0 Or TextBox2.Text.Trim().Length = 0 Then
MsgBox("Enter a user id and password")
Return 'Terminate this method
End If
Неправильное назначение здесь,
Dim userid=TextBox1.Text
Dim password=TextBox2.Text
Другая проблема заключается в использовании жестко закодированного оператора SQL.
myconnection = New SqlConnection("server=PARTH-PC\SQLEXPRESS;uid=sa;pwd=demo;database=fc")
mycommand = New SqlCommand("select * from student where [user id]=@userid and [password]=@password",myconnection)
mycommand.Parameters.Add("@userid",SqlDbType.VarChar,30).Value = userid
mycommand.Parameters.Add("@password",SqlDbType.VarChar,30).Value = password
myconnection.Open()
dr = mycommand.ExecuteReader()
Dim isFound as boolean = false
if dr.Read() Then
isFound=true
End If
dr.Close()
myConnection.Close()
if IsFound Then
MsgBox("User is authenticated")
Form2.Show()
Else
MsgBox("Please enter correct username and password")
End If