Problem with FormView and DropDownLists

G

Guest

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:eek:rdersystemConnectionString %>"
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:eek:rdersystemConnectionString %>"
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>
 
G

Guest

http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Bas Paap said:
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:eek:rdersystemConnectionString %>"
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:eek:rdersystemConnectionString %>"
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>
 
G

Guest

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 said:
http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Bas Paap said:
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:eek:rdersystemConnectionString %>"
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:eek:rdersystemConnectionString %>"
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>
 
G

Guest

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 said:
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 said:
http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Bas Paap said:
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:eek:rdersystemConnectionString %>"
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:eek:rdersystemConnectionString %>"
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>
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top