Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...!!!


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


Please Help me.....!!!


[edit]Code block added, SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 15-Feb-12 9:10am
v2
Comments
OriginalGriff 15-Feb-12 15:10pm    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

1 solution

Look at your code, and at the error message:
C#
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);

Change the paramater name to match the UPDATE statement:
C#
cmd.Parameters.AddWithValue("@emp_name", emp_name.Text);


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

[edit]Added other four parameters - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Prasad Guduri 15-Feb-12 15:50pm    
Hi Sir after changing parameter as u said,

All the rows are updating with same values...
OriginalGriff 16-Feb-12 3:12am    
Well yes. That is what your UPDATE statement tells it to do.
If you want to update a single row, you have to tell it which one:
UPDATE myTable SET ... WHERE column=value
Prasad Guduri 15-Feb-12 15:51pm    
ename,eid,department,elocation,esal are my table field names.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900