【问题标题】:Error Updating data in ASP.NET在 ASP.NET 中更新数据时出错
【发布时间】:2013-12-27 20:40:36
【问题描述】:

当我尝试更新时,我无法弄清楚给定代码的确切问题。

protected void UpdateService(object sender, GridViewUpdateEventArgs e)
    {
        int ServiceId = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblServiceID")).Text);
        string ServiceName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtService")).Text;
        decimal Rate = Convert.ToDecimal(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtRate")).Text);
        string ParentService = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlParentService")).SelectedItem.Text;
        string GrandParentService = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlGrandParentService")).SelectedItem.Text;

        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Service set ServiceName=@ServiceName,Rate=@Rate,"+
        "ParentService=@ParentService,GrandParentService=@GrandParentService "+
        "where ServiceId = @ServiceId;" +
         "select ServiceId,GrandParentService,ParentService,ServiceName,Rate from Service";
        cmd.Parameters.Add("@ServiceName", SqlDbType.VarChar).Value = ServiceName;
        cmd.Parameters.Add("@Rate", SqlDbType.Decimal).Value = Rate;
        cmd.Parameters.Add("@ParentService", SqlDbType.VarChar).Value = ParentService;
        cmd.Parameters.Add("@GrandParentService", SqlDbType.VarChar).Value = GrandParentService;


        GridView1.EditIndex = -1;
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }



private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);                    //Error "Must declare the scalar variable "@ServiceId".
        return dt;
    }

我收到一条错误消息“必须在更新时声明标量变量“@ServiceId”。“ServiceId”是表的主键。

请帮助我。提前致谢。

【问题讨论】:

  • 您的SqlCommand 中有一个@ServiceId,但您从未使用Parameters.Add() 方法将其添加为参数。
  • ServiceId 是我的主键。
  • 但是就像 Soner 说的,您仍然需要传递 ServiceId 值,否则如果您在 WHERE 子句中没有传递任何内容,您的 SQL 命令将如何知道要更新的行?
  • 非常感谢....解决了。

标签: c# asp.net sql-server c#-4.0


【解决方案1】:

您在查询中指定了 @ServiceId 参数,但从未在命令中声明或添加参数。您需要在执行更新时指定此项,或在插入新行时省略 where 子句。

cmd.CommandText = "update Service set ServiceName=@ServiceName,Rate=@Rate,"+
    "ParentService=@ParentService,GrandParentService=@GrandParentService "+
    "where ServiceId = @ServiceId;" +
     "select ServiceId,GrandParentService,ParentService,ServiceName,Rate from Service";
cmd.Parameters.Add("@ServiceName", SqlDbType.VarChar).Value = ServiceName;
cmd.Parameters.Add("@Rate", SqlDbType.Decimal).Value = Rate;
cmd.Parameters.Add("@ParentService", SqlDbType.VarChar).Value = ParentService;
cmd.Parameters.Add("@GrandParentService", SqlDbType.VarChar).Value = GrandParentService;
cmd.Parameters.Add("@ServiceId", SqlDbType.Int).Value = 1; //Some Value

【讨论】:

    【解决方案2】:

    您的查询有一个名为 @ServiceId 的参数:

    cmd.CommandText = "update Service set ServiceName=@ServiceName,Rate=@Rate,"+
    "ParentService=@ParentService,GrandParentService=@GrandParentService "+
    "where ServiceId = @ServiceId;" +  // <--- right here
     "select ServiceId,GrandParentService,ParentService,ServiceName,Rate from Service";
    

    但你从不添加这样的参数:

    cmd.Parameters.Add("@ServiceName", SqlDbType.VarChar).Value = ServiceName;
    cmd.Parameters.Add("@Rate", SqlDbType.Decimal).Value = Rate;
    cmd.Parameters.Add("@ParentService", SqlDbType.VarChar).Value = ParentService;
    cmd.Parameters.Add("@GrandParentService", SqlDbType.VarChar).Value = GrandParentService;
    

    对于 SQL 代码,@ServiceId 是一个从未声明或赋值的变量,因此会出现错误。您需要为其添加一个参数:

    cmd.Parameters.Add("@ServiceId", SqlDbType.Int).Value = ServiceId;
    

    【讨论】:

      【解决方案3】:

      看起来 ServiceId 没有被赋予一个值,即使 @ServiceId 在你的 WHERE 子句中。

      您正在为 ServiceName 添加一个参数,而不是为 ServiceId 添加一个参数

      【讨论】:

      • ServiceId 是我的主键。请详细说明如何更新。
      • 您正在执行更新,因此假设您知道服务 ID
      【解决方案4】:

      添加这个

      cmd.Parameters.Add("@ServiceId", SqlDbType.Int).Value = ServiceId ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-13
        • 2022-01-20
        • 2011-12-30
        • 2020-06-21
        • 2019-03-14
        • 2012-10-04
        相关资源
        最近更新 更多