Syntax error near Desc SQL, error displayed in cmd.ExecuteNonQuery ()

I am trying to issue an Insert statement in C # for an accounting application, and I came across some problems that say that it has an error on line 88 that falls out 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());
                }
            }
        }
    }
}

talks about incorrect syntax next to desc and points to this line cmd.ExecuteNonQuery(), what am I missing?

+4
source share
2 answers

DESC is a reserved word in SQL, short for top-down and used in ORDER BY clauses. Wrap it in square brackets in your SQL statement.

+2
source

desc. . (, ), , , , . [ ] : [desc] .

0

Source: https://habr.com/ru/post/1688588/


All Articles