I have two questions about the code below that where I'm using the Dataset
to copy the database to the memory.
First question: I wonder why I can't use this code for the connection:
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DALLayer.Properties.Settings.AnimalMotelConnectionString"].ConnectionString);
I get this error message:
Object reference not set to an instance of an object.
Please explain what I have missed. I use the name string in the app.config. Now my connection is hardcoded like in the code below and that works.
Second question: When I use the dataset, I guess I can only read and do things within the sql command that I have. When I want to change, add or delete, I have to use another sql command and a new dataset?
public void Test()
{
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet dataset = new DataSet();
string connectionsString = null;
string sql = null;
connectionsString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\AnimalMotel.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
sql = "SELECT * From Guests";
connection = new SqlConnection(connectionsString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(dataset);
adapter.Dispose();
command.Dispose();
connection.Close();
MessageBox.Show(Convert.ToString(dataset.Tables[0].Rows[1].ItemArray[1]), "This is only a test!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch
{
throw;
}
}