views:

33

answers:

2

Here is my code. In the debugger, I can see that the code is running. No errors are thrown. But, when I go back to the table, no row has been inserted. What am I missing??

protected void submitButton_Click(object sender, EventArgs e)
{
    CfdDataClassesDataContext db = new CfdDataClassesDataContext();

    string sOfficeSought = officesSoughtDropDownList.SelectedValue;
    int iOfficeSought;
    Int32.TryParse(sOfficeSought, out iOfficeSought);

    Account act = new Account()
    {
        FirstName = firstNameTextBox.Text,
        MiddleName = middleNamelTextBox.Text,
        LastName = lastNameTextBox.Text,
        Suffix = suffixTextBox.Text,
        CampaignName = campaignNameTextBox.Text,
        Address1 = address1TextBox.Text,
        Address2 = address2TextBox.Text,
        TownCity = townCityTextBox.Text,
        State = stateTextBox.Text,
        ZipCode = zipTextBox.Text,
        Phone = phoneTextBox.Text,
        Fax = faxTextBox.Text,
        PartyAffiliation = partyAfilliatinoTextBox.Text,
        EmailAddress = emailTextBox.Text,
        BankName = bankNameTextBox.Text,
        BankMailingAddress = bankAddressTextBox.Text,
        BankTownCity = bankTownCityTextBox.Text,
        BankState = bankStateTextBox.Text,
        BankZip = bankZipTextBox.Text,
        TreasurerFirstName = treasurerFirstNameTextBox.Text,
        TreasurerMiddleName = treasurerMiddleNamelTextBox.Text,
        TreasurerLastName = treasurerLastNameTextBox.Text,
        TreasurerMailingAddress = treasurerMailingAddressTextBox.Text,
        TreasurerTownCity = treasurerTownCityTextBox.Text,
        TreasurerState = treasurerStateTextBox.Text,
        TreasurerZipCode = treasurerZipTextBox.Text,
        TreasurerPhone = treasurerPhoneTextBox.Text
        //OfficeSought = iOfficeSought
    };
    act.Suffix = suffixTextBox.Text;

    db.SubmitChanges();
}
+5  A: 

Before you call db.SubmitChanges() you also need to have a line like (depending on what the table name is):

db.Accounts.InsertOnSubmit(act);
Justin Niessner
A: 

You are never adding the newly created object to the data context.

In general you would call something like:

Db.AddtoAccounts( act );  

That syntax is for EF, but the concept is the same.

You have allocated a new object, but never told the database context that you want to use it.

Then call submit changes.

Jason Short