bind dropdown field in GridView EditItemTemplate?

Discussion in 'ASP .Net' started by =?Utf-8?B?RGFiYmxlcg==?=, Mar 21, 2006.

  1. 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>
     
    =?Utf-8?B?RGFiYmxlcg==?=, Mar 21, 2006
    #1
    1. Advertising

  2. Super Green! Thanks!

    "Phillip Williams" wrote:

    > 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
    >
    >
    > "Dabbler" wrote:
    >
    > > 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>
     
    =?Utf-8?B?RGFiYmxlcg==?=, Mar 21, 2006
    #2
    1. Advertising

  3. 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 ;)

    "Phillip Williams" wrote:

    > 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
    >
    >
    > "Dabbler" wrote:
    >
    > > 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>
     
    =?Utf-8?B?RGFiYmxlcg==?=, Mar 22, 2006
    #3
  4. What is the VendorsDropDownList? How are you using it? In your earlier
    question you only explained a dropdownlist named ddlCompany?
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Dabbler" wrote:

    > 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 ;)
    >
    > "Phillip Williams" wrote:
    >
    > > 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
    > >
    > >
    > > "Dabbler" wrote:
    > >
    > > > 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>
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Mar 22, 2006
    #4
  5. If you still have not solved this issue, review this article to an indepth
    explanation of how the datasource creates paramters for databound fields:
    http://msdn2.microsoft.com/en-us/library/ms228051(VS.80).aspx
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Dabbler" wrote:

    > 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 ;)
    >
    > "Phillip Williams" wrote:
    >
    > > 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
    > >
    > >
    > > "Dabbler" wrote:
    > >
    > > > 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>
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Mar 23, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vishnu
    Replies:
    1
    Views:
    1,214
    Patrick.O.Ige
    Mar 25, 2006
  2. =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=

    RE: bind dropdown field in GridView EditItemTemplate?

    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Mar 21, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    9,760
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Mar 21, 2006
  3. Replies:
    0
    Views:
    947
  4. sjmyers
    Replies:
    0
    Views:
    1,757
    sjmyers
    Mar 27, 2007
  5. Brian Greiwe

    Can't get to nested dropdown in edititemtemplate column

    Brian Greiwe, Sep 16, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    157
    Brian Greiwe
    Sep 16, 2003
Loading...

Share This Page