none
Noob Address Book part 2 – Inserting an address goes badly. RRS feed

  • Question

  • A more terse explanation of the problem is here:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3047184&SiteID=1

     

     

    I have a couple of questions for this knowledgeable forum.
    1. I assume that since I added a ‘Linq to SQL classes” item to my project and can use a Linq query to populate the address book form which I can cycle thru, that I am indeed using “Linq to SQL classes’. My question is that is it proper to fill my address class with a new address object and then submit that as the insert? Or is there a Linq syntax that will directly insert a row into the table?
    2. Does the DataContext object automatically update or must it always be done manually somehow after a successful insert. In other words when I peruse my address book form after an insert should the newly inserted item be shown there?

    How I Load The Data
    public partial class MainWindow : Window
    {
    ICollectionView cvs;
    ...
    private void Window_Loaded( object sender, RoutedEventArgs e )
    {
    AddressesLinqDataContext dc = new AddressesLinqDataContext();
    var query = from s in dc.Addresses orderby s.State select s;
    cvs = CollectionViewSource.GetDefaultView(query);
    DataContext = cvs;
    }

    Like I said this seems to do it’s job and I can cycle through my boring address table.

     

    This is how I am attempting an insert which I put in a second window.

    private void btnSubmit_Click( object sender, RoutedEventArgs e )

    {

      string firstName = FirstNameDTKey.Text;

      string lastName = LastNameDTKey.Text;

      string address1 = Address1DTKey.Text;

      ...

      Address address = new Address(firstName, lastName, address1, address2, city, state);

      AddressesLinqDataContext dc = new AddressesLinqDataContext();

      dc.Addresses.InsertOnSubmit(address);

      System.Data.Linq.ChangeSet changeSet = dc.GetChangeSet();

      // changeSet Tooltip states that there are {inserts 1, deletes 0, updates 0}

      dc.SubmitChanges();

    }

     

    Browsing the address form does not reflect the new entry.

    When I close the debugger and check the datatable there are no new entries.

    Thanks in advance for any help.

     

    I apologize for the horrendous formatting. I wanted to be cool and use a code snippet box but it seemed a little tedious and I got lazy. Sorry

     

    Note: I added a textwriter to record the datacontext log.

    TextWriter tw = new StreamWriter("SqlLog.dbg");

    dc.Log = tw;

     

    The results say:

    INSERT INTO [dbo].[Addresses]([FirstName], [LastName], [Address1], [Address2], [City], [State])
    VALUES (@p0, @p1, @p2, @p3, @p4, @p5)

    SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
    -- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [Null]
    -- @p1: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [Null]
    -- @p2: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [Null]
    -- @p3: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [Null]
    -- @p4: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [Null]
    -- @p5: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [Null]
    -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8

     

    I think it is saying all the parameters are null? It's hard for me to read.

    I have an ID identity column in the table which I assume will be updated automatically.

    Thank you in advance for your time.

    Friday, March 21, 2008 11:58 PM

Answers

  • They say noob for a reason.

    Boy was I ever wrong about how to do this thing.

    The magick of the dbml file has setters and getters for the new 'Linq to Sql class' so there was no point in making a Address.cs of my own, unless I guess to add further methods and logic. I suppose that's right, I'll do a test later. The new coding works fine now:

    private void btnSubmit_Click( object sender, RoutedEventArgs e )

    {

    AddressLinqDataContext dc = new AddressLinqDataContext();

    TextWriter tw = new StreamWriter("SqlLog.dbg");

    dc.Log = tw;

    Address address = new Address(); // This is the dbml magick address class created for me

    address.FirstName =FirstNameDTKey.Text; // Fill them all with my text fields.

    address.LastName =LastNameDTKey.Text;

    address.Address1 = Address1DTKey.Text;

    address.Address2 = Address2DTKey.Text;

    address.City = CityDTKey.Text;

    address.State = StateDTKey.Text;

    dc.Addresses.InsertOnSubmit(address);

    System.Data.Linq.ChangeSet changeSet = dc.GetChangeSet();

    try

    {

    dc.SubmitChanges(); // tada a new address row is created.

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.ToString());

    }

    finally

    { tw.Close(); }

    }

    One little misconception can lead to failure.
    Sunday, March 23, 2008 9:00 AM