Reputation: 43
I am trying to issue an Insert statement in C# for an Accounting Application and i stumble into some problems which says it has an Error on Line 88 Which happens to fall here
private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(constring))
{
cn.Open();
if (choice.Text == "DEPOSIT")
{
double newAccBal = Convert.ToDouble(opening_amount.Text) + Convert.ToDouble(amount.Text);
string newBal = newAccBal.ToString();
string sql = "insert into credit (fullname,accountNo,opening_amount,amount,desc,newBal) values (@fullname,@accountNo,@opening_amount,@amount,@desc,@newBal)";
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
cmd.Parameters.AddWithValue("@fullname", fullname.Text);
cmd.Parameters.AddWithValue("@accountNo", textBox3.Text);
cmd.Parameters.AddWithValue("@opening_amount", opening_amount.Text);
cmd.Parameters.AddWithValue("@amount", amount.Text);
cmd.Parameters.AddWithValue("@desc", desc.Text);
cmd.Parameters.AddWithValue("@newBal", newBal);
try
{
var msg = MessageBox.Show("Information to be Sent for Deposit" + Environment.NewLine + "Please Confirm to Continue?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (msg == DialogResult.Yes)
{
cmd.ExecuteNonQuery(); <------------------------------ This Area
string confirmation = "Full Name : '"+fullname.Text+"' "+Environment.NewLine+" Depositing Amount : '"+amount.Text+"' "+Environment.NewLine+" Description : '"+desc.Text+"' "+Environment.NewLine+" New Balance : '"+newBal+"'";
MessageBox.Show("Deposit Successful" + Environment.NewLine + "Information has been Saved for Records" + Environment.NewLine + "Confirmation is as follows" + Environment.NewLine + confirmation ,"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
string sql2 = "update account_info set opening_amount = '"+newBal+"' where id='"+id.Text+"'";
using (SqlCommand cmd2 = new SqlCommand(sql2, cn))
{
cmd2.ExecuteNonQuery();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
}
says Incorrect syntax near desc and points at that line cmd.ExecuteNonQuery()
, what am i missing?
Upvotes: 4
Views: 142
Reputation: 2257
DESC is a reserved word in SQL, short for descending and used in ORDER BY clauses. Wrap it in square brackets in your SQL statement.
Upvotes: 2
Reputation: 77304
You named one of your columns desc
. However, that is a reserved word. You will either have to pick a different column name (might be a smart choice) or use a way to make sure your DBMS recognizes you mean your column, not the reserved word. You can use [
and ]
to annotate names: [desc]
should work.
Upvotes: 0