c# - 无效的转换异常,没有重定向或警报

标签 c# asp.net sql

我有一个返回 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");
     }
}
  1. 此处无效转换异常:

    "结果 =(int)sqlcom.Parameters["@OutRes"].Value; System.InvalidCastException 未被用户代码处理 Message=指定的转换无效"

  2. 该代码不显示警报或重定向。发生了什么事?


这不会显示。根据我的查询,我的结果变量存储为 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/

相关文章:

缺少 MemoryMappedFiles 的 C# UWP Windows 10 进程间通信

asp.net - 列表框 asp.net 中列表项的点

mysql - 根据 id 列的值将一个 sql 列转换为两列

c# - 用于 C# 的免费条码 128 库

c# - 确定用户CPU大小的java程序?

c# - 获取与类型关联的所有 namespace ?

sql - 变量作为最高值

asp.net - 嵌套 <form> 可能吗?

asp.net - Session.Clear() 与 Session.Contents.Clear() 之间的区别

mysql - 将多列转置为行 - MySQL