我在Asp.NET项目中有登录页面,我想要的基本上是获取返回i_idficha,contrasena和 s_nombre 的查询列的行。名为 s_nombre 的列将保存在变量SESSION中。这就是我所拥有的:
Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Threading.Thread.Sleep(2000)
Dim cmd As New SqlCommand("SELECT dbo.USUARIO.i_idficha, dbo.CONTRASENA_USUARIO.s_contrasena, dbo.USUARIO.s_nombre FROM dbo.USUARIO INNER JOIN dbo.CONTRASENA_USUARIO ON dbo.USUARIO.i_idficha = dbo.CONTRASENA_USUARIO.i_ficha WHERE i_idficha = @ficha AND s_contrasena = @pass", cn)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@ficha", SqlDbType.Int).Value = txtUser.Text
cmd.Parameters.AddWithValue("@pass", SqlDbType.VarChar).Value = txtPass.Text
cn.Open()
Dim adp As New SqlDataAdapter(cmd)
Dim table As New DataTable()
adp.Fill(table)
Dim nombre = table.Rows("s_nombre").ToString()
If table.Rows.Count <> 0 Then
If CheckBox1.Checked Then
Response.Cookies("FICHA").Value = txtUser.Text
Response.Cookies("CONTRASENA").Value = txtPass.Text
Response.Cookies("FICHA").Expires = DateTime.Now.AddDays(10)
Response.Cookies("CONTRASENA").Expires = DateTime.Now.AddDays(10)
Else
Response.Cookies("FICHA").Expires = DateTime.Now.AddDays(-1)
Response.Cookies("CONTRASENA").Expires = DateTime.Now.AddDays(-1)
End If
Session("NOMBRE") = nombre
Response.Redirect("~/Default.aspx")
Else
Panel1.Visible = True
End If
End Sub
我在这里做的是声明一个名为 nombre 的变量,它等于
table.Rows( “s_nombre”)。的ToString()
并将其保存在变量 SESSION(“NOMBRE”)中,但不起作用。
这是错误:
输入字符串的格式不正确
这是我对Default.aspx的代码隐藏:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Session("FICHA") <> Nothing Then
Label1.Text = "Bienvendio <i aria-hidden='true' class='glyphicon glyphicon-user'></i> " & Session("NOMBRE").ToString()
Else
Response.Redirect("~/Login.aspx")
End If
End Sub
Protected Sub linkBtnLogOut_Click(sender As Object, e As EventArgs) Handles linkBtnLogOut.Click
Session("FICHA") = Nothing
Response.Redirect("~/Login.aspx")
End Sub
如何保存查询中名为 s_nombre 的列?
答案 0 :(得分:0)
我非常确定table.Rows(x)需要行索引,而不是列字符串。所以它应该是:
Dim nombre = table.Rows(0)("s_nombre").ToString()
答案 1 :(得分:0)
我想发布我的答案,我解决了,因为我忘了写
SESSION("FICHA") = txtUser.Text
然后我的登录代码将:
Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Threading.Thread.Sleep(2000)
Dim cmd As New SqlCommand("SELECT dbo.USUARIO.i_idficha, dbo.CONTRASENA_USUARIO.s_contrasena, dbo.USUARIO.s_nombre FROM dbo.USUARIO INNER JOIN dbo.CONTRASENA_USUARIO ON dbo.USUARIO.i_idficha = dbo.CONTRASENA_USUARIO.i_ficha WHERE i_idficha = @ficha AND s_contrasena = @pass", cn)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@ficha", SqlDbType.Int).Value = txtUser.Text
cmd.Parameters.AddWithValue("@pass", SqlDbType.VarChar).Value = txtPass.Text
cn.Open()
Dim adp As New SqlDataAdapter(cmd)
Dim table As New DataTable()
adp.Fill(table)
If table.Rows.Count <> 0 Then
If CheckBox1.Checked Then
Response.Cookies("FICHA").Value = txtUser.Text
Response.Cookies("CONTRASENA").Value = txtPass.Text
Response.Cookies("FICHA").Expires = DateTime.Now.AddDays(10)
Response.Cookies("CONTRASENA").Expires = DateTime.Now.AddDays(10)
Else
Response.Cookies("FICHA").Expires = DateTime.Now.AddDays(-1)
Response.Cookies("CONTRASENA").Expires = DateTime.Now.AddDays(-1)
End If
Dim nombre = table.Rows(0)(2).ToString()
Session("FICHA") = txtUser.Text
Session("NOMBRE") = nombre
Response.Redirect("~/Default.aspx")
Else
Panel1.Visible = True
End If
End Sub
谢谢@Aki