更新时间:2023-02-06 08:54:06
if myReader.HasRows Then
Session( UserID)= reader.GetString( 0 )
参考:使用DataReader检索数据 [ ^ ]
你可以使用 ExecuteScalar方法 [ ^ ]因为您需要一个值。阅读文档和给出的样本。
最后我终于解决了它,但不幸的是,在我提交课程后,解决方案非常简单,我很困惑你们怎么都无法想象它出来了。Dim con 作为 新 SqlConnection(_conString)
Dim sql 作为 字符串 = SELECT AccountType FROM tblUser WHERE Username = @ Username
Dim cmd As 新 SqlCommand(sql,con)
cmd.Parameters.AddWithValue( @用户名跨度>, txtUsername.Text)
Dim myReader As SqlDataReader
con.Open()
myReader = cmd.ExecuteReader
DetailsView1.DataSource = myReader
DetailsView1.DataBind()
' 从查询返回的行中读取accountType字段并将其存储在字符串中
Dim AT As String = myReader( AccountType)
如果 AT.Contains( Customer)然后
Resp onse.Redirect( 〜/ Customer.aspx)
Else :Response.Redirect( 〜/ Seller.aspx)
I'm using asp.net 4.0 code using visual basic in visual studio 2010 with web forms.
I have the following code:
Presentation layer:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<br /><br />
<asp:Label ID="lblUsername" runat="server" Text="Enter Username"></asp:Label>
<asp:TextBox ID="txtUsername" runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUsername" ErrorMessage="RequiredFieldValidator"
ForeColor="Red">*</asp:RequiredFieldValidator>
<br /><br />
<asp:Label ID="lblPassword" runat="server" Text="Enter Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtPassword" ErrorMessage="RequiredFieldValidator"
ForeColor="Red">*</asp:RequiredFieldValidator>
<br /><br />
Select Account Type
<asp:DropDownList ID="ddlAccountType" runat="server">
<asp:ListItem Value="C">Customer</asp:ListItem>
<asp:ListItem Value="S">Seller</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Label ID="lblRememberMe" runat="server" Text="Remember me"></asp:Label>
<asp:CheckBox ID="chkRememberMe" runat="server" />
<br /><br />
<asp:Button ID="btnLogin" runat="server" Text="Login" />
</asp:Content>
code-behind:
Imports System.Data Imports System.Data.SqlClient Imports System.Web.Configuration Partial Class Login Inherits System.Web.UI.Page Private ReadOnly _conString As String Public Sub New() _conString = WebConfigurationManager.ConnectionStrings("PetiteAnnonceCS").ConnectionString End Sub Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click Dim con2 As New SqlConnection(_conString) Dim sql2 As String = "SELECT UserId FROM tblUser WHERE Username=@Username And Password=@Password" Dim cmd2 As New SqlCommand(sql2, con2) cmd2.Parameters.AddWithValue("@Username", txtUsername.Text) cmd2.Parameters.AddWithValue("@Password", txtPassword.Text) Dim myReader As SqlDataReader Using con2 con2.Open() myReader = cmd2.ExecuteReader If myReader.HasRows Then If chkRememberMe.Checked = True Then Session("Username") = txtUsername.Text Session("LoginTime") = DateAndTime.Now.ToString Session.Timeout = 50000 If ddlAccountType.SelectedValue = "C" Then Response.Redirect("~/Customer.aspx") Else : Response.Redirect("~/Seller.aspx") End If Else Session("Username") = txtUsername.Text Session("LoginTime") = DateAndTime.Now.ToString If ddlAccountType.SelectedValue = "C" Then Response.Redirect("~/Customer.aspx") Else : Response.Redirect("~/Seller.aspx") End If End If Else MsgBox("Login UnSuccessful!") End If con2.Close() End Using End Sub End Class
I have the UserId as the primary key in my table User.
When the user logs in, the system checks if the user is valid, if it's valid the username is stored in a session variable. It's easy to store the username in a session because we can easily grab the value from the textbox, but I want to grab the UserId value also and store it in a session variable which I don't have any clue on how to do it. Can anyone help?
Is there a way to know the user
If myReader.HasRows Then Session("UserID") = reader.GetString(0)
refer : Retrieving Data Using a DataReader[^]
you can use ExecuteScalar Method[^] since you need one value. Read the documentation and the samples given.
Finally I've finally solved it but unfortunately after I submitted my coursework, the solution is pretty straightforward, I am puzzled on how you guys could not figure it out.Dim con As New SqlConnection(_conString) Dim sql As String = "SELECT AccountType FROM tblUser WHERE Username=@Username" Dim cmd As New SqlCommand(sql, con) cmd.Parameters.AddWithValue("@Username", txtUsername.Text) Dim myReader As SqlDataReader con.Open() myReader = cmd.ExecuteReader DetailsView1.DataSource = myReader DetailsView1.DataBind() 'Read the accountType field from the row returned from the query and store it in a string Dim AT As String = myReader("AccountType") If AT.Contains("Customer") Then Response.Redirect("~/Customer.aspx") Else : Response.Redirect("~/Seller.aspx")