2012-02-01 13 views
1

フィールドが空白であるかどうかを確認した後に問題が発生します。私はプログラムを停止したいと思っています。今は進んでいて、フィールドが空白であっても、間違ったユーザー名パスワードを印刷私は、関数から次に知識vb.netのテキストボックスでの複数の検証

Private Sub ButtonOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOk.Click 
    Try 
     Dim con As New SqlConnection("Initial Catalog=stock;Data source=.;integrated security=true") 
     Dim ds1 As New DataSet 
     Dim da1 As New SqlDataAdapter("select * from login where Name='" & Trim(txtusername.Text) & "'and password='" & Trim(txtpassword.Text) & "'", con) 


     If txtpassword.Text.Length = 0 Then 
      MsgBox("Password or username feild left blank") 



     End If 


     If da1.Fill(ds1) Then 
      adminmain.Show() 
      Me.Close() 
     Else 

      MsgBox("Invalid Password or Username") 

     End If 

    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

End Sub 
+0

これはウェブアプリですか、デスクトップアプリですか? ASP.Net? WinForms? WPF? – DOK

+0

@ user1182892:StackOverflowへようこそ!最も役立つ答えを受け入れる必要があります(その隣のチェックマークをクリックしてください)。また、あなたを助けた他の回答をupvoteすることもできます。 –

答えて

0

の欠如Returnを言い訳してください、この時は本当に新しいです。

If txtpassword.Text.Length = 0 Then 
    MsgBox("Password or username feild left blank") 
    Return 
End If 

しかし、それは仕事だろうがTry/Catchの外

finally-blockは、return文の前に実行されます)。実際にTextBoxの妥当性検査は Try/Catchの内部にある必要はありませんので、試してみると、Try/Catch内から返すのは混乱します。

+0

omgそれはそれほど簡単でした:Oと私はそれをキャッチしようと外に移動:)ありがとう – Ketan

0

妥当性検査をSubの上部に置き、エラーが見つかるとすぐにExit Subを呼び出してください。

Private Sub ButtonOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOk.Click 
    If txtpassword.Text.Length = 0 Then 
     MsgBox("Password or username feild left blank") 
     Exit Sub 
    End If 
    'Other validations here 
    'Code to save changes here 
End Sub 

また、Try..Catch節を削除する必要があります。予期しないエラーが発生した場合は、エラーの行番号とスタックトレースを知りたいでしょう。

+0

ありがとう。 – Ketan

関連する問題