Click here to Skip to main content
15,881,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friend,

I am trying to update the gridview.I wrote the code also, the problem is when i update a single row its update all the data in database.

Here is my code for your reference.

C#
protected void addpro_Grid2_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       string pid = ((TextBox)addpro_Grid2.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       string pname = ((TextBox)addpro_Grid2.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
       addpro_Grid_Update(pid, pname);
       addpro_Grid2.EditIndex = -1;
       fill();
   }


C#
protected void fill()
    {
        cmd = new SqlCommand("select * from masters_addproduct", con);
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        addpro_Grid2.DataSource = dt;
        addpro_Grid2.DataBind();
    }


C#
public void addpro_Grid_Update(string pid, string pname)
    {
        cmd = new SqlCommand("update masters_addproduct set pid='" + pid + "',p_name='" + pname + "'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }




How can i update the gridview data using identity column in db.

update masters_addproduct set p_name='"+pname+"' where pid=@pid"


If i execute this code it shows error.

Must declare scalar variable pid..

How to overcome this problem.

Thanks in Advance.
Posted
Updated 7-Jul-11 20:06pm
v4
Comments
koolprasad2003 6-Jul-11 3:24am    
Make post as "Resolved", if it solved.

You forgot to put the where clause in the update query

it should be

cmd = new SqlCommand("update masters_addproduct set pid='" + pid + "',p_name='" + pname + "' where pid = '" + pid + "'", con);
 
Share this answer
 
v2
Comments
koolprasad2003 6-Jul-11 3:22am    
Yeah +5
[no name] 6-Jul-11 3:24am    
thanks
I suppse you need to add a where clause in your query?

"update masters_addproduct set pid='" + pid + "',p_name='" + pname + "' Where column1 = " + condition1 + " And column2 = " + condition2, con);
 
Share this answer
 
v2
Comments
[no name] 6-Jul-11 3:21am    
haha at same time.
nithibs 6-Jul-11 4:58am    
i used where condition, but the values are not updating in database what to do...
nithibs 6-Jul-11 5:20am    
cmd = new SqlCommand("UPDATE masters_addproduct SET pid='"+pid+"', p_name = '" + pname + "' WHERE pid='"+pid+"' AND p_name='"+pname+"'",con);

actually this condition is wrong bcoz the new value is given in where class condition also.... i know but any one clear me wat to give in where class condition........
You have to write WHERE condition in UPDATE query.
cmd = new SqlCommand("update masters_addproduct set pid='" + pid + "',p_name='" + pname + "' WHERE primarycolumnname = primaryid", con);
 
Share this answer
 
v2
Where is the "WHERE" ?

check your SQL query.
 
Share this answer
 
you do not need to update your pid..
you should pass pid in where clause.


cmd = new SqlCommand("update masters_addproduct set p_name='" + pname + "'" where pid='" + pid + "', con);

this hepls you to resolve and update particular row.
 
Share this answer
 
Hi,

use where conditon to update only one record
regards,
shefeek
 
Share this answer
 
Hi ,
You should write the where statement at the end of your query.
 
Share this answer
 

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