2013-12-12 2 views
-1

내 프로젝트의 로그인 페이지를 만들고 있지만 실행 중 "위치 0에 행이 없습니다"라는 오류 메시지가 나타납니다. 나는이 코드 라인을 시도했다.빈 데이터 세트를 처리하는 방법은 무엇입니까?

Imports System.Data.SqlClient 
Imports System.Data 

Partial Class Dept_login 
    Inherits System.Web.UI.Page 

    Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click 
     Dim ds As New DataSet 
     'Try 
     Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("VMSConnectionString").ConnectionString) 
     con.Open() 
     Dim cmd As New SqlCommand("SELECT password FROM Dept_login WHERE user_id='" + Txtuname.Text + "'", con) 
     Dim da As New SqlDataAdapter(cmd) 
     da.Fill(ds) 

     If Not IsDBNull(ds) Then 
      If Txtpwd.Text = ds.Tables(0).Rows(0).Item("password") Then 
       Response.Redirect("Online Services.aspx") 'the page i want to redirect to after login successful 
      Else 
       Label1.Visible = True 'initially visible is false and text is INVALID PASSWORD 
      End If 

      con.Close() 
      ' Catch ex As Exception 

      ' End Try 
     End If 
    End Sub 

    Private Function Dept_login() As Integer 
     Throw New NotImplementedException 
    End Function 

End Class 
+1

더 나은 타이틀을 선택하십시오! –

+0

또한 MVC가 아닙니다. –

+0

plz 지금 도와주세요. 내일이 프로젝트를 선생님에게 보여줘야합니다. – user3030098

답변

1

이 줄은 이해가되지 않습니다 :

If Not IsDBNull(ds) Then 

DS가 DBNull이 될하지 않습니다. 대신, 행의 수처럼 돌아 오는 확인 : 당신이되지 않은 경우 첫 번째 행 (.Rows(0))를 얻기 위해 노력하고

If ds.Tables(0).Rows.Length > 0 Then 

- 그 오류이 알려주는거야.

같은 것을 사용해보십시오 :

If ds.Tables(0).Rows.Count > 0 AndAlso Txtpwd.Text = ds.Tables(0).Rows(0).Item("password") Then 
     Response.Redirect("Online Services.aspx", False) 'the page i want to redirect to after login successful 
     Context.ApplicationInstance.CompleteRequest(); 
    Else 
     Label1.Visible = True 'initially visible is false and text is INVALID PASSWORD 
    End If 

    con.Close() 
    ' Catch ex As Exception 

    ' End Try 

(참고 : 당신은 SQL 쿼리에 대한 매개 변수화를 사용해야합니다 당신은 SQL 주입 공격에 노출 자신을 떠난다..)

+0

제발 당신이 나를 간단하게 말해 coz 내가 asp.net에 매우 익숙한, 사실 그냥 그것을 배우는 나를 도울 수 있습니다. 친절하게 당신이 전화를 할 수 있습니다 내가 어디로 이러한 변경 사항을 넣어해야합니다. 당신을 감사합니다. – user3030098

+0

나는 내가 그랬다고 생각했다. 내 대답의 첫 번째 "if"문을 두 번째 대답으로 변경하십시오. –

+0

하지만 보여주는 길이는 this.hehe – user3030098

0

이 줄은 '아무튼 t 메이크 감각 :

If Not IsDBNull(ds) Then 

는 의미를

If ds.Tables(0).Rows.count > 0 andalso ds.Tables..count > 0 Then 

END IF 
만든다

희망이 있습니다

+0

당신의 코드를 잘 실행했지만, 그것이 성공해야만하는 일을하지 않을 때, 성공 로그인이나 유효하지 않은 패스워드가 나에게 걸릴 것입니다. msg.these는 msgs입니다. 내가 얻고있는 첫 번째 예외는 ' System.Threading.ThreadAbortException '이 (가) mscorlib.dll에서 발생했습니다. mscorlib에서'System.Threading.ThreadAbortException '유형의 예외가 발생했습니다.dll하지만 사용자 코드 – user3030098

+0

Response.Redirect ("Online Services.aspx", false)이 도움이 희망을 추가하십시오. –

+0

죄송하지만 작동하지 않습니다. asp.net 수있는 학습자 오전 수 없습니다. 너의 제안으로 바로 잡아라.하지만, – user3030098

0

데이터 어댑터 대신 datareader를 사용해 보셨습니까?

Try 
     Dim datare As SqlDataReader 
     Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("VMSConnectionString").ConnectionString) 
      Using cmd As New SqlCommand("SELECT password FROM Dept_login WHERE user_id='@User'", cn) 
       cmd.Parameters.AddWithValue("@User", Txtuname.Text) 
       cn.Open() 
       datare = cmd.ExecuteReader() 
       With datare 
        If .Read() Then 
         If .Item(0) = txtpwd.Text Then 
          Response.Redirect("Online Services.aspx") 
         Else 
          Label1.Visible = True 
         End If 
        End If 
       End With 
      End Using 
     End Using 
    Catch ex As Exception 
     Throw ex 
    End Try 

또한 쿼리에서 user_id를 작성합니다. 사용자 이름을 의미 했습니까? 당신은 정확한 쿼리입니까?

관련 문제