本文介绍了尝试更新值时如何从ASP .NET页中的数据键名称获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我在尝试更新值时编写了以下代码.它没有更新.但这是说 @emp_id 期望我创建的过程的值是

Actually I wrote following code while I was trying to update a value. It was not updating. But it was saying that @emp_id expecting a value from the procedure that I created as

ALTER PROCEDURE EmployeeUpdate
(
       @emp_id int,@emp_name varchar(30),
       @emp_address varchar(300)
)
AS
begin
update EMPLOYEE_TABLE set Name=@emp_name,Address=@emp_address where EmployeeID=@emp_id

end




我编写的用于以gridview格式更新的代码是


and

the code i written for updating in a gridview format was

protected void EmployeeGridView_RowUpdate(object sender, GridViewUpdateEventArgs e)

        {
            int empid = Convert.ToInt32(EmployeeGridView.DataKeys[e.RowIndex].Value.ToString());//Get Each Row unique value from DataKeyNames

        string name = ((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtname")).Text;//get TextBox Value in EditItemTemplet that row is clicked

        string address = ((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtaddress")).Text;
        string connectionstring = @" Data Source=01HW235982\SQLEXPRESS2008;Initial Catalog=adarsh;Persist Security Info=True;User ID=sa;Password=Pass@123";
            SqlConnection con = new SqlConnection(connectionstring);
        con.Open();
        SqlCommand cmd = new SqlCommand("EmployeeUpdate",con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EmployeeID ", empid);
        cmd.Parameters.AddWithValue("@Name ", name);
       
        cmd.Parameters.AddWithValue("@Address ", address);
        cmd.ExecuteNonQuery();//Sql Command Class method return effected rows use for insert,update, delete
        EmployeeGridView.EditIndex = -1;// no row in edit mode
        FillGrid();//call method for agin fill grid after update
       }


请告诉我解决方案


Please tell me the solution

推荐答案

cmd.Parameters.AddWithValue("@EmployeeID ", empid);
cmd.Parameters.AddWithValue("@Name ", name);
cmd.Parameters.AddWithValue("@Address ", address);



而在过程 EmployeeUpdate 中,您已定义了诸如...
之类的参数.@emp_id, @emp_name, @emp_address.

它们不一样.因此,您必须使它们相同才能执行代码.

尝试使名称完全相同,并让我知道.

谢谢...



Whereas in your procedure EmployeeUpdate, you have defined the parameters like...
@emp_id, @emp_name, @emp_address.

They are different. So, you have to have them same in order to execute the code.

Try making the names exactly the same and let me know.

Thanks...


这篇关于尝试更新值时如何从ASP .NET页中的数据键名称获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:55