0

Tried to move data from one form to another and there is a problem with the table. Yes I found such themes with a mistake, and tried to correct himself, but something went wrong.

using (SqlConnection conn = new SqlConnection("Data Source=DESKTOP-R552818\\SQLEXPRESS;Initial Catalog=Fond;Integrated Security=True"))
{
    SqlDataAdapter comm = new SqlDataAdapter("INSERT INTO Pacient (Name, id_diagnoz, Surname, Middle_name, Column__Passport, Legal_address_Clinic, Age) " +
                "VALUES ('"+ tName.Text + "', (SELECT id_diagnoz FROM Diagnoz WHERE Name_diagnoz = '" + cbName.Text + "' and Stage = '" + cbStage.Text + "'), '" + tSurname.Text + "', '" + tMiddle.Text + "', '" + tPas.Text + "', '" + cbClinic.Text + "', '" + tAge.Text + "')", conn);
    conn.Open();

    DataSet ds = new DataSet();
    //ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
    comm.Fill(ds);

    Form1 form = new Form1();
    form.DataGrid.DataSource = ds.Tables[0]; //?
}

string connectionString = "Data Source=DESKTOP-R552818\\SQLEXPRESS;Initial Catalog=Fond;Integrated Security=True";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();

                SqlCommand command = connection.CreateCommand();
                command.Transaction = transaction;

                try
                {                        
                    command.CommandText = "INSERT INTO Pacient (Name, id_diagnoz, Surname, Middle_name, Column__Passport, Legal_address_Clinic, Age) " +
                "VALUES ('" + metroTextBox1.Text + "', (SELECT id_diagnoz FROM Diagnoz WHERE Name_diagnoz = '" + metroComboBox1.Text + "' and Stage = '" + metroComboBox2.Text + "'), '" + metroTextBox2.Text + "', '" + metroTextBox3.Text + "', '" + maskedTextBox1.Text + "', '" + metroComboBox3.Text + "', '" + metroTextBox5.Text + "')";

                    command.ExecuteNonQuery();                        
                    transaction.Commit();
                    MessageBox.Show("Added");  
                   //here is a DataSet                                              
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    transaction.Rollback();
                }

            }
  • 1
    First of all, use [Parameterized queries](https://www.dotnetperls.com/sqlparameter). Second: did you run your code inside SQL, does it return anything (I feet, that it doesn't)? – SᴇM Mar 01 '19 at 08:23

1 Answers1

2

You are expecting results to be returned from your query, but what you do is just INSERT statement.

For inserting values you should use ExecuteNonQuery method of SqlCommand (see this for reference).

Then, assign another command: SELECT to get the results, then you can fill DataSet with the result and then you can fill DataGridView with it.

Also: you are rpone to SQL injection, use parametrized query to prevent yourself from such threat (see this for reference).

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • I've made some changes as you say. But i can not use now .Fill, maybe need again to reconnect SqlDataAdapter for this? Or another way –  Mar 01 '19 at 13:37
  • @Yippeeki-yay No, you call `Fill` when executing `SELECT` statement. With your current code: after `ExecuteNonQuery` you assign another SQL command: `SELECT` statement, then you proceed as usual, using `Fill` method. – Michał Turczyn Mar 01 '19 at 14:13