views:

329

answers:

3

Take a simple example:

I have a customer table and an account table both with customerID field. I want to formview that can update data in both tables.

FormView:

First Name (Customer)
Last Name (Customer)
Address (Customer)
AccountRate (Account)

How can I do this with Linq to SQL? Do I just use Bind statements like Bind("Account.AccountRate")? Or do I have to do some magic in ItemUpdating event or similar?

+1  A: 

you will need to join

from c in db.Customer
join a in db.Account
on a.customerID equals c.customerID
select new Model {FirstName = c.firstName,LastName = c.lastName,Address = c.address,AccountRate = a.accountRate}

define you model as a class somewhere and bind to that.

Paul Creasey
Would I use an objectdatasource to bind to the formview then? Or can you do it with a linqdatasource?
User
+1  A: 

I'am never work with linq to SQL, but in Entity Framework i need to do something like this:

protected void ObjectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
        // Declaring the Entity context 
        AvalonEntities db = new AvalonEntities();

        // In updating méthod of a ObjectDataSource the "e" arg is my customer, so i grab this
        customer cus = (customer)e.InputParameters[0];


        // say that i have a DropDown to select the account ...
        // I Get de DropDown and get your SelectedValue.
        DropDownList ddlAccount  = (DropDownList)FormView1.FindControl("ddlAccount");
        // Whit the value i declare a new account
        account ac = new account () { id_account = int.Parse(ddlAccount.SelectedValue) };
        // and associate it to Customer
        cus.account = ac;

I hope this help

Ewerton
A: 

I've had some success retrieving properties from the linked class, like this:

First Name:
<asp:TextBox ID="FirstNameTextBox" runat="server"
 Text='<%# Bind("Entitycontact.Namefirst") %>' />
MAbraham1