关于dropdownlist的值用作数据库查询
我用VB语言编写了一个程序,在前台添加了一个dropdownlist控件(Dlst用户名),里面的值在Page_load时从数据库提取赋值,然后点击“登陆”按钮后,把dropdownlist中选定的值作为“用户名”条件输入,用户输入密码,然后判断是否通过验证。但是发现,我这个程序运行后点击“登陆”,VS就报错:“reader不存在该行/列”(红字部分,我用来检测哪里出问题的代码)。如果去除where后面的“ 用户名='" & Me.Dlst用户名.Text & "' ”这个条件,程序就可以正常取得reader的值了,请问这是怎么回事?Imports System.Data
Imports System.
Partial Public Class login
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Dlst用户名.Items.Clear()
'初始化下拉菜单
Initlst()
End Sub
Public Sub Initlst()
Dim engin As New MyDbEngine
Dim cmdText As String
cmdText = "select 用户名 from 用户信息表"
engin.OpenConnect()
Dim reader As Common.DbDataReader = engin.ExecuteReader(cmdText)
reader.Read()
If IsNothing(reader) Or reader.HasRows = False Then
Response.Write("没有用户信息!")
reader.Close()
engin.CloseConnect()
Exit Sub
End If
reader.Close()
reader = engin.ExecuteReader(cmdText)
While reader.Read
Me.Dlst用户名.Items.Add(reader("用户名").ToString)
End While
reader.Close()
engin.CloseConnect()
End Sub
Protected Sub btn登陆_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn登陆.Click
Dim engin As New MyDbEngine
Dim cmdText As String
cmdText = "select * from 用户信息表 where 密码='" & Me.txt密码.Text.Trim & "' and 用户名='" & Me.Dlst用户名.Text & "'"
engin.OpenConnect()
Dim reader As Common.DbDataReader = engin.ExecuteReader(cmdText)
reader.Read()
Response.Write(reader("用户名"))
If IsNothing(reader) Or reader.HasRows = False Then
Response.Write("用户名或密码错误!")
reader.Close()
engin.CloseConnect()
Exit Sub
End If
Response.Write("验证通过")
reader.Close()
engin.CloseConnect()
End Sub
End Class