Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the scenario (user clicks update)
TIME IN TIME OUT
PERSON A | 7:30 8:00
PERSON A | 7:35 8:00

I have the following code:
C#
string cmdstr = "update dbo.RecordEmployee set TIME_OUT=@timeout, REMARKS=@editRemarks where EMPLOYEE_NAME =@empname";


what it does and when I update the first record 8:00 will be assigned to the first record, and it will also be assigned to the second record


what I want to achieve here is when the user clicks update, the other record of person A's time out will remain null

so the only selected row updated will have a value
Posted
Comments
Pranav-BiTwiser 1-Apr-14 5:45am    
totally blank....what u want? plzz elaborate
JB0301 1-Apr-14 5:46am    
I want only the selected row to be updated, because for example there are instances that there are duplication of entries, so when I update one, all of them are updated, I want to update only the selected row.

You need to have an id column for each row and pass it as parameter in to your query.
The current row id. in your table you may have a unique id for each row.
In grid view where you are having your edit button pass commandName as id
e.g
C#
<asp:button id="btnEdit" text="Edit" runat="server" commandargument="<%#Eval("id")%>" commandname="Edit" xmlns:asp="#unknown" />


in grid view RowCommand event:
C#
using(SqlCommand cmd = new SqlCommand("update dbo.RecordEmployee set TIME_OUT=@timeout, REMARKS=@editRemarks where EMPLOYEE_NAME =@empname and id=@id", conn))
        {
           cmd.Parameters.AddWithValue("@id", id);
           cmd.ExecuteNonQuery();
        }
 
Share this answer
 
Comments
JB0301 1-Apr-14 20:28pm    
cmd.Parameters.AddWithValue("@id", id); this line is giving me an error- id does not include in current context
Schatak 2-Apr-14 1:16am    
forgot to mention..

int id = Convert.ToInt32(e.CommandArgument);
using(SqlCommand cmd = new SqlCommand("update dbo.RecordEmployee set TIME_OUT=@timeout, REMARKS=@editRemarks where EMPLOYEE_NAME =@empname and id=@id", conn))
{
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
}
JB0301 24-Apr-14 21:04pm    
how do i get form controls i.e TEXTBOX inside my gridview inside ROW COMMAND EVENT?
Check here, may be you get any useful information
link here
 
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