Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > bind dropdown field in GridView EditItemTemplate?

Reply
Thread Tools

bind dropdown field in GridView EditItemTemplate?

 
 
=?Utf-8?B?RGFiYmxlcg==?=
Guest
Posts: n/a
 
      03-21-2006
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>
<aspropDownList ID="ddlCompany" runat="server"
DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId">
</aspropDownList>
</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>
 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGFiYmxlcg==?=
Guest
Posts: n/a
 
      03-21-2006
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:
>
> <aspropDownList ID="ddlCompany" runat="server"
> DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId"
> SelectedValue='<%# Bind("LessorID") %>'>
> </aspropDownList>
>
> 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>
> > <aspropDownList ID="ddlCompany" runat="server"
> > DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId">
> > </aspropDownList>
> > </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>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGFiYmxlcg==?=
Guest
Posts: n/a
 
      03-22-2006
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:
>
> <aspropDownList ID="ddlCompany" runat="server"
> DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId"
> SelectedValue='<%# Bind("LessorID") %>'>
> </aspropDownList>
>
> 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>
> > <aspropDownList ID="ddlCompany" runat="server"
> > DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId">
> > </aspropDownList>
> > </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>

 
Reply With Quote
 
=?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      03-22-2006
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:
> >
> > <aspropDownList ID="ddlCompany" runat="server"
> > DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId"
> > SelectedValue='<%# Bind("LessorID") %>'>
> > </aspropDownList>
> >
> > 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>
> > > <aspropDownList ID="ddlCompany" runat="server"
> > > DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId">
> > > </aspropDownList>
> > > </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>

 
Reply With Quote
 
=?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      03-23-2006
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/lib...51(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:
> >
> > <aspropDownList ID="ddlCompany" runat="server"
> > DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId"
> > SelectedValue='<%# Bind("LessorID") %>'>
> > </aspropDownList>
> >
> > 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>
> > > <aspropDownList ID="ddlCompany" runat="server"
> > > DataSourceID="sdsLessors" DataTextField="Company" DataValueField="LessorId">
> > > </aspropDownList>
> > > </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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to access and bind a label inside a GridView Template Field? Miguel Dias Moura ASP .Net 2 04-14-2011 04:06 AM
1 Gridview. Dropdown A is column from database, Dropdown B is column from database, Data in A and B must be from same row. anonymoushamster@gmail.com ASP .Net 2 11-07-2007 12:40 PM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
RE: bind dropdown field in GridView EditItemTemplate? =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?= ASP .Net 0 03-21-2006 06:40 PM



Advertisments