0

This is my first time using Databases so maybe I am missing something but my Database is not being updated.

I have created a Database in Visual Studio named MainAreas with two columns: MainAreaId (primary key) and MainAreaName(string).

In my application the user adds, deletes and updates main areas and I want to save this information in the database so when the program is closed and opened again the main areas added previously are still there.

The design I´ve done is the following (this part may not be the more appropiate solution, I accept advice): I have created a class called DatabaseConnection which connects to the Database, and a class called RoomsDatabaseHandler which updates the database with the MainAreas added in the AreasUserControl (see attached diagram):Class diagram

At the moment I have implemented a function that adds a new row in the MainAreas Table of the database and a function that retrieves all the MainAreas in the MainAreas table when starting up the application. But this doesn´t work.

This is my DatabaseConnection class:

class DatabaseConnection
{
    private string sql_string;
    private string strCon;
    private System.Data.SqlClient.SqlDataAdapter da_1;

    public string Sql
    {
        set { sql_string = value; }
    }

    public string connection_string
    {
        set { strCon = value; }
    }

    public System.Data.DataSet GetConnection
    {
        get 
        { return MyDataSet();}
    }
    public void UpdateDatabase(System.Data.DataSet ds)
    {
        System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);
        cb.DataAdapter.Update(ds.Tables[0]);
    }

    private System.Data.DataSet MyDataSet()
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);

        da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
        System.Data.DataSet dat_set = new System.Data.DataSet();
        da_1.Fill(dat_set, "MainAreas");
        //da_1.Fill(dat_set);
        con.Close();

        return dat_set;

    }
}

This is my RoomsDatabaseHandler class:

public class RoomsDatabaseHandler
{
    DatabaseConnection objConnect;
    string conString;

    DataSet ds;
    DataRow dRow;

    public RoomsDatabaseHandler()
    {
        objConnect = new DatabaseConnection();
        conString = Properties.Settings.Default.MyDatabaseConnectionString;

        objConnect.connection_string = conString;
        objConnect.Sql = Properties.Settings.Default.SQL;

        ds = objConnect.GetConnection;
    }
    public void AddNewMainArea(string mainAreaName)
    {
        DataRow row = ds.Tables[0].NewRow();
        row[0] = ds.Tables[0].Rows.Count;
        row[1] = mainAreaName;

        ds.Tables[0].Rows.Add(row);

        try
        {
            objConnect.UpdateDatabase(ds);
            MessageBox.Show("Database updated", "OK",
                    MessageBoxButton.OK, MessageBoxImage.Information);
        }
        catch(Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }
    public List<string> LoadMainAreas()
    {
        List<string> mainAreasList = new List<string>();

        for (int it = 0; it < ds.Tables[0].Rows.Count; it++ )
        {
            dRow = ds.Tables[0].Rows[it];
            mainAreasList.Add(dRow.ItemArray.GetValue(1).ToString());
        }

        return mainAreasList;
    }
}

Could you please give me a hand on what´s wrong here? When the user adds a new main Area, the message box "Database updated" is shown but if I have a look to the MainAreas table data in the Server Explorer I cannot see any row added. And when I restart the application, and the LoadMainAreas method is called, the table looks empty.

Thanks in advance!! And sorry for the long text.

chincheta73
  • 187
  • 1
  • 22
  • First thing to check is the value of the ConnectionString used. Is the same connectionstring used in the ServerExplorer window? If not, of course, you are looking at two different database. The second thing to check is (if you have the database file listed between the project files) the value of the Copy To Output Directory of that file. Could you give us this informations? – Steve Apr 16 '16 at 22:35
  • Hi Steve. Thanks for your quick response. My ConnectionString is generated automatically by Visual Studio and is this one: Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True I don´t know tho, which connectionstring I am using in the ServerExplorer window. How can I know that? – chincheta73 Apr 16 '16 at 22:50
  • Regarding your second question Steve, The database is listed in the project files. But I am not understanding what you mean with the Copy To Output Directory. What´s this and how can I get it? – chincheta73 Apr 16 '16 at 22:52
  • If you click on the file and look at the properties window there is a property named Copy To Output Directory. What is the value? – Steve Apr 16 '16 at 23:00
  • I see it. It says: Copy always – chincheta73 Apr 16 '16 at 23:01
  • All right, everything matches the condition explained in [this answer](http://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails/17147460#17147460) of mine, so I think you could find your solution there. Let me know if you need clarifications on that answer – Steve Apr 16 '16 at 23:06
  • Thanks Steve. That solved the issue. I changed the Copy To Output Directory to Copy if Newer and now works fine. I have some further questions about "why this solved the problem". I don´t understand it 100% but I will ask it in the other thread, where you posted your answer. Thanks again. – chincheta73 Apr 16 '16 at 23:16
  • Sorry Steve, I cannot comment in the other thread (not enough reputation yet). My question is: changing this property to Copy if Newer solved the issue. But, is it realy solved or is it a walkaround? I mean, when I am going to build the program and create an installer which will be installed in a specified folder in the user computer, will this still work? – chincheta73 Apr 16 '16 at 23:23
  • The behavior of |DataDirectory| is coherent both in debug and in production mode, It resolves to the folder where the program runs. When you debug in Visual Studio the folder is BIN\DEBUG, on the customer PC is the same folder where you install the app. Anyway any decent deployment tool allows you to change the connectionstring to be what you want it to be. Also a fixed path from the ProgramData folder (as it should be). Which one is installed? The one in the Project Folder but also this depends on your choice oof the installation tool – Steve Apr 17 '16 at 07:22

0 Answers0