tanya
tanya

Reputation: 43

Updating records

private void button1_Click(object sender, EventArgs e)
{
    using (SqlConnection sqlConn = new SqlConnection("Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True"))
    {
        string sqlQuery = @"UPDATE cottonpurchase SET @slipNo, @basicprice, @weight, @totalamountbasic, @premium, @totalamountpremium, @totalamountpaid, @yeildestimates  WHERE farmercode = @farmercode";
        {
            SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
            cmd.Parameters.Add("@slipNo", SqlDbType.Int).Value = TxtSlipNo.Text;
            cmd.Parameters.Add("@basicprice", SqlDbType.Int).Value = TxtBasicPrice.Text;
            cmd.Parameters.Add("@weight", SqlDbType.Int).Value = TxtWeight.Text;
            cmd.Parameters.Add("@totalamountbasic", SqlDbType.Int).Value = TxtTotalAmountBasic.Text;
            cmd.Parameters.Add("@premium", SqlDbType.Int).Value = TxtPremium.Text;
            cmd.Parameters.Add("@totalamountpremium", SqlDbType.Int).Value = TxtTotalAmountPremium.Text;
            cmd.Parameters.Add("@totalamountpaid", SqlDbType.Int).Value = TxtTotalAmountPaid.Text;
            cmd.Parameters.Add("@yeildestimates", SqlDbType.Int).Value = TxtYeildEstimates.Text;

            sqlConn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

It's giving me an error even though everything seems fine with my code:

error : incorrect syntax near  ','

Upvotes: 4

Views: 122

Answers (2)

Alex Aza
Alex Aza

Reputation: 78447

You need to specify column names that you are trying to set.

string sqlQuery = @"
    UPDATE cottonpurchase 
    SET 
        slipNo = @slipNo, 
        basicprice= @basicprice, 
        weight = @weight, 
        totalamountbasic = @totalamountbasic, 
        premium = @premium, 
        totalamountpremium = @totalamountpremium, 
        totalamountpaid = @totalamountpaid, 
        yeildestimates = @yeildestimates
    WHERE farmercode = @farmercode";

Also, you didn't provide @farmercode parameter:

cmd.Parameters.AddWithValue("@farmercode", <someValue>);

Upvotes: 4

Alex R.
Alex R.

Reputation: 4754

You forgot to mention the column names in the set.

string sqlQuery = @"UPDATE cottonpurchase SET slipNo=@slipNo, basicprice=@basicprice, ...  WHERE farmercode = @farmercode";

Upvotes: 1

Related Questions