我在asp.net中使用会话时出错
本文关键字:会话 出错 asp net 我在 | 更新日期: 2023-09-27 18:00:43
我有两个表Users
和Hobbies
。userId is primary key in
用户table and it is foreign key in
爱好table. I want same
用户ID in both tables. When user logged in he gets
用户ID . On next page there is hobbies page where user have to fill info and the same
用户ID `应保存到爱好表中。我正在为此使用会话,但它作为给出了例外
"Error converting datatype object to int."
我的代码是:
这是用户页面上的代码
protected void loginbtn_Click(object sender, EventArgs e)
{
SqlCommand cmd1 = new SqlCommand("select UserId from Users where username=@username and password=@password",con);
Session["ID"] = cmd1;
}
这是关于爱好的代码:
protected void Button4_Click(object sender, EventArgs e)
{
int id= Convert.ToInt32(Session["ID"]);
cmd.Parameters.AddWithValue("@UserId",id);
con.Open();
cmd.ExecuteNonQuery();
}
我是会话和存储过程的新手。这是我的第一个网络应用程序。请帮我提高。
您需要首先执行SQLCommand
以获得UserID
的值。现在,您正在传递一个SQLCommand
对象,它显然不是一个整数值。要修复错误,您需要做以下操作:
using (SqlConnection con = new SqlConnection (connectionstring))
{
SqlCommand cmd1 = new SqlCommand("select UserId from Users where username=@username and password=@password",con);
cmd.Parameters.AddWithValue("@username", username.Text);
cmd.Parameters.AddWithValue("@password", password.Text);
con.Open();
int UID = (int)cmd1.ExecuteScalar(); //Execute command and assign value
Session["ID"] = UID; //Now set session variable
}
当然,您需要处理异常等等,但这应该让您开始。
您不应该在会话中存储command
,也不应该将其转换为integer
。
登录tn_Click
protected void loginbtn_Click(object sender, EventArgs e)
{
string conString = "xxxxxxxxxxxxxxxxxxxxxxxx";
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
using (var cmd = new SqlCommand(
"select UserId from Users where username=@username and password=@password",
con))
{
cmd.Parameters.AddWithValue("@username", username.Text);
cmd.Parameters.AddWithValue("@password", password.Text);
var id = cmd.ExecuteScalar();
Session["ID"] = id;
}
}
}
按钮4_点击
protected void Button4_Click(object sender, EventArgs e)
{
string conString ="xxxxxxxxxxxxxxxxxxxxxxxxxx";
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
using (var cmd = new SqlCommand("select * from Users where UserId=@UserId",con))
{
var id= Convert.ToInt32(Session["ID"]);
cmd.Parameters.AddWithValue("@UserId",id);
using (SqlDataReader reader = cmd.ExecuteReader())
{
//get the data
}
}
}
}
首先,我想通知您,您正在将command
对象存储到Session
中,而不是UserID
中。所以你得到了这个错误!
在Users.aspx
页面:
在会话中存储用户ID:
using (SqlConnection con = new SqlConnection (yourConnectionString))
{
SqlCommand mycmd = new SqlCommand("select UserId from Users where username=@username and password=@password",con);
//add parameters to pass the username & password here
mycmd.Parameters.AddWithValue("@username", username.Text);
mycmd.Parameters.AddWithValue("@password", password.Text);
if (con.State != ConnectionState.Open)
{
con.Close();
con.Open();
}
int userID = (int)mycmd.ExecuteScalar();
Session["UserID"] = userID; //Store USerID in session
con.Close();
}
我不简单使用的原因:
if (con.State == ConnectionState.Closed)
{
con.Open();
}
是因为ConnectionState
也可以是:
Broken, Connnecting, Executing, Fetching
此外,微软表示,关闭,然后重新打开连接"将刷新状态值"。更多详细信息
在Hobbies.aspx
页面:
使用会话用户ID:
if(Session["UserID"]!=null)
{
int userID=Convert.ToInt32(Session["UserID"])
//do other stuff
}
插入爱好表:
private void btnInsertHobbies_Click(object sender, EventArgs e)
{
int userID=0;
if(Session["UserID"]!=null)
{
userID=Convert.ToInt32(Session["UserID"]);
}
//add Here fields of hobbies table
string hobby1= textBox2.Text;
string hobby2= textBox3.Text;
if (conn2.State != ConnectionState.Open)
{
conn2.Close();
conn2.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn2;
cmd.CommandText = "INSERT INTO Hobbies(userID, hobby1, hobby2) VALUES(@userID,@hobby1,@hobby2)";
cmd.Parameters.AddWithValue("@userID", userID);
cmd.Parameters.AddWithValue("@hobby1", hobby1);
cmd.Parameters.AddWithValue("@hobby2", hobby2);
cmd.ExecuteNonQuery();
conn2.Close();
}
注意:请根据您的表字段进行更改,如果有帮助,请标记为答案!