System.InvalidCastException: 从类型“OleDbDataReader”到类型“String”的转换无效。
源文件
If TextBox1.Text = "" Or TextBox2.Text = "" Then
Response.Write("<script>alert('用户名或密码不能为空!');history.back();</script>")
TextBox1.Focus()
Else
conn.Open()
'用户登录
If DropDownList1.SelectedValue = "学生" Then
Dim str As String
str = "Select username,password from [user] where username='" & Trim(TextBox1.Text) & "' and password='" & Trim(TextBox2.Text) & "'"
Dim cmd As New Data.OleDb.OleDbCommand(str, conn)
Dim myreader As Data.OleDb.OleDbDataReader
myreader = cmd.ExecuteReader()
If (myreader.Read()) Then
Dim str1 As String
str1 = "Select username,password from [user] where username='" & Trim(TextBox1.Text) & "' and password='" & Trim(TextBox2.Text) & "'"
Dim cmd1 As New Data.OleDb.OleDbCommand(str1, conn)
Dim myreader1 As Data.OleDb.OleDbDataReader
myreader1 = cmd1.ExecuteReader()
Dim aa As Object
aa = myreader1
Session("name") = TextBox1.Text
Response.Redirect("success.aspx?id=" & Server.UrlEncode(TextBox1.Text) & "& pas=" & Server.UrlEncode(aa))
'Response.Redirect("userInfo.aspx?name=" & Server.UrlEncode(TextBox1.Text))
Else
Response.Write("<script>alert('用户不存在或密码错误!');history.back();</script>")
End If
Else
If DropDownList1.SelectedValue = "老师" Then
Dim str1 As String
str1 = "Select admin_name,admin_pass from [adminuser] where admin_name='" & Trim(TextBox1.Text) & "' and admin_pass='" & Trim(TextBox2.Text) & "'"
Dim cmd1 As New Data.OleDb.OleDbCommand(str1, conn)
Dim myreader1 As Data.OleDb.OleDbDataReader
myreader1 = cmd1.ExecuteReader()
If (myreader1.Read()) Then
Session("mem") = TextBox1.Text
Response.Redirect("admin.aspx")
Else
Response.Write("<script>alert('用户不存在,请注册!');history.back();</script>")
End If
End If
End If
End If
aa = myreader1
行 47: Session("name") = TextBox1.Text
行 48: Response.Redirect("success.aspx?id=" & Server.UrlEncode(TextBox1.Text) & "& pas=" & Server.UrlEncode(aa))
行 49: 'Response.Redirect("userInfo.aspx?name=" & Server.UrlEncode(TextBox1.Text))
行 50: Else
源文件: F:\任叶毕业设计\网络0601任叶\系统源程序\login.aspx.vb 行: 48
堆栈跟踪:
[InvalidCastException: 从类型“OleDbDataReader”到类型“String”的转换无效。]
Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value) +591
_Default.Button2_Click(Object sender, EventArgs e) in F:\任叶毕业设计\网络0601任叶\系统源程序\login.aspx.vb:48
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
[解决办法]
if(aa.Reader()) Then
Dim pass as String
pass = aa["password"].ToString()
Response.Redirect("success.aspx?id=" & Server.UrlEncode(TextBox1.Text) & "&pas=" & pass)
[解决办法]
应该是这样的
if(aa.Reader()) Then
Dim pass as String
pass = aa("password").ToString()
Response.Redirect("success.aspx?id=" & Server.UrlEncode(TextBox1.Text) & "&pas=" & pass)
如果不行把pass = aa("password").ToString()改成pass = aa(1).ToString()
对于OleDbDataReader对象,在查询出来时,它是指在了第一行的前面,便不指向具体的行
所以你要调用它的Reader()方法,这时,它指向第一行,这个时候就可以用aa(1)进行取值了
你代码中的写法是明显错了