网格视图-更新错误 [英] Grid view- updating error

查看:110
本文介绍了网格视图-更新错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,

Hi Sir,

Server Error in '/GridView_7' Application.

Must declare the scalar variable "@emp_name".

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@emp_name".



源错误:




Source Error:


Line 62: 
Line 63:         con.Open();
Line 64:         cmd.ExecuteNonQuery();
Line 65:         con.Close();
Line 66:         

Source File: f:\Prasad Guduri\GridView_7\Default2.aspx.cs    Line: 64 



我遇到了以下代码的错误... !!!




I Got above error for the below CODE...!!!


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;



        var emp_name = row.FindControl("TextBox1") as TextBox;
        var emp_id = row.FindControl("TextBox2") as TextBox;
        var emp_dept = row.FindControl("TextBox3") as TextBox;
        var emp_location = row.FindControl("TextBox4") as TextBox;
        var emp_salary = row.FindControl("TextBox5") as TextBox;

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText=" update Employee set ename=@emp_name,eid=@emp_id,department=@emp_dept,elocation=@emp_location,esal=@emp_salary";
        cmd.CommandType = CommandType.Text;

        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@ename", emp_name.Text);
        cmd.Parameters.AddWithValue("@eid", emp_id.Text);
        cmd.Parameters.AddWithValue("@department", emp_dept.Text);
        cmd.Parameters.AddWithValue("@elocation", emp_location.Text);
        cmd.Parameters.AddWithValue("@esal", emp_salary.Text);

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        

        GridView1.EditIndex = -1;          
    }



请帮助我..... !!!


[edit]添加了代码块,删除了SHOUTING-OriginalGriff [/edit]



Please Help me.....!!!


[edit]Code block added, SHOUTING removed - OriginalGriff[/edit]

推荐答案

查看您的代码,并显示错误消息:
Look at your code, and at the error message:
cmd.CommandText=" update Employee set ename=@emp_name,eid=@emp_id,department=@emp_dept,elocation=@emp_location,esal=@emp_salary";
...
cmd.Parameters.AddWithValue("@ename", emp_name.Text);


更改参数名称以匹配UPDATE语句:


Change the paramater name to match the UPDATE statement:

cmd.Parameters.AddWithValue("@emp_name", emp_name.Text);



然后,对其他四个参数重复该过程...

[edit]添加了其他四个参数-OriginalGriff [/edit]



Then, repeat the process for the other four parameters...

[edit]Added other four parameters - OriginalGriff[/edit]


这篇关于网格视图-更新错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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