简体   繁体   中英

Incorrect syntax near',',

I am trying to add data into a database. When I run code in the save button method, the above error pops up.

        try
        {
            string constr = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Noah\Desktop\vs13\Project\JW_Accounting_Appliction\JW_Accounting_App\myDatabase.mdf;Integrated Security=True";
            SqlConnection conn = new SqlConnection(constr); //connects to regstration DB//
            conn.Open(); //to open connection to DB
            String insertQuery = "insert into AccountReceipt(TNo.,[Date], [WorldWide], [Local], [sumWW_Local]) values (@TNo., @Date, @WorldWide, @Local, @sumWW_Local)"; // inserts into table and declares data as variables// 
            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.AddWithValue("@TNo.", textBox1.Text);
            com.Parameters.AddWithValue("@Date", pickerDate.Text);
            com.Parameters.AddWithValue("@WorldWide", txtWorldWide.Text);
            com.Parameters.AddWithValue("@Local", txtLocal.Text);
            com.Parameters.AddWithValue("@sumWW_Local", txtTotal.Text);


            com.ExecuteNonQuery();
            MessageBox.Show("Thank you, Your registration has been successfull!");
            conn.Close();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }

The usage of parameterization is appreciated, But you have to care about naming conventions as well. Here the issue is with the column name TNo. please enclose then inside a [] ie.,

 "insert into AccountReceipt([TNo.] .. // rest of codce)

Anyway TNo. would not be a good name for a column, try to follow some good naming conventions

Don't use No. both in column name and in paramater.

insert into AccountReceipt([TNo.],[Date], [WorldWide], [Local], [sumWW_Local]) values (@TNo, @Date, @WorldWide, @Local, @sumWW_Local)

com.Parameters.AddWithValue("@TNo", textBox1.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM