2011-10-21 43 views
0

我在VB.NET中實現了用戶驗證的代碼。當我在表單的文本框中輸入用戶名和密碼並單擊提交按鈕時,即使我已爲其編寫代碼,也不會顯示任何消息框。 try-catch塊中是否存在一些問題,或者我缺少一些代碼行?VB.NET中的用戶驗證

有人可以指出這段代碼有什麼問題嗎?

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    If TextBox1.Text = "" Or TextBox2.Text = " " Then 
     MsgBox("Enter a user id and password") 
    End If 
    TextBox1.Text = userid 
    TextBox2.Text = password 

    Try 
     myconnection = New SqlConnection("server=PARTH-PC\SQLEXPRESS;uid=sa;pwd=demo;database=fc") 
     'you need to provide password for sql server 
     myconnection.Open() 

     mycommand = New SqlCommand("select * from student where user id='" & TextBox1.Text & "' and password='" & TextBox2.Text & "')", myconnection) 
     dr = mycommand.ExecuteReader() 

    Catch ex As Exception 
    Finally 



     If (dr IsNot Nothing) Then 

      If (dr.Read()) Then 

       MsgBox("User is authenticated") 
       Form2.Show() 



      Else 
       MsgBox("Please enter correct username and password") 
      End If 


     End If 

    End Try 

    myconnection.Close() 
     End Sub 
    End Class 
+0

您是否收到任何錯誤?您的Form2.Show()是否按預期工作? – Tariqulazam

+0

@Tariqulazam:沒有控制是不會在那如果阻止..i dnt knw爲什麼 –

回答

1

使用Trim()Length方法或String.IsNullOrWhiteSpace()(.NET框架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][email protected] and [password][email protected]",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 
+0

是的,我需要做一些嚴重的修改後,我的代碼看到你的代碼..謝謝很多幫助! :-) –

1

我的猜測是,您沒有Option Strict On,並且正在您的Try/Catch塊的Try部分中創建dr。當你到達Finally欄目時,它超出了範圍。您也在吞嚥您的catch塊中沒有throw語句的任何錯誤。

嘗試:

Dim myconnection as SqlConnection 
Dim mycommand as SqlCommand 
Dim dr as SqlDataReader 
Try 
    myconnection = New SqlConnection("server=PARTH-PC\SQLEXPRESS;uid=sa;pwd=demo;database=fc") 
     'you need to provide password for sql server 
    myconnection.Open() 

    mycommand = New SqlCommand("select * from student where user id='" & TextBox1.Text & "' and password='" & TextBox2.Text & "')", myconnection) 
    dr = mycommand.ExecuteReader() 

Catch ex As Exception 
    Throw 
Finally 
    If (dr IsNot Nothing) Then 
     If (dr.Read()) Then 
      MsgBox("User is authenticated") 
      Form2.Show() 
     Else 
      MsgBox("Please enter correct username and password") 
     End If 
    End If 

End Try 

myconnection.Close() 

編輯:傑夫·阿特伍德的Option StrictOption Explicit聲明

http://www.readmespot.com/question/o/222370/option-strict-on-and--net-for-vb6-programmers

和編碼恐怖article其他鏈接

+0

所以我應該寫dr = mycommand.ExecuteReader()後最後? –

+0

您需要在進入Try塊之前創建它。你現在在哪裏聲明myconnection,mycommand和dr? –

+0

非常感謝所有的幫助! :) –

1

這樣的:

TextBox1.Text = userid 
TextBox2.Text = password 

看起來不對。除此之外,你可能沒有得到讀者的任何記錄(因爲這條線)..這就是爲什麼你沒有得到任何結果。無論如何,在finally塊上使用它是浪費開銷。

而且你的SQL是錯誤的,它有一個)比需要

+0

好吧,我應該刪除這些行:TextBox1.Text = userid TextBox2.Text = password? –

+0

是的,這是什麼porpuose?該變量是空的(可能需要選項嚴格和顯式打開)。另外你的sql是錯誤的,看看它... – gbianchi

+0

好的罰款亞,SQL查詢需要修復..我有一個額外的')'括號..我會解決這個問題..也是什麼意思是由選項嚴格和顯式打開 ?我第一次遇到這個問題..你可以點亮一下嗎? –