C# Insert new record to dataset with sql
i have spent the few days trying to get this to work and checked a tonne of tutorials but it still won't work. i get go build errors and it works perfectly, but the new row isn't loading into the dataset to be saved to the database. the data shows up in my datagridview but isnt updating the dataset.
im using a sql express 2008 database
im average at programming though im new to c# and this is just wrapping my head around could some one either point me in the right direction or tell me what ive done wrong. thanks for the help.
private void button1_Click(object sender, EventArgs e)
{
string AccountName, Address, PhoneNumber, Suburb, Email, PostcodeInput;
int Postcode;
bool Parsed;
AccountName = textBox1.Text;
Address = textBox2.Text;
PhoneNumber = textBox5.Text;
Suburb = textBox3.Text;
PostcodeInput = postcodeTextBox.Text;
Email = textBox6.Text;
ToolsPlusDataSet ToolsDS = new ToolsPlusDataSet();
Parsed = Int32.TryParse(PostcodeInput, out Postcode);
if (!Parsed)
{
开发者_运维百科 MessageBox.Show("Postcode was Invalid - Must be a numerical value");
NewAccount newaccount = new NewAccount();
newaccount.Show();
this.Close();
}
ToolsPlusDataSet.AccountDetailsRow newAccountDetail;
newAccountDetail = ToolsDS.AccountDetails.NewAccountDetailsRow();
newAccountDetail.AccountName = AccountName;
newAccountDetail.Address = Address;
newAccountDetail.PhoneNumber = PhoneNumber;
newAccountDetail.Suburb = Suburb;
newAccountDetail.Postcode = Postcode;
newAccountDetail.Email = Email;
ToolsDS.AccountDetails.Rows.Add(newAccountDetail);
this.accountDetailsTableAdapter.Update(ToolsDS.AccountDetails);
this.Close();
}
I wrote this article for SQL Express, with the use of parametric query, if interested you can adapt it to your application.
http://code.msdn.microsoft.com/Esempio-applicazione-dati-494c129a
Regards.
精彩评论