Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
How to insert data in an existing row using parameter
Posted
Comments
Sergey Alexandrovich Kryukov 11-May-12 13:29pm    
"Insert" and "existing" are mutually exclusive. You can "modify".
--SA

Here's one good tutorial you can go through: SQL UPDATE Statement[^]

Addition:

To update the last row based on column DateCol the statement could be something like:
SQL
UPDATE table 
SET ColumnA = @_ColumnA 
WHERE firstname LIKE @_firstname
AND DateCol = (SELECT MAX(DateCol)
               FROM table
               WHERE firstname LIKE @_firstname)
 
Share this answer
 
v2
Comments
Maciej Los 11-May-12 11:21am    
Good answer and link, my 5!
Wendelius 11-May-12 11:28am    
Thanks losmac :)
Member 8961685 11-May-12 12:41pm    
Thank you Mika, there is no primary key or unique key on this table , here is the code i am using
cmdScnDesc = new SqlCommand("UPDATE table SET ColumnA = @_ColumnA WHERE firstname LIKE @_firstname ",dbConnection);
but the problem its inserting into all rows but i like to update only the last row
Wendelius 11-May-12 12:49pm    
You could define the condition to your statement to update only the last row. For example if you have a date column which defines what is the last row, then use the MAX value of that column (per name).

However, I would strongly advice you to use a primary key, for example uniqueidentifier[^]
Member 8961685 11-May-12 13:17pm    
Thank Mila , if i have the uniqueidentifier then how do i update the specific field
insert data in an existing row using parameter
Looks like you asked for parameter based data insert/update.

Look here for parameterized query and it's usage:
MSDN: Configuring Parameters and Parameter Data Types (ADO.NET)[^]
MSDN: DataAdapter Parameters (ADO.NET)[^]
MSDN: SqlCommand.Parameters Property [^]


Read about protecting from SQL Injection here: SQL Injection Mitigation: Using Parameterized Queries[^]
 
Share this answer
 
Comments
Maciej Los 11-May-12 12:56pm    
Good answer, my 5!
Sandeep Mewara 11-May-12 13:07pm    
Thanks.
Wendelius 11-May-12 13:59pm    
Very good list!
Sandeep Mewara 12-May-12 2:00am    
Thanks Mika.
Same as inserting new row, just use UPDATE instead of INSERT. Make sure you use primary key to identify the row you want to change.
 
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