0

I´ve WPF application where i have button SaveData which calls function InsertValuesIntoGAStatistics and i want to insert data into database but nothing is inserted i don´t know where is problem everything looks fine and i didn´t get any error message just nothing is done.

private void SaveData_Click(object sender, RoutedEventArgs e)
{
    database.InsertValuesIntoGAStatistics("haha", 4, 4, 4, 4, 11, "hlalha", "blabla", "blabsdla");
}

button for saving data

public static string ConnectionStringGADatabase
{
    get
    {
        return ConfigurationManager.ConnectionStrings["GADatabase"].ConnectionString;
    }
}

here is property to get connectionString

public void InsertValuesIntoGAStatistics(string GAType, int Generations, int PopulationSize, float MaxFitness, float AverageFitness, int DmaxValue, string Selection, string CrossOver, string FilePath)
{
    try
    {
        using (SqlConnection connection = new SqlConnection(Connection.ConnectionStringGADatabase))
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandText = "INSERT INTO GAStatistics (GAType, Generations, PopulationSize, MaxFitness, AverageFitness, DmaxValue, Selection, CrossOver, FilePath) VALUES (@GAType, @Generations, @PopulationSize, @MaxFitness, @AverageFitness, @DmaxValue, @Selection, @CrossOver, @FilePath)";
            command.Parameters.AddWithValue("@GAType", GAType);
            command.Parameters.AddWithValue("@Generations", Generations);
            command.Parameters.AddWithValue("@PopulationSize", PopulationSize);
            command.Parameters.AddWithValue("@MaxFitness", MaxFitness);
            .............
            connection.Open();
            command.ExecuteNonQuery();
        }
    }
    catch (SqlException ex)
    {
        Console.WriteLine(ex.Message);
    }
}

this is function InsertValuesIntoGAStatistics which should insert data into database

<connectionStrings>
<add name="GADatabase" connectionString="Data Source=LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database\GADatabase.mdf;Integrated Security=True" 
     providerName="System.Data.SqlClient" />
</connectionStrings>

and this part of code is in App.config file

daniele3004
  • 13,072
  • 12
  • 67
  • 75
Seda
  • 103
  • 2
  • 12
  • 1
    Did you debug your code line by line? Are you sure your `ConnectionStrings` is right? What is your `command` looks like exactly when you add parameter values? – Soner Gönül Oct 18 '14 at 21:24
  • Another victim of DataDirectory and Copy Always settings? [Take a look here](http://stackoverflow.com/questions/17147249/why-dont-changes-to-database-save/17147460#17147460) – Steve Oct 18 '14 at 21:36
  • Connection.ConnectionStringGADatabase is missing a . – paparazzo Oct 19 '14 at 12:18

1 Answers1

1

First of all if you have copied the app.config file, then you have an error in it... you are missing the opening bracket - apart from that the config looks fine to me.

Second: Try to put symbol @ before "insert into..." so it will look like this:

command.CommandText = @"INSERT INTO GAStatistics (GAType, Generations, PopulationSize, MaxFitness, AverageFitness, DmaxValue, Selection, CrossOver, FilePath) VALUES (@GAType, @Generations, @PopulationSize, @MaxFitness, @AverageFitness, @DmaxValue, @Selection, @CrossOver, @FilePath)";
Eru
  • 332
  • 3
  • 17