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.