I'm running a query from a web form to update records. Since I'm just learning about C#, I'm using a command string as opposed to a stored procedure.
My update method is as follows:
public void updateOne()
{
string commandText = "update INVOICE SET <Redacted> = @<Redacted>,
Supplier = @Sup, SupplierName = @SupN, NetTotal = @Net,
VATTotal = @VAT, InvoiceDate = @InvDt "
<needed a line break here, which is why I split the string again>
+ "WHERE K_INVOICE = @K_INV";
using (SqlConnection dbConnection = new SqlConnection
(conParams.connectionString))
{
SqlCommand cmd = new SqlCommand(commandText, dbConnection);
cmd.Parameters.Add("@K_INV", SqlDbType.Int);
cmd.Parameters["@K_INV"].Value = @K_INV;
cmd.Parameters.AddWithValue("@<Redacted>", @<Redacted>.ToString());
cmd.Parameters.AddWithValue("@Sup", @Sup.ToString());
cmd.Parameters.AddWithValue("@SupN", @SupN.ToString());
cmd.Parameters.AddWithValue("@Net", @Net.ToString());
cmd.Parameters.AddWithValue("VAT", @VAT.ToString());
cmd.Parameters.AddWithValue("@InvDt", @InvDt.ToString());
try
{
dbConnection.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
errorString = e.Message.ToString();
}
}
}
Catch stalls on an SQL error (Incorrect syntax near SET), and I have an idea that the issue occurs because I convert the parameters to strings. The first parameter is an Int, which should be OK.
If this is the case, what should I convert the parameters to? If not, what on earth is wrong?
<Redacted>contains<>that might be the problem in your query.@Literal symbol in your second param for the addwithvalue. what's< >used for in your Param..?cmd.Parameters.AddWithValue("@SupN", @SupN.ToString());just usecmd.Parameters.Add("@SupN", SqlDbType.VarChar, 100).Value = ........;