我有一个返回 OutRes 的 SQL 过程
protected void LoginButton_Click1(object sender, EventArgs e)
{
int Results = 0;
using (SqlConnection sc1 = new SqlConnection())
{
sc1.ConnectionString = "Data Source=localhost;Initial Catalog=Drug;Integrated Security=True";
SqlCommand sqlcom = new SqlCommand("user_login",sc1);
{
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.Add("@UsernameSP", SqlDbType.VarChar, 10).Value = this.txtUserName.Text.Trim();
sqlcom.Parameters.Add("@PasswordSP", SqlDbType.VarChar, 10).Value = this.txtPwd.Text.Trim();
sqlcom.Parameters.Add("@OutRes", SqlDbType.VarChar, 4);
sqlcom.Parameters["@OutRes"].Direction = ParameterDirection.Output;
sqlcom.Connection = sc1;
try
{
sc1.Open();
sqlcom.ExecuteNonQuery();
Results =(int)sqlcom.Parameters["@OutRes"].Value;
}
catch (SqlException ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + ex.Message + "')", true);
}
finally
{
sqlcom.Dispose();
if (sc1 != null)
{
sc1.Close();
}
}
}
if (Results == 0)
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "result 0" + "')", true);
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "result 1" + "')", true);
Response.Redirect("/DrugEntry.aspx");
}
}
此处无效转换异常:
"结果 =(int)sqlcom.Parameters["@OutRes"].Value; System.InvalidCastException 未被用户代码处理 Message=指定的转换无效"
该代码不显示警报或重定向。发生了什么事?
这不会显示。根据我的查询,我的结果变量存储为 0 或 1。
if (Results == 0)
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "result 0" + "')", true);
else
{ ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "result 1" + "')", true);
Response.Redirect("DrugEntry.aspx");
}
两个警报都没有显示。知道为什么吗?
最佳答案
您需要将 @Outres Parameter
的类型从 Varchar 更改为 Int
。
protected void LoginButton_Click1(object sender, EventArgs e)
{
int Results = 0;
using (SqlConnection sc1 = new SqlConnection())
{
sc1.ConnectionString = "Data Source=localhost;Initial Catalog=Drug;Integrated Security=True";
SqlCommand sqlcom = new SqlCommand("user_login",sc1);
{
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.Add("@UsernameSP", SqlDbType.VarChar, 10).Value = this.txtUserName.Text.Trim();
sqlcom.Parameters.Add("@PasswordSP", SqlDbType.VarChar, 10).Value = this.txtPwd.Text.Trim();
sqlcom.Parameters.Add("@OutRes", SqlDbType.Int, 4);
sqlcom.Parameters["@OutRes"].Direction = ParameterDirection.Output;
sqlcom.Connection = sc1;
try
{
sc1.Open();
sqlcom.ExecuteNonQuery();
Results =(int)sqlcom.Parameters["@OutRes"].Value;
}
catch (SqlException ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + ex.Message + "')", true);
}
finally
{
sqlcom.Dispose();
if (sc1 != null)
{
sc1.Close();
}
}
}
if (Results == 0)
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "result 0" + "')", true);
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "result 1" + "')", true);
Response.Redirect("/DrugEntry.aspx");
}
}
关于c# - 无效的转换异常,没有重定向或警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22527427/