【发布时间】:2014-01-06 00:07:32
【问题描述】:
我的登录系统有问题。我制作了一个运行良好的简单系统,但我想让它更高级,并根据用户的UserType 显示不同的起始页面。但是,我遇到了以下错误:“必须声明标量变量@UserType。”
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class LoginwithEncryption : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(
"select * from dbo.UserInfo where Login =@Login and UserType=@UserType and Password=@Password and UserType=@UserType", con);
cmd.Parameters.AddWithValue("@Login", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPWD.Text+".123");
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
int usertype = Convert.ToInt32(dt.Rows[0]["UserType"]);
if (usertype == 1)
{
Response.Redirect("StartPage.aspx");
}
else if (usertype == 2)
{
Response.Redirect("DiferentStartPage.aspx");
}
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation",
"<script language='javascript'>alert('Invalid UserName and Password')</script>");
}
}
}
【问题讨论】:
-
您的意思是说
and UserType=@UserType两次?为什么不包括cmd.Parameters.AddWithValue("@UserType"...?