Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 指定的强制转换无效,无法强制转换SqlParameter_C#_Sql Server_Ado.net - Fatal编程技术网

C# 指定的强制转换无效,无法强制转换SqlParameter

C# 指定的强制转换无效,无法强制转换SqlParameter,c#,sql-server,ado.net,C#,Sql Server,Ado.net,此代码提供InvalidCastException:指定的强制转换无效 // getting the user id SqlCommand cmd2 = new SqlCommand("getId", conn); cmd2.CommandType = CommandType.StoredProcedure; cmd2.Parameters.Add(new SqlParameter("@email", email)); // output parm SqlParameter user_id

此代码提供InvalidCastException:指定的强制转换无效

// getting the user id

SqlCommand cmd2 = new SqlCommand("getId", conn);
cmd2.CommandType = CommandType.StoredProcedure;

cmd2.Parameters.Add(new SqlParameter("@email", email));

// output parm
SqlParameter user_id = cmd2.Parameters.Add("@user_id", SqlDbType.Int);
count.Direction = ParameterDirection.Output;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

Session["user_id"] = user_id;

Response.Redirect("~/CheckProfile.aspx");
然后在另一个页面中使用此用户id时,我写下:

int user_id = (int)(Session["user_id"]);
这应该可以

SqlCommand cmd2 = new SqlCommand("getId", conn);
cmd2.CommandType = CommandType.StoredProcedure;

cmd2.Parameters.Add(new SqlParameter("@email", email));

// output parm
SqlParameter user_id = new SqlParameter("@user_id", SqlDbType.Int);
user_id.Direction = ParameterDirection.Output;
cmd2.Parameters.Add(user_id);

conn.Open();
cmd2.ExecuteNonQuery();
conn.Close();

Session["user_id"] = cmd2.Parameters["@user_id"].Value;

Response.Redirect("~/CheckProfile.aspx");
这应该可以

SqlCommand cmd2 = new SqlCommand("getId", conn);
cmd2.CommandType = CommandType.StoredProcedure;

cmd2.Parameters.Add(new SqlParameter("@email", email));

// output parm
SqlParameter user_id = new SqlParameter("@user_id", SqlDbType.Int);
user_id.Direction = ParameterDirection.Output;
cmd2.Parameters.Add(user_id);

conn.Open();
cmd2.ExecuteNonQuery();
conn.Close();

Session["user_id"] = cmd2.Parameters["@user_id"].Value;

Response.Redirect("~/CheckProfile.aspx");

您正在会话[“用户id”]中存储
SqlParameter
,然后尝试将其转换为
int
。这一角色无法完成

相反,你应该:

Session["user_id"] = (int)user_id.Value;
然后,您将已经存储了
int
,并且能够检索它。不过,您可能希望强制转换为
int?
,因为这样您就可以处理未设置的问题:

int? userIDMaybe = (int?)Session["user_id"];
if(userIDMaybe.HasValue)
{
  int userID = userIDMaybe.Value;
  /* ... */
}
else
{
  /* Code to handle not logged in yet... */
}

您正在会话[“用户id”]中存储
SqlParameter
,然后尝试将其转换为
int
。这一角色无法完成

相反,你应该:

Session["user_id"] = (int)user_id.Value;
然后,您将已经存储了
int
,并且能够检索它。不过,您可能希望强制转换为
int?
,因为这样您就可以处理未设置的问题:

int? userIDMaybe = (int?)Session["user_id"];
if(userIDMaybe.HasValue)
{
  int userID = userIDMaybe.Value;
  /* ... */
}
else
{
  /* Code to handle not logged in yet... */
}

SqlParameter
!=
int
使用
AddWithValue()
代替:
SqlParameter
!=
int
改用
AddWithValue()
:我试过了,但它说:对象引用没有设置为对象的实例,指向这行代码:Session[“user\u id”]=(int)(user\u id.Value);有什么建议吗?我试过了,但它说:对象引用没有设置为对象的实例,指向这行代码:Session[“user_id”]=(int)(user_id.Value);有什么建议吗?