2011-10-21 7 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 Frameworkの4)よりa)の多くを持っています。

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

私の推測では、あなたがオプション厳格にしていないということで、drはあなたのtry/catchブロックのトライ部分に作成されています。あなたが最後のセクションに行くとき、それは範囲外です。 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のクエリを修正する必要があります..私は余分な ')'ブレース..私はそれを修正します..また、オンにする ?私は初めてこれを見てきました。これでいくつかの光を投げられますか? –

関連する問題