我正在为我的项目制作登录页面但是我在运行时收到错误“位置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
答案 0 :(得分:1)
这条线没有意义:
If Not IsDBNull(ds) Then
ds永远不会是DBNull
。而是检查返回的行数,例如:
If ds.Tables(0).Rows.Length > 0 Then
当你没有任何时候,你试图获得第一行(.Rows(0)
) - 这就是错误告诉你的。
尝试使用以下内容:
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注入攻击持开放态度。)
答案 1 :(得分:0)
这条线没有意义:
If Not IsDBNull(ds) Then
有道理
If ds.Tables(0).Rows.count > 0 andalso ds.Tables..count > 0 Then
END IF
希望这有帮助
答案 2 :(得分: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。你的意思是用户名?你的查询是否正确?