467,910 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,910 developers. It's quick & easy.

Problem with FormView and DropDownLists

I'm having a problem with formviews and DropDownLists in ASP.NET 2.0. I'm
using a formview to insert an order into a database. Part of the order is the
customer. Currently, I have a dropdownlist bound to a datasource that simply
selects all customers from a database table. The DropDownList's Selectedvalue
is bound with the following expression:

Bind("customerId")

This works perfectly, the id is passed to a stored procedure and the order
is insterted. However, rather than provide a list of all customers, I'd like
to provide a dropdownlist of companies. If the user selects one of the
companies, the page posts back (the lists autopostback is set to true), and
then the dropdownlist for the customers should show only the customers
related to that company.

I added a clause to the customer datasource that ensures it only selects
customers related to the currently selected company. (It uses a control
parameter that refers to the company dropdownlist's selected value.) However,
when I run the page, and try to select a company, Visual studio gives me the
following message:

"There is no source code available for the current location." Followed by an
"OK" and a "Show Disassembly" button. When I press OK, I get an
"InvalidOperationException was unhandled by user code" message, informing me
that "Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control."

I have no idea where I'm going wrong. Does anybody have a clue?

Here's the code from the InsertItemTemplate:

<asp:DropDownList ID="CompanyDropDownList" runat="server"
AutoPostBack="True" DataSourceID="CompanySqlDataSource"
DataTextField="companyName" DataValueField="companyId">
</asp:DropDownList>
<asp:SqlDataSource ID="CompanySqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ordersystemConnectionString %>"
SelectCommand="SELECT [companyId], [companyName] FROM
[company]">
</asp:SqlDataSource>

<asp:DropDownList ID="CustomerDropDownList" runat="server"
DataSourceID="CustomerSqlDataSource"
DataTextField="lastName"
DataValueField="personId" SelectedValue='<%# Bind("customerId") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="CustomerSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ordersystemConnectionString %>"
SelectCommand="SELECT [lastName],
[personId], [companyId] FROM [person] WHERE ([companyId] = @companyId)">
<SelectParameters>
<asp:ControlParameter ControlID="CompanyDropDownList"
Name="companyId" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>


This is the InsertParameter for the FormView's datasource:

<InsertParameters>
<asp:Parameter Name="customerId" Type="Int32" />
<InsertParameters>

Apr 10 '06 #1
  • viewed: 1844
Share:
3 Replies

http://www.webswapp.com/codesamples/...s/default.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Bas Paap" wrote:
I'm having a problem with formviews and DropDownLists in ASP.NET 2.0. I'm
using a formview to insert an order into a database. Part of the order is the
customer. Currently, I have a dropdownlist bound to a datasource that simply
selects all customers from a database table. The DropDownList's Selectedvalue
is bound with the following expression:

Bind("customerId")

This works perfectly, the id is passed to a stored procedure and the order
is insterted. However, rather than provide a list of all customers, I'd like
to provide a dropdownlist of companies. If the user selects one of the
companies, the page posts back (the lists autopostback is set to true), and
then the dropdownlist for the customers should show only the customers
related to that company.

I added a clause to the customer datasource that ensures it only selects
customers related to the currently selected company. (It uses a control
parameter that refers to the company dropdownlist's selected value.) However,
when I run the page, and try to select a company, Visual studio gives me the
following message:

"There is no source code available for the current location." Followed by an
"OK" and a "Show Disassembly" button. When I press OK, I get an
"InvalidOperationException was unhandled by user code" message, informing me
that "Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control."

I have no idea where I'm going wrong. Does anybody have a clue?

Here's the code from the InsertItemTemplate:

<asp:DropDownList ID="CompanyDropDownList" runat="server"
AutoPostBack="True" DataSourceID="CompanySqlDataSource"
DataTextField="companyName" DataValueField="companyId">
</asp:DropDownList>
<asp:SqlDataSource ID="CompanySqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ordersystemConnectionString %>"
SelectCommand="SELECT [companyId], [companyName] FROM
[company]">
</asp:SqlDataSource>

<asp:DropDownList ID="CustomerDropDownList" runat="server"
DataSourceID="CustomerSqlDataSource"
DataTextField="lastName"
DataValueField="personId" SelectedValue='<%# Bind("customerId") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="CustomerSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ordersystemConnectionString %>"
SelectCommand="SELECT [lastName],
[personId], [companyId] FROM [person] WHERE ([companyId] = @companyId)">
<SelectParameters>
<asp:ControlParameter ControlID="CompanyDropDownList"
Name="companyId" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>


This is the InsertParameter for the FormView's datasource:

<InsertParameters>
<asp:Parameter Name="customerId" Type="Int32" />
<InsertParameters>

Apr 10 '06 #2
Excellent! That worked perfectly. Such a simple solution, too.

I never would've guessed that the formview wouldn't be re-bound, but
automatically recreated from the ViewState. Wouldn't it be possible to force
the formview to rebind rather than have it recreated from the ViewState? That
way you could, (I'm just guessing here), leave the dropdownlist's 2-way
databinding intact and automated.

Anyway, this works perfectly. Many thanks, you just saved my vacation!

"Phillip Williams" wrote:

http://www.webswapp.com/codesamples/...s/default.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Bas Paap" wrote:
I'm having a problem with formviews and DropDownLists in ASP.NET 2.0. I'm
using a formview to insert an order into a database. Part of the order is the
customer. Currently, I have a dropdownlist bound to a datasource that simply
selects all customers from a database table. The DropDownList's Selectedvalue
is bound with the following expression:

Bind("customerId")

This works perfectly, the id is passed to a stored procedure and the order
is insterted. However, rather than provide a list of all customers, I'd like
to provide a dropdownlist of companies. If the user selects one of the
companies, the page posts back (the lists autopostback is set to true), and
then the dropdownlist for the customers should show only the customers
related to that company.

I added a clause to the customer datasource that ensures it only selects
customers related to the currently selected company. (It uses a control
parameter that refers to the company dropdownlist's selected value.) However,
when I run the page, and try to select a company, Visual studio gives me the
following message:

"There is no source code available for the current location." Followed by an
"OK" and a "Show Disassembly" button. When I press OK, I get an
"InvalidOperationException was unhandled by user code" message, informing me
that "Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control."

I have no idea where I'm going wrong. Does anybody have a clue?

Here's the code from the InsertItemTemplate:

<asp:DropDownList ID="CompanyDropDownList" runat="server"
AutoPostBack="True" DataSourceID="CompanySqlDataSource"
DataTextField="companyName" DataValueField="companyId">
</asp:DropDownList>
<asp:SqlDataSource ID="CompanySqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ordersystemConnectionString %>"
SelectCommand="SELECT [companyId], [companyName] FROM
[company]">
</asp:SqlDataSource>

<asp:DropDownList ID="CustomerDropDownList" runat="server"
DataSourceID="CustomerSqlDataSource"
DataTextField="lastName"
DataValueField="personId" SelectedValue='<%# Bind("customerId") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="CustomerSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ordersystemConnectionString %>"
SelectCommand="SELECT [lastName],
[personId], [companyId] FROM [person] WHERE ([companyId] = @companyId)">
<SelectParameters>
<asp:ControlParameter ControlID="CompanyDropDownList"
Name="companyId" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>


This is the InsertParameter for the FormView's datasource:

<InsertParameters>
<asp:Parameter Name="customerId" Type="Int32" />
<InsertParameters>

Apr 11 '06 #3
If you re-bind the FormView then you would reset the selection for the
customers dropdownlist that was supposed to set the next list's selection.

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Bas Paap" wrote:
Excellent! That worked perfectly. Such a simple solution, too.

I never would've guessed that the formview wouldn't be re-bound, but
automatically recreated from the ViewState. Wouldn't it be possible to force
the formview to rebind rather than have it recreated from the ViewState? That
way you could, (I'm just guessing here), leave the dropdownlist's 2-way
databinding intact and automated.

Anyway, this works perfectly. Many thanks, you just saved my vacation!

"Phillip Williams" wrote:

http://www.webswapp.com/codesamples/...s/default.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Bas Paap" wrote:
I'm having a problem with formviews and DropDownLists in ASP.NET 2.0. I'm
using a formview to insert an order into a database. Part of the order is the
customer. Currently, I have a dropdownlist bound to a datasource that simply
selects all customers from a database table. The DropDownList's Selectedvalue
is bound with the following expression:

Bind("customerId")

This works perfectly, the id is passed to a stored procedure and the order
is insterted. However, rather than provide a list of all customers, I'd like
to provide a dropdownlist of companies. If the user selects one of the
companies, the page posts back (the lists autopostback is set to true), and
then the dropdownlist for the customers should show only the customers
related to that company.

I added a clause to the customer datasource that ensures it only selects
customers related to the currently selected company. (It uses a control
parameter that refers to the company dropdownlist's selected value.) However,
when I run the page, and try to select a company, Visual studio gives me the
following message:

"There is no source code available for the current location." Followed by an
"OK" and a "Show Disassembly" button. When I press OK, I get an
"InvalidOperationException was unhandled by user code" message, informing me
that "Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control."

I have no idea where I'm going wrong. Does anybody have a clue?

Here's the code from the InsertItemTemplate:

<asp:DropDownList ID="CompanyDropDownList" runat="server"
AutoPostBack="True" DataSourceID="CompanySqlDataSource"
DataTextField="companyName" DataValueField="companyId">
</asp:DropDownList>
<asp:SqlDataSource ID="CompanySqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ordersystemConnectionString %>"
SelectCommand="SELECT [companyId], [companyName] FROM
[company]">
</asp:SqlDataSource>

<asp:DropDownList ID="CustomerDropDownList" runat="server"
DataSourceID="CustomerSqlDataSource"
DataTextField="lastName"
DataValueField="personId" SelectedValue='<%# Bind("customerId") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="CustomerSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ordersystemConnectionString %>"
SelectCommand="SELECT [lastName],
[personId], [companyId] FROM [person] WHERE ([companyId] = @companyId)">
<SelectParameters>
<asp:ControlParameter ControlID="CompanyDropDownList"
Name="companyId" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>


This is the InsertParameter for the FormView's datasource:

<InsertParameters>
<asp:Parameter Name="customerId" Type="Int32" />
<InsertParameters>

Apr 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Larry R | last post: by
4 posts views Thread by Brad Baker | last post: by
reply views Thread by =?Utf-8?B?TWFyaw==?= | last post: by
5 posts views Thread by Mark Olbert | last post: by
1 post views Thread by shapper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.