错误在ASP.NET中更新数据 [英] Error Updating data in ASP.NET

查看:80
本文介绍了错误在ASP.NET中更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





 受保护  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))。文字;
decimal Rate = Convert.ToDecimal(((TextBox)GridView1.Rows [e.RowIndex] .FindControl( txtRate))。文字);
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 +
其中ServiceId = @ServiceId; +
选择ServiceId,GrandParentService,ParentService,ServiceName,Service from Service;
cmd.Parameters.Add( @ ServiceName,SqlDbType.VarChar).Value =服务名称;
cmd.Parameters.Add( @ Rate,SqlDbType.Decimal).Value =率;
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();
}



私有 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); // 错误必须声明标量变量@ServiceId。
返回 dt;
}









我收到一条错误消息必须在更新时声明标量变量@ServiceId。



请帮帮我。在此先感谢。

解决方案

该异常意味着您没有将命名变量声明为命令的参数。



你需要加上这个:



 cmd.Parameters.Add(@ ServiceId,SqlDbType.VarChar).Value = 它的价值; 









如果您不会声明该变量并为其赋值,因此您不能将其用作WHERE子句中的参数。您可以选择完全删除它或添加变量。


正如我在您的代码中看到的那样。你在where子句中使用@ServiceId



其中ServiceId = @ ServiceId;

你不是将值传递给此参数

使用以下



 cmd.Parameters。添加  @ ServiceId,SqlDbType.YourDatatype).value = yourValue ; 


I am unable to figure out the exact problem of the given code when i try to update.


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;
        }





I am getting an error message "Must declare the scalar variable "@ServiceId" at the time of update.

Please help me. Thanks in advance.

解决方案

That exception means you are not declaring the named variable as a parameter of your command.

You need to add this:

cmd.Parameters.Add("@ServiceId", SqlDbType.VarChar).Value = "the value for it";



[EDIT]

If you are not going to declare that variable and give it a value, then you cannot use it as a parameter in your WHERE clause. Your options are to remove it completely or add the variable.


As i can see in your code. You are using @ServiceId in your where clause

where ServiceId = @ServiceId;
and you are not passing value to this parameter
use below

cmd.Parameters.Add("@ServiceId", SqlDbType.YourDatatype).value=yourValue;


这篇关于错误在ASP.NET中更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆