-1

i have the following error in the Update code :

You have an error in your SQL syntax; check the manual that corresponds to your MSQL server version for the right syntax to _use _near'(where Aid ='3'at line '1' Here is the code

private void button2_Click(object sender, EventArgs e) 

    { 

        try 

        { 

            //This is my connection string i have assigned the database file address path 

            string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root"; 

            //This is my update query in which i am taking input from the user through windows forms and update the record. 

            string Query = "update aircraft.l1201 set Name='" + this.nameTextBox.Text + "',Initials='" + this.piniTextBox.Text + "', where Aid='" + this.AidTextBox.Text + "';"; 

            //This is  MySqlConnection here i have created the object and pass my connection string. 

            MySqlConnection MyConn2 = new MySqlConnection(MyConnection2); 

            MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2); 

            MySqlDataReader MyReader2; 

            MyConn2.Open(); 

            MyReader2 = MyCommand2.ExecuteReader(); 

            MessageBox.Show("Data Updated"); 

            while (MyReader2.Read()) 

            { 



            } 

            MyConn2.Close();//Connection closed here 

        } 

        catch (Exception ex) 

        { 



            MessageBox.Show(ex.Message); 

        } 

    }
juergen d
  • 201,996
  • 37
  • 293
  • 362
guetty
  • 13
  • 4
  • Please format your post. It shows some effort on your side. – juergen d Jun 19 '17 at 10:24
  • Have a read of https://stackoverflow.com/a/30367283/34092 re: SQL injection. It won't solve your problem today - but it will save you a lifetime of pain later. – mjwills Jun 19 '17 at 10:41

1 Answers1

3

Remove the comma

... , where Aid=...
    ^---here

And you should really use Prepared Statements instead of patching your query together like this. And it would be way more secure too.

juergen d
  • 201,996
  • 37
  • 293
  • 362