-1

for school I need to make a web application with c# and mysql. In the application there is a registration page but when I click on the button to finish I get a syntax error that says that the execute.nonquery is wrong. Can somebody help me ?

This is the code:

            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["gpopdrachtConnectionString"].ConnectionString);

            string insertQ;
            insertQ = "Insert into tblWerknemer(Gebruikersnaam, Vnaam, Fnaam, Wachtwoord, Geboortedatum, Geboorteplaats, E-mailadres, Geslacht, Loon, gsmNummer, Nationaliteit, Gemeente, Postcode, StraatEnNr)";
            insertQ += "values(@gbr, @Vn, @Fn, @Ww, @GebD, @GebPl, @Email, @Geslacht, @Loon, @gsm, @Nat, @Gemeent, @Post, @Strtnr)";
            insertQ += "Insert into tbldiploma(school,NaamDiploma) Values(@school,@diploma)";
            insertQ += "Insert into tblpostcode(postcode,gemeente) Values(@Post,@Gemeent)";
            insertQ += "Insert into tblstatuut(omschrijving) Values(@omschrijving)";

            MySqlCommand cmd = new MySqlCommand();
            //= new MySqlCommand(insertQ, conn); 

            cmd.CommandText = insertQ;
            cmd.Connection = conn;
            cmd.Parameters.AddWithValue("@gbr", txtgbr.Text);
            cmd.Parameters.AddWithValue("@Vn", txtVnaam.Text);
            cmd.Parameters.AddWithValue("@Fn", txtfnaam.Text);
            cmd.Parameters.AddWithValue("@Ww", txtww.Text);
            cmd.Parameters.AddWithValue("@GebD", txtgdatum.Text);
            cmd.Parameters.AddWithValue("@GebPl", txtgplaats.Text);
            cmd.Parameters.AddWithValue("@Email", txtemail.Text);
            cmd.Parameters.AddWithValue("@Geslacht", checkgeslacht.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@diploma", txtdiploma.Text);
            cmd.Parameters.AddWithValue("@school", txtschool.Text);
            cmd.Parameters.AddWithValue("@Loon", txtloon.Text);
            cmd.Parameters.AddWithValue("@omschrijving", txtstatuut.Text);
            cmd.Parameters.AddWithValue("@gsm", txtgsm.Text);
            cmd.Parameters.AddWithValue("@Nat", txtnat.Text);
            cmd.Parameters.AddWithValue("@Gemeent", txtgemeent.Text);
            cmd.Parameters.AddWithValue("@Post", txtpost.Text);
            cmd.Parameters.AddWithValue("@Strtnr", txtstrt.Text);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

and this is the error:

MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-mailadres, Geslacht, Loon, gsmNummer, Nationaliteit, Gemeente, Postcode, Straat' at line 1'

1 Answers1

1

You problem is a syntax near E-mailadres(the way you wrote it) try changing that to one name like emailaddres or email_adres.the dash(-) is causing you trouble

Peter
  • 111
  • 1
  • 1
  • 7
  • There are multiple problems. Either way, the code should be _in the question_. – ProgrammingLlama May 13 '18 at 13:20
  • Now I get an other error: MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Insert into tbldiploma(school,NaamDiploma) Values('UGent','Boekhouder')Insert in' at line 1' – Jolien Jansoone May 13 '18 at 13:44
  • 1
    @JolienJansoone You can execute multiple `INSERT` statements with one `ExecuteNonQuery` call, but they _must_ be separated with semicolons (`;`). So change `Values(@school,@diploma)";` (for example) to `Values(@school,@diploma);";` (and same for all statements in your SQL string). – Bradley Grainger May 13 '18 at 14:34
  • Try updating here to be something like this,, @Post, @Strtnr);"; //add a semicolon insertQ += "Insert into tbldiploma(school,NaamDiploma) Values(@school,@diploma);"; – Peter May 13 '18 at 14:45