How can I fix the System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'of'.'

flowerforlife 51 Reputation points
2022-07-27T06:55:47.767+00:00

System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'of'.'

I am getting the above mentioned error whenever I try to run my form. The error highlights this statement: cmd.ExecuteNonQuery();

Here's my code.

namespace RegistrationForm
{
public partial class AptId : Form
{
public AptId()
{
InitializeComponent();
}

    void Connection()  
    {  
        string cs = ConfigurationManager.ConnectionStrings["Data Source=DESKTOP-0R6CP8V;Initial Catalog=Apartment;Persist Security Info=True;User ID=sa;Password=*******"].ConnectionString;  
        SqlConnection conn = null;  
        using (conn = new SqlConnection(cs))  
        {  
            string queryString = "select Rent from Customer_Apartment_Details where Rent>150";  
            SqlCommand command = new SqlCommand(queryString, conn);  
            conn.Open();  
            String datavalue = command.ExecuteScalar().ToString();  

            MessageBox.Show(datavalue);  
        }  
    }  

void insertData()
{
SqlConnection sqlConnection = new SqlConnection("Data Source=DESKTOP-0R6CP8V;User ID=sa;Password=*******;Initial Catalog=Apartment;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

        string query = "INSERT INTO Customer_Apartment_Details( ApartmentID, Date of occupying, Rent, Paymode) VALUES ( '" + apartmentIDTextBox.Text + "','" + date_of_occupyingTextBox.Text + "','" + rentTextBox.Text + "','" + paymodeTextBox.Text + "')";  
        SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query, sqlConnection);  
        sqlConnection.Open();  
        **cmd.ExecuteNonQuery();**  
        sqlConnection.Close();  
    }  
    public void button1_Click(object sender, EventArgs e)  
    {  
        int num = int.Parse(apartmentIDTextBox.Text);  
        switch (num)  
        {  
            case 1:  
                rentTextBox.Text = "280";  
                break;  
            case 2:  
                rentTextBox.Text = "250";  
                break;  
            case 3:  
                rentTextBox.Text = "300";  
                break;  
            case 4:  
                rentTextBox.Text = "300";  
                break;  
            case 5:  
                rentTextBox.Text = "270";  
                break;  
            case 6:  
                rentTextBox.Text = "270";  
                break;  
            case 7:  
                rentTextBox.Text = "220";  
                break;  
            case 8:  
                rentTextBox.Text = "200";  
                break;  
            case 9:  
                rentTextBox.Text = "200";  
                break;  
            case 10:  
                rentTextBox.Text = "200";  
                break;  
            default:  
                MessageBox.Show("Enter your apartment ID");  
                break;  
        }  
    }  
      
    public void button2_Click(object sender, EventArgs e)  
    {  
            insertData();  

        Rent rent = new Rent(this);  
        rent.ShowDialog();  

    }  
}  

Can someone please help me out solving this error?
Thanks in advance

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 84,281 Reputation points
    2022-07-27T07:02:44.287+00:00

    "INSERT INTO Customer_Apartment_Details( ApartmentID, Date of occupying,

    The problem seems to be the spaces in the column name
    Try to use [], like [ApartmentID], [Date of occupying],


2 additional answers

Sort by: Most helpful
  1. Ali B. Ali 0 Reputation points
    2023-03-23T03:49:20.1333333+00:00

    System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'into'.'

    0 comments No comments

  2. Ali B. Ali 0 Reputation points
    2023-03-23T03:50:16.4166667+00:00
     private void button1_Click(object sender, EventArgs e)
            {
                Con.Open();
                string query = "inseret into DoctorTbl Values(" + Docid.Text + ",  ' " + DocName.Text + "', " + Docexp.Text + "'," + DocPass.Text + "')";
                SqlCommand cmd = new SqlCommand(query, Con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Doctor Successfully Added");
                Con.Close();
            }
    
    System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'into'.'
     0No comments
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.