Adding rows with linq trouble with reference table

Posted by Laurence Burke on Stack Overflow See other posts from Stack Overflow or by Laurence Burke
Published on 2010-03-12T13:40:22Z Indexed on 2010/03/12 13:47 UTC
Read the original article Hit count: 319

Filed under:
|

I am adding a new address and I know the structure as AddressID = PK and all other entities are non nullable. Now on insert of a new row the addrID Pk is autogened and I am wondering if I would have to get that to create a new row in the referencing table EmployeeAddress or does that automatically get generated also. also I want to be able to repopulate the dropdownlist that lists the current employee's addresses with the newly created address.

    static uint _curEmpID;

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtZip.Text != "" && txtAdd1.Text != "" && txtCity.Text != "")
        {
            TestDataClassDataContext dc = new TestDataClassDataContext();
            Address addr = new Address()
            {
                AddressLine1 = txtAdd1.Text,
                AddressLine2 = txtAdd2.Text,
                City = txtCity.Text,
                PostalCode = txtZip.Text,
                StateProvinceID = Convert.ToInt32(ddlState.SelectedValue)
            };
            dc.Addresses.InsertOnSubmit(addr);
            lblSuccess.Visible = true;
            lblErrMsg.Visible = false;
            dc.SubmitChanges();
     //
     //    TODO: insert new row in EmployeeAddress to reference CurEmp to newly created address
     //
            SetAddrList();
        }
        else
        {
            lblErrMsg.Text = "Invalid Input";
            lblErrMsg.Visible = true;
        }
    }

    protected void ddlAddList_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblErrMsg.Visible = false;
        lblSuccess.Visible = false;
        TestDataClassDataContext dc = new TestDataClassDataContext();
        dc.ObjectTrackingEnabled = false;
        if (ddlAddList.SelectedValue != "-1")
        {
            var addr = (from a in dc.Addresses
                        where a.AddressID == Convert.ToInt32(ddlAddList.SelectedValue)
                        select a).FirstOrDefault();
            txtAdd1.Text = addr.AddressLine1;
            txtAdd2.Text = addr.AddressLine2;
            txtCity.Text = addr.City;
            txtZip.Text = addr.PostalCode;
            ddlState.SelectedValue = addr.StateProvinceID.ToString();
            btnSubmit.Visible = true;
            btnAdd.Visible = false;
        }
        else
        {
            txtAdd1.Text = "";
            txtAdd2.Text = "";
            txtCity.Text = "";
            txtZip.Text = "";
            btnAdd.Visible = true;
            btnSubmit.Visible = false;
        }
    }

    protected void SetAddrList()
    {
        TestDataClassDataContext dc = new TestDataClassDataContext();
        dc.ObjectTrackingEnabled = false;

        var addList = from addr in dc.Addresses
                      from eaddr in dc.EmployeeAddresses
                      where eaddr.EmployeeID == _curEmpID && addr.AddressID == eaddr.AddressID
                      select new
                      {
                          AddValue = addr.AddressID,
                          AddText = addr.AddressID,
                      };
        ddlAddList.DataSource = addList;
        ddlAddList.DataValueField = "AddValue";
        ddlAddList.DataTextField = "AddText";
        ddlAddList.DataBind();
        ddlAddList.Items.Add(new ListItem("<Add Address>", "-1"));
    }

OK I am hoping that I did not include too much code. I would really appreciate any other comments about I could otherwise improve this code in any other ways also.

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql