Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Problem with SQL INSERT

$
0
0
private bool InsertTableDokument(string id_vrste, string datum, string id_korisnika)
{
      SqlConnection connection = new SqlConnection(Properties.Settings.Default.ReproDatabaseConnectionString);
      SqlCommand command = new SqlCommand("INSERT INTO [Dokument] ([id_dokumenta], [id_vrste], [datum], [id_korisnika]) VALUES (@id_dokumenta, @id_vrste, @datum, @id_korisnika);", connection);
      docuAdapter.Fill(document);

      connection.Open();
      command.Parameters.AddWithValue("@id_dokumenta", document.Rows.Count + 1);
      command.Parameters.AddWithValue("@id_vrste", id_vrste);
      command.Parameters.AddWithValue("@datum", datum);
      command.Parameters.AddWithValue("@id_korisnika", id_korisnika);

      try
      {
          command.ExecuteNonQuery();

          return true;
      }

      catch(Exception)
      {
          return false;
      }

      finally
      {
          connection.Close();
      }
 }

public async Task<bool> InsertTableDokumentAsync(string id_vrste, string datum, string id_korisnika)
{
     return await Task.Run(() => this.InsertTableDokument(id_vrste, datum, id_korisnika)).ConfigureAwait(continueOnCapturedContext: false);
}

Those are my methods for inserting new values into table. When I go to the form where I am using that method (async type), I can see values in the DataGridView control that I have entered manually into the database. Then, I enter new values via 3 TextBox controls, and when I press button I activate InsertTableDokumentAsync. DGV then shows those values, but as soon as I close the application, those values are gone from database. I have checked everywhere in the project if I forget to write somewhere connection.Close, but I didn't. Is there a specific reason why is this happening? And how to fix this bug? I have also been using debugger but there is no exception in the application.

Development Technician, Microsoft Certified Professional


Viewing all articles
Browse latest Browse all 31927

Trending Articles