I'm using the following code to store data from form inputs into a database.
The code seems to execute successfully and the message box states that one row has been affected (as expected).
When I look at the data sources window within visual studio there are no new rows saved within the data set.
Where are the new rows being saved to? How do I commit them to the database?
Thanks in advance.
private void btnSavePublication_Click(object sender, EventArgs e)
{
// get the inputs
int pubID = Convert.ToInt32(txtPubID.Text);
string pubTitle = txtTitle.Text;
long pubBarcode = Convert.ToInt64(txtBarcode.Text);
string pubDate = txtRecievedDate.Text;
decimal pubPrice = Convert.ToDecimal(txtPrice.Text);
// retrieve the connection string
string myConnectionString = Properties.Settings.Default.Database1ConnectionString;
// open the connection to the database
SqlConnection connection = new SqlConnection();
connection.ConnectionString = myConnectionString;
// create a SQL command to update the table
SqlCommand command = new SqlCommand();
command.Connection = connection;
// set up the paramaters
command.Parameters.AddWithValue("@PublicationID", pubID);
command.Parameters.AddWithValue("@PublicationTitle", pubTitle);
command.Parameters.AddWithValue("@PublicationBarcode", pubBarcode);
command.Parameters.AddWithValue("@PublicationDate", pubDate);// TODO parse the date
command.Parameters.AddWithValue("@PublicationPrice",pubPrice);
// create the SQL query
command.CommandText = "INSERT INTO Publication (PublicationID, PublicationTitle, " +
"PublicationBarcode, PublicationDate, "+
"PublicationPrice)" +
"VALUES ( @PublicationID, @PublicationTitle, @PublicationBarcode, " +
"@PublicationDate, @PublicationPrice)";
// run the query (hopefully)
try {
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
MessageBox.Show( rowsAffected + " rows affected");
}
catch {
MessageBox.Show("Wakka wakka");
}
finally {
connection.Close();
}
}
edit : This is my connection string -
"Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integr" +
"ated Security=True;Connect Timeout=30"