-1

Im actually trying to insert data on my SQL Server database from my C# code, but it doesn't work, when i want to see my data it says no data inserted... i don't know exactly why i'm new to databases and SQL.

private void addRdv_Click(object sender, RoutedEventArgs e)
    {
        string query = "INSERT INTO RendezVous VALUES((SELECT Id FROM Patient WHERE nomPatient=@nomPatient),@jour,@mois,@annee,@heure,@minute,(SELECT Id FROM Medecin WHERE nomMedecin=@nomMedecin))";
        using (connection = new SqlConnection(connectionString))
        using (SqlCommand command = new SqlCommand(query, connection))
        {
            connection.Open();
            command.Parameters.AddWithValue("@nomPatient", nomPatient.Text);
            command.Parameters.AddWithValue("@jour", Int32.Parse(jour.Text));
            command.Parameters.AddWithValue("@mois", Int32.Parse(mois.Text));
            command.Parameters.AddWithValue("@annee", Int32.Parse(annee.Text));
            command.Parameters.AddWithValue("@heure", Int32.Parse(heure.Text));
            command.Parameters.AddWithValue("@minute", Int32.Parse(minutes.Text));
            command.Parameters.AddWithValue("@nomMedecin", nomMedecin.Text);
            command.ExecuteScalar();
            command.CommandText = "SELECT * FROM RendezVous";
            SqlDataReader reader = command.ExecuteReader();
            nomMedecin.Text = reader.GetInt32(0).ToString();
        }
B. Amine
  • 578
  • 4
  • 9
  • command.CommandText = "SELECT * FROM RendezVous"; Is part of the problem, Your command.CommandText would need to be your INSERT statement. – Ryan Wilson Feb 20 '18 at 19:06
  • You never execute your insert statement. You're only calling your reader. – Ryan C Feb 20 '18 at 19:06
  • @Steve he didn't get an exception because he is changing the command objects query string to the SELECT statement right before calling ExecuteReader() – Ryan Wilson Feb 20 '18 at 19:08
  • You should separate your select statement from your insert command. You only need the `Id` column from your query, so a simple insert of that value after your select statement makes more sense – Ryan C Feb 20 '18 at 19:08

1 Answers1

1

You have a trouble with the next lines of code:

command.CommandText = "SELECT * FROM RendezVous";
SqlDataReader reader = command.ExecuteReader();

There you implement data reading.

For data inserting you need to use INSERT query which you defined to variable string query.

Try to use next code:

command.CommandText = query;
command.ExecuteNonQuery();

UPDATE:

If you need to get INSERTED ID then please review next Stack Overflow topic.

Alexander I.
  • 2,380
  • 3
  • 17
  • 42
  • i tried this for the insert statement, but still no data is inserted to my database. When i reach the nomMedecin.Text = reader.GetInt32(0).ToString(); I get an exception that states the database is empty so i can't read data – B. Amine Feb 22 '18 at 10:17
  • @B.Amine please review next SO [topic](https://stackoverflow.com/questions/18373461/execute-insert-command-and-return-inserted-id-in-sql). There is answer to your question. – Alexander I. Feb 23 '18 at 09:38