preetham acharya
preetham acharya

Reputation: 1

insert code error

insert into customer (Advance,status)
values(@Advance,@status)
where Name='" + txtcname.Text.Trim() + "'";

in the above insert statement in going to insert 2 values based in condition but i'm getting error in where condition...

incorrect syntax near keyword where

this is the error

Upvotes: 0

Views: 166

Answers (3)

Munawar
Munawar

Reputation: 2587

Insert query do not needs Where clause. Just write

insert into customer (Advance, status) values(@Advance, @status)

Are you trying to insert or update? if you need to update an existing record then use update instead of insert like this:

update customer set Advance=@Advance, status=@status 
where Name='" + txtcname.Text.Trim() + "'";

EDIT

Aforementioned update query will serve the purpose but its recommended to use stored procedures/parameterized queries for SQL injection safety. You should following use approach:

Private void UpdateRecord(string advance,string status, string name)
{
//SqlConnection con
SqlCommand cmdUpdate = new SqlCommand("update customer set Advance = @Advance, status = @Status where Name=@Name", con);
            cmdUpdate.Parameters.AddWithValue("@Advance", advance);
            cmdUpdate.Parameters.AddWithValue("@Status", status);            
            cmdUpdate.Parameters.AddWithValue("@name", name);
            cmdUpdate.ExecuteNonQuery();

}

Pass your data as following:

UpdateRecord(@Advance,@Status,txtcname.Text.Trim());

Upvotes: 4

Rajat Jaiswal
Rajat Jaiswal

Reputation: 643

You can not add where clause with values. You can achieve this with following way if you really want to insert new rows else you can follow the @Munawar solution

insert into customer (Advance, status)

SELECT @Advance,@status
FROM  customer where Name='" + txtcname.Text.Trim() + "'"

Upvotes: 0

Pablo
Pablo

Reputation: 2052

You can't use 'where' in an insert-statement. To achieve the same result, you can insert all entries and delete the wrong.

You can use a select-statement after an insert, where you select entries from a table into another. This could be a solution for you, too.

Insert into customer (advance, status) values (...)
select advance, status
from anotherCustomerTable
where ... 

P.S. try to prepare the where-part, too.

Upvotes: 0

Related Questions