hello everyone. i have a data table, tblrating and i will refill a specific row in it. i have selected that row and hen insert the values, wihtout any result???!!! the codes are as follow:
sxtring q = string.Format("select * from tblrating where Nr='{0}'", rew_n[0]);
command.CommandText = q;
int amountOfUsers = Convert.ToInt32(command.ExecuteScalar());
if (amountOfUsers >= 1) //the row exist
{
int rew = (j + rew_n[3] * (rew_n[1] - 1)) / rew_n[1];
q = string.Format(@"INSERT INTO tblrating (Email,Title,Visitors,rating,review,Nr)VALUES
('" + adEmail.Text + "','" + s[1].ToString() + "','" + rew_n[1]++ + "','" + j + "','" + rew + "','" + rew_n[0] + "')");
command.CommandText = q;
command.ExecuteNonQuery();
}
i tried with:
q = string.Format(@"INSERT INTO tblrating (Email,Title,Visitors,rating,review,Nr)VALUES
('" + adEmail.Text + "','" + s[1].ToString() + "','" + rew_n[1]++ + "','" + j + "','" + rew + "','" + rew_n[0] + "') where Nr='{0}'", rew_n[0]);
but got error: i appreciate any help... kind regards
Cr BhargaviPosted Aug 8, 2023, 8:33 AM
It looks like you're trying to perform an
INSERToperation with aWHEREclause, which is not the correct syntax. AnINSERTstatement is used to add new rows to a table, and it doesn't have aWHEREclause since it's meant to create new records. If you want to update an existing row in the table, you should use theUPDATEstatement.Amit MohantyPosted Aug 8, 2023, 5:51 AM
I think here you're trying to update a specific row in the tblrating table if it already exists based on the value of Nr, and if it doesn't exist, insert a new row. However, your SQL syntax seems to be a bit mixed up.
Lokesh VarmanPosted Aug 8, 2023, 3:16 AM
It seems like you are trying to update an existing row in the "tblrating" table based on the condition that the row with the specified "Nr" value exists. To achieve this, you should use an
UPDATEstatement instead of anINSERTstatement. TheUPDATEstatement is used to modify existing records in a table.Here's how you can modify your code to use an
UPDATEstatement: