bind dropdown field in GridView EditItemTemplate?

G

Guest

I have two tables I'm editing in a Gridview. The VANS table contains a key to
the other LESSOR table. I would like to use a dropdown list to select the
LessorId value while displaying the Lessor table's "Company" field as text.
When I commit the edit I get error:Must declare the scalar variable
"@LessorId".

Also as a bonus question ;) I don't know how to set the selectedindex value
in the dropdown list based on the LessorId value in the Vans table.

Any ideas would be appreciated!

Thanks.

Here's my templatefield:
<asp:TemplateField HeaderText="Company" >
<ItemTemplate>
<asp:literal ID="Literal1" runat="server" Text='<%# Eval("Company") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlCompany" runat="server"
DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>

Gridview's SqlDataSource:
<asp:SqlDataSource ID="sdsVans" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:myDB %>"
SelectCommand="SELECT v.VanNo, v.LessorId, v.Status, v.Active,
l.Company, l.Account FROM dbo.Van v join dbo.Lessors l ON
v.LessorId=l.LessorId order by VanNo"
UpdateCommand="Update dbo.Vans set VanNo=@VanNo, LessorId=@LessorId,
Status=@Status,Active=@Active where LessorId=@LessorId" >
</asp:SqlDataSource>

DropDownList's SqlDataSource:
<asp:SqlDataSource ID="sdsLessors" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:myDB %>"
SelectCommand="SELECT LessorId, Company, Account FROM dbo.Lessors
order by Company">
</asp:SqlDataSource>
 
G

Guest

Super Green! Thanks!

Phillip Williams said:
The GridView will pass the LessorID as a parameter (in the NewValues
collection) if you create a 2-way databind on the dropdownlist:

<asp:DropDownList ID="ddlCompany" runat="server"
DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId"
SelectedValue='<%# Bind("LessorID") %>'>
</asp:DropDownList>

In addition, for good programming style, you should define an
<UpdateParameters> collection within the markup of the SqlDatasource that
updates the GridView.

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 
G

Guest

You solution worked but in an effort to practice "good programming" I tried
to add InsertParameters. I'm a little puzzled here.. I tried creating
InsertParameter for my InsertCommand for the drop down list as in:
InsertCommand="INSERT INTO dbo.Vans(... VendorCode) VALUES (... @VendorCode)">

<InsertParameters>
<asp:ControlParameter ControlID="VendorsDropDownList" Name="VendorCode"
PropertyName="SelectedValue" />
</InsertParameters>
but when I try and insert a row I get an error:
cannot find control "VendorsDropDownList" in controlparameter "VendorCode"
Just would like to understand this binding better.

Thanks for listening ;)
 
G

Guest

What is the VendorsDropDownList? How are you using it? In your earlier
question you only explained a dropdownlist named ddlCompany?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top