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.

9
  • 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? Commented Apr 16, 2016 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? Commented Apr 16, 2016 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? Commented Apr 16, 2016 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? Commented Apr 16, 2016 at 23:00
  • I see it. It says: Copy always Commented Apr 16, 2016 at 23:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.