Home All Groups Group Topic Archive Search About

Dropdownlist: SelectedValue which is invalid

Author
5 Dec 2005 8:48 PM
Karl-Inge Reknes
I have a Formview with a dropdownlist. The dropdownlist is bind to the
formview datasourceobject. When I retrieve a value that is not in the
dropdownlist I got this error:
“'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value “
I have try this code, but helpless:
protected void DropDownList1_DataBinding(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
ddl.ClearSelection();
//The string s held the value that not is in the dropdownlist.
ListItem li = ddl.Items.FindByValue(s);
if (lm == null)
{
    dl.SelectedIndex = 0;
}
else
{
   lm.Selected = true;
}
}

Can anybody help me??

Karl-Inge

Author
5 Dec 2005 9:39 PM
Phillip Williams
Hi Karl-Inge,

You would need to access the DataItem of the NamingContainer(the FormView),
e.g.
//assuming that the second value in your datasource is the one databound to
the
string strContainerValue =
((DataRowView)((FormView)((DropDownList)sender).NameingContainer).DataItem)[1];   
//Then you can set the selectedItem of the dropdownlist to match this value

Show quoteHide quote
"Karl-Inge Reknes" wrote:

> I have a Formview with a dropdownlist. The dropdownlist is bind to the
> formview datasourceobject. When I retrieve a value that is not in the
> dropdownlist I got this error:
> “'DropDownList1' has a SelectedValue which is invalid because it does not
> exist in the list of items.
> Parameter name: value “
> I have try this code, but helpless:
> protected void DropDownList1_DataBinding(object sender, EventArgs e)
> {
> DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
> ddl.ClearSelection();
> //The string s held the value that not is in the dropdownlist.
> ListItem li = ddl.Items.FindByValue(s);
> if (lm == null)
> {
>     dl.SelectedIndex = 0;
> }
> else
> {
>    lm.Selected = true;
> }
> }
>
> Can anybody help me??
>
> Karl-Inge
>
Author
6 Dec 2005 10:21 AM
Karl-Inge Reknes
Hi Phillip

Thanks for your help, but it don’t solve my problem.  Her is my code:

protected void DropDownList1_DataBinding(object sender, EventArgs e)
{
string customerStatus =   
(Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus;

DropDownList dl =
(DropDownList)((FormView)((DropDownList)sender).NamingContainer).FindControl("DropDownList1");
dl.ClearSelection();
ListItem lm = dl.Items.FindByValue(customerStatus);
if (lm == null)
{
  dl.SelectedIndex = 0;
}
else
{
  lm.Selected = true;
}
}

When customerStatus is in the DropDownList1 I got this error:

The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

When customerStatus is not in the DropDownList I got this error:
'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value
I hope you can help me?

Karl-Inge


Show quoteHide quote
"Phillip Williams" wrote:

> Hi Karl-Inge,
>
> You would need to access the DataItem of the NamingContainer(the FormView),
> e.g.
> //assuming that the second value in your datasource is the one databound to
> the
> string strContainerValue =
> ((DataRowView)((FormView)((DropDownList)sender).NameingContainer).DataItem)[1];   
> //Then you can set the selectedItem of the dropdownlist to match this value
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Karl-Inge Reknes" wrote:
>
> > I have a Formview with a dropdownlist. The dropdownlist is bind to the
> > formview datasourceobject. When I retrieve a value that is not in the
> > dropdownlist I got this error:
> > “'DropDownList1' has a SelectedValue which is invalid because it does not
> > exist in the list of items.
> > Parameter name: value “
> > I have try this code, but helpless:
> > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > {
> > DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
> > ddl.ClearSelection();
> > //The string s held the value that not is in the dropdownlist.
> > ListItem li = ddl.Items.FindByValue(s);
> > if (lm == null)
> > {
> >     dl.SelectedIndex = 0;
> > }
> > else
> > {
> >    lm.Selected = true;
> > }
> > }
> >
> > Can anybody help me??
> >
> > Karl-Inge
> >
Author
6 Dec 2005 3:03 PM
Phillip Williams
Hi Karl-Inge,

I think that you can solve the problem by adding the value to the
dropdownlist items if it is not found, e.g.

if (lm == null)
{
  dl.Items.Add (new ListItem(customerStatus, customerStatus));
}
else { //do nothing here, let the normal Bind works }
Show quoteHide quote
"Karl-Inge Reknes" wrote:

> Hi Phillip
>
> Thanks for your help, but it don’t solve my problem.  Her is my code:
>
> protected void DropDownList1_DataBinding(object sender, EventArgs e)
> {
> string customerStatus =   
> (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus;
>
> DropDownList dl =
> (DropDownList)((FormView)((DropDownList)sender).NamingContainer).FindControl("DropDownList1");
> dl.ClearSelection();
> ListItem lm = dl.Items.FindByValue(customerStatus);
> if (lm == null)
> {
>   dl.SelectedIndex = 0;
> }
> else
> {
>   lm.Selected = true;
> }
> }
>
> When customerStatus is in the DropDownList1 I got this error:
>
> The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive
>
> When customerStatus is not in the DropDownList I got this error:
> 'DropDownList1' has a SelectedValue which is invalid because it does not
> exist in the list of items.
> Parameter name: value
> I hope you can help me?
>
> Karl-Inge
>
>
> "Phillip Williams" wrote:
>
> > Hi Karl-Inge,
> >
> > You would need to access the DataItem of the NamingContainer(the FormView),
> > e.g.
> > //assuming that the second value in your datasource is the one databound to
> > the
> > string strContainerValue =
> > ((DataRowView)((FormView)((DropDownList)sender).NameingContainer).DataItem)[1];   
> > //Then you can set the selectedItem of the dropdownlist to match this value
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Karl-Inge Reknes" wrote:
> >
> > > I have a Formview with a dropdownlist. The dropdownlist is bind to the
> > > formview datasourceobject. When I retrieve a value that is not in the
> > > dropdownlist I got this error:
> > > “'DropDownList1' has a SelectedValue which is invalid because it does not
> > > exist in the list of items.
> > > Parameter name: value “
> > > I have try this code, but helpless:
> > > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > > {
> > > DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
> > > ddl.ClearSelection();
> > > //The string s held the value that not is in the dropdownlist.
> > > ListItem li = ddl.Items.FindByValue(s);
> > > if (lm == null)
> > > {
> > >     dl.SelectedIndex = 0;
> > > }
> > > else
> > > {
> > >    lm.Selected = true;
> > > }
> > > }
> > >
> > > Can anybody help me??
> > >
> > > Karl-Inge
> > >
Author
6 Dec 2005 5:49 PM
Phillip Williams
You can also blank out the source data if the selection is not found, e.g.




if (lm == null)
{ (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus="";
}
else { //do nothing here, let the normal Bind works }

Show quoteHide quote
"Phillip Williams" wrote:

> Hi Karl-Inge,
>
> I think that you can solve the problem by adding the value to the
> dropdownlist items if it is not found, e.g.
>
> if (lm == null)
> {
>   dl.Items.Add (new ListItem(customerStatus, customerStatus));
> }
> else { //do nothing here, let the normal Bind works }
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Karl-Inge Reknes" wrote:
>
> > Hi Phillip
> >
> > Thanks for your help, but it don’t solve my problem.  Her is my code:
> >
> > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > {
> > string customerStatus =   
> > (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus;
> >
> > DropDownList dl =
> > (DropDownList)((FormView)((DropDownList)sender).NamingContainer).FindControl("DropDownList1");
> > dl.ClearSelection();
> > ListItem lm = dl.Items.FindByValue(customerStatus);
> > if (lm == null)
> > {
> >   dl.SelectedIndex = 0;
> > }
> > else
> > {
> >   lm.Selected = true;
> > }
> > }
> >
> > When customerStatus is in the DropDownList1 I got this error:
> >
> > The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive
> >
> > When customerStatus is not in the DropDownList I got this error:
> > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > exist in the list of items.
> > Parameter name: value
> > I hope you can help me?
> >
> > Karl-Inge
> >
> >
> > "Phillip Williams" wrote:
> >
> > > Hi Karl-Inge,
> > >
> > > You would need to access the DataItem of the NamingContainer(the FormView),
> > > e.g.
> > > //assuming that the second value in your datasource is the one databound to
> > > the
> > > string strContainerValue =
> > > ((DataRowView)((FormView)((DropDownList)sender).NameingContainer).DataItem)[1];   
> > > //Then you can set the selectedItem of the dropdownlist to match this value
> > >
> > > --
> > > HTH,
> > > Phillip Williams
> > > http://www.societopia.net
> > > http://www.webswapp.com
> > >
> > >
> > > "Karl-Inge Reknes" wrote:
> > >
> > > > I have a Formview with a dropdownlist. The dropdownlist is bind to the
> > > > formview datasourceobject. When I retrieve a value that is not in the
> > > > dropdownlist I got this error:
> > > > “'DropDownList1' has a SelectedValue which is invalid because it does not
> > > > exist in the list of items.
> > > > Parameter name: value “
> > > > I have try this code, but helpless:
> > > > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > > > {
> > > > DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
> > > > ddl.ClearSelection();
> > > > //The string s held the value that not is in the dropdownlist.
> > > > ListItem li = ddl.Items.FindByValue(s);
> > > > if (lm == null)
> > > > {
> > > >     dl.SelectedIndex = 0;
> > > > }
> > > > else
> > > > {
> > > >    lm.Selected = true;
> > > > }
> > > > }
> > > >
> > > > Can anybody help me??
> > > >
> > > > Karl-Inge
> > > >
Author
6 Dec 2005 6:59 PM
Phillip Williams
One issue about this approach though is that you are using
DropDownList.DataBinding event.  But I think this is not correct because at
this point the dropdownlist has not been populated yet.  You need to use the
DropDownList.DataBound.  I have modified my demo to include this snippet of
code too.  http://www.webswapp.com/demos/FormView1.aspx

(Notice that this demo is based on removing the declarative binding for the
dependent dropdownlist)



Show quoteHide quote
"Karl-Inge Reknes" wrote:

> Hi Phillip
>
> Thanks for your help, but it don’t solve my problem.  Her is my code:
>
> protected void DropDownList1_DataBinding(object sender, EventArgs e)
> {
> string customerStatus =   
> (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus;
>
> DropDownList dl =
> (DropDownList)((FormView)((DropDownList)sender).NamingContainer).FindControl("DropDownList1");
> dl.ClearSelection();
> ListItem lm = dl.Items.FindByValue(customerStatus);
> if (lm == null)
> {
>   dl.SelectedIndex = 0;
> }
> else
> {
>   lm.Selected = true;
> }
> }
>
> When customerStatus is in the DropDownList1 I got this error:
>
> The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive
>
> When customerStatus is not in the DropDownList I got this error:
> 'DropDownList1' has a SelectedValue which is invalid because it does not
> exist in the list of items.
> Parameter name: value
> I hope you can help me?
>
> Karl-Inge
>
>
> "Phillip Williams" wrote:
>
> > Hi Karl-Inge,
> >
> > You would need to access the DataItem of the NamingContainer(the FormView),
> > e.g.
> > //assuming that the second value in your datasource is the one databound to
> > the
> > string strContainerValue =
> > ((DataRowView)((FormView)((DropDownList)sender).NameingContainer).DataItem)[1];   
> > //Then you can set the selectedItem of the dropdownlist to match this value
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Karl-Inge Reknes" wrote:
> >
> > > I have a Formview with a dropdownlist. The dropdownlist is bind to the
> > > formview datasourceobject. When I retrieve a value that is not in the
> > > dropdownlist I got this error:
> > > “'DropDownList1' has a SelectedValue which is invalid because it does not
> > > exist in the list of items.
> > > Parameter name: value “
> > > I have try this code, but helpless:
> > > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > > {
> > > DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
> > > ddl.ClearSelection();
> > > //The string s held the value that not is in the dropdownlist.
> > > ListItem li = ddl.Items.FindByValue(s);
> > > if (lm == null)
> > > {
> > >     dl.SelectedIndex = 0;
> > > }
> > > else
> > > {
> > >    lm.Selected = true;
> > > }
> > > }
> > >
> > > Can anybody help me??
> > >
> > > Karl-Inge
> > >
Author
7 Dec 2005 12:55 PM
Karl-Inge Reknes
Thanks for helping me Phillip!

I have tried your suggestions, but it doesn’t work well.

I have run the debugger and found the following.

When the DropdownList.DataBinding Event is triggered, the dropdownlist is
not populated. Like you told me.

If I move the code to check if database value is found in dropdownlist1 to
the Databound event, the following error appears before the Databound event
is triggered:

'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value

I have tried to set CustomerStatus to a valid value in the Databinding
event, but with the same error. I use this code in the Databinding event:

protected void DropDownList1_DataBinding(object sender, EventArgs e)
{ (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus="0";

//“0” is a valid value in the dropdownlist1 (“Customerstatus not selected”).
}

I have also tried to add an item in the DataBinding event, but that item is
removed when DataBound event is triggered.

Is the problem being that I use ObjectDataSource instead of SqlDataSource?

Karl-Inge Reknes


Show quoteHide quote
"Phillip Williams" wrote:

>
> One issue about this approach though is that you are using
> DropDownList.DataBinding event.  But I think this is not correct because at
> this point the dropdownlist has not been populated yet.  You need to use the
> DropDownList.DataBound.  I have modified my demo to include this snippet of
> code too.  http://www.webswapp.com/demos/FormView1.aspx
>
> (Notice that this demo is based on removing the declarative binding for the
> dependent dropdownlist)
>
>
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Karl-Inge Reknes" wrote:
>
> > Hi Phillip
> >
> > Thanks for your help, but it don’t solve my problem.  Her is my code:
> >
> > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > {
> > string customerStatus =   
> > (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus;
> >
> > DropDownList dl =
> > (DropDownList)((FormView)((DropDownList)sender).NamingContainer).FindControl("DropDownList1");
> > dl.ClearSelection();
> > ListItem lm = dl.Items.FindByValue(customerStatus);
> > if (lm == null)
> > {
> >   dl.SelectedIndex = 0;
> > }
> > else
> > {
> >   lm.Selected = true;
> > }
> > }
> >
> > When customerStatus is in the DropDownList1 I got this error:
> >
> > The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive
> >
> > When customerStatus is not in the DropDownList I got this error:
> > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > exist in the list of items.
> > Parameter name: value
> > I hope you can help me?
> >
> > Karl-Inge
> >
> >
> > "Phillip Williams" wrote:
> >
> > > Hi Karl-Inge,
> > >
> > > You would need to access the DataItem of the NamingContainer(the FormView),
> > > e.g.
> > > //assuming that the second value in your datasource is the one databound to
> > > the
> > > string strContainerValue =
> > > ((DataRowView)((FormView)((DropDownList)sender).NameingContainer).DataItem)[1];   
> > > //Then you can set the selectedItem of the dropdownlist to match this value
> > >
> > > --
> > > HTH,
> > > Phillip Williams
> > > http://www.societopia.net
> > > http://www.webswapp.com
> > >
> > >
> > > "Karl-Inge Reknes" wrote:
> > >
> > > > I have a Formview with a dropdownlist. The dropdownlist is bind to the
> > > > formview datasourceobject. When I retrieve a value that is not in the
> > > > dropdownlist I got this error:
> > > > “'DropDownList1' has a SelectedValue which is invalid because it does not
> > > > exist in the list of items.
> > > > Parameter name: value “
> > > > I have try this code, but helpless:
> > > > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > > > {
> > > > DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
> > > > ddl.ClearSelection();
> > > > //The string s held the value that not is in the dropdownlist.
> > > > ListItem li = ddl.Items.FindByValue(s);
> > > > if (lm == null)
> > > > {
> > > >     dl.SelectedIndex = 0;
> > > > }
> > > > else
> > > > {
> > > >    lm.Selected = true;
> > > > }
> > > > }
> > > >
> > > > Can anybody help me??
> > > >
> > > > Karl-Inge
> > > >
Author
7 Dec 2005 8:31 PM
Phillip Williams
Hi Karl-Inge,  Can you post the markup that you use for defining the
FormView?  I need to understand what you are trying to do with the FormView
(from a business perspective).

Show quoteHide quote
"Karl-Inge Reknes" wrote:

> Thanks for helping me Phillip!
>
> I have tried your suggestions, but it doesn’t work well.
>
> I have run the debugger and found the following.
>
> When the DropdownList.DataBinding Event is triggered, the dropdownlist is
> not populated. Like you told me.
>
> If I move the code to check if database value is found in dropdownlist1 to
> the Databound event, the following error appears before the Databound event
> is triggered:
>
> 'DropDownList1' has a SelectedValue which is invalid because it does not
> exist in the list of items.
> Parameter name: value
>
> I have tried to set CustomerStatus to a valid value in the Databinding
> event, but with the same error. I use this code in the Databinding event:
>
> protected void DropDownList1_DataBinding(object sender, EventArgs e)
> {
> (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus="0";
>
> //“0” is a valid value in the dropdownlist1 (“Customerstatus not selected”).
> }
>
> I have also tried to add an item in the DataBinding event, but that item is
> removed when DataBound event is triggered.
>
> Is the problem being that I use ObjectDataSource instead of SqlDataSource?
>
> Karl-Inge Reknes
>
>
> "Phillip Williams" wrote:
>
> >
> > One issue about this approach though is that you are using
> > DropDownList.DataBinding event.  But I think this is not correct because at
> > this point the dropdownlist has not been populated yet.  You need to use the
> > DropDownList.DataBound.  I have modified my demo to include this snippet of
> > code too.  http://www.webswapp.com/demos/FormView1.aspx
> >
> > (Notice that this demo is based on removing the declarative binding for the
> > dependent dropdownlist)
> >
> >
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Karl-Inge Reknes" wrote:
> >
> > > Hi Phillip
> > >
> > > Thanks for your help, but it don’t solve my problem.  Her is my code:
> > >
> > > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > > {
> > > string customerStatus =   
> > > (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus;
> > >
> > > DropDownList dl =
> > > (DropDownList)((FormView)((DropDownList)sender).NamingContainer).FindControl("DropDownList1");
> > > dl.ClearSelection();
> > > ListItem lm = dl.Items.FindByValue(customerStatus);
> > > if (lm == null)
> > > {
> > >   dl.SelectedIndex = 0;
> > > }
> > > else
> > > {
> > >   lm.Selected = true;
> > > }
> > > }
> > >
> > > When customerStatus is in the DropDownList1 I got this error:
> > >
> > > The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive
> > >
> > > When customerStatus is not in the DropDownList I got this error:
> > > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > > exist in the list of items.
> > > Parameter name: value
> > > I hope you can help me?
> > >
> > > Karl-Inge
> > >
> > >
> > > "Phillip Williams" wrote:
> > >
> > > > Hi Karl-Inge,
> > > >
> > > > You would need to access the DataItem of the NamingContainer(the FormView),
> > > > e.g.
> > > > //assuming that the second value in your datasource is the one databound to
> > > > the
> > > > string strContainerValue =
> > > > ((DataRowView)((FormView)((DropDownList)sender).NameingContainer).DataItem)[1];   
> > > > //Then you can set the selectedItem of the dropdownlist to match this value
> > > >
> > > > --
> > > > HTH,
> > > > Phillip Williams
> > > > http://www.societopia.net
> > > > http://www.webswapp.com
> > > >
> > > >
> > > > "Karl-Inge Reknes" wrote:
> > > >
> > > > > I have a Formview with a dropdownlist. The dropdownlist is bind to the
> > > > > formview datasourceobject. When I retrieve a value that is not in the
> > > > > dropdownlist I got this error:
> > > > > “'DropDownList1' has a SelectedValue which is invalid because it does not
> > > > > exist in the list of items.
> > > > > Parameter name: value “
> > > > > I have try this code, but helpless:
> > > > > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > > > > {
> > > > > DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
> > > > > ddl.ClearSelection();
> > > > > //The string s held the value that not is in the dropdownlist.
> > > > > ListItem li = ddl.Items.FindByValue(s);
> > > > > if (lm == null)
> > > > > {
> > > > >     dl.SelectedIndex = 0;
> > > > > }
> > > > > else
> > > > > {
> > > > >    lm.Selected = true;
> > > > > }
> > > > > }
> > > > >
> > > > > Can anybody help me??
> > > > >
> > > > > Karl-Inge
> > > > >
Author
8 Dec 2005 7:59 PM
Karl-Inge Reknes
Hi Phillip

Here is an example that shows the problem.

When I retrieve a Customer that has a Customer.Status that exists in table
CustomerStatus everything works fin (Customer.ID = 1 or 2), but when a
retrieve a Customer (Customer.id = 3) with Customer.Status than not exist in
table CustomerStatus a got this error:

'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value

You can say that this is an inconsistent database. Yes, but that is not the
question. I want to find a solution that handles this situation. In this
example I want to set customer.status to a valid value when the database is
inconsistent.

Here is the code:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FormView ID="FormView1" runat="server"
DataSourceID="ObjectDataSourceCustomer"
            DefaultMode="Edit">
            <EditItemTemplate>
                Status:
                <asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSourceCustomerStatus"
                    DataTextField="Text" DataValueField="Status"
SelectedValue='<%# Bind("Status") %>'>
                </asp:DropDownList>
                <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
runat="server" SelectMethod="GetCustomerStatus"
                    TypeName="CustomerDBClass"></asp:ObjectDataSource>
                <br />
                Id:
                <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
Bind("Id") %>'></asp:TextBox><br />
                Name:
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
Bind("Name") %>'></asp:TextBox><br />
                <asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
                    Text="Update"></asp:LinkButton>
                <asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
                    Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <InsertItemTemplate>
                Status:
                <asp:TextBox ID="StatusTextBox" runat="server" Text='<%#
Bind("Status") %>'>
                </asp:TextBox><br />
                Id:
                <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
Bind("Id") %>'>
                </asp:TextBox><br />
                Name:
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
Bind("Name") %>'>
                </asp:TextBox><br />
                <asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True" CommandName="Insert"
                    Text="Insert">
                </asp:LinkButton>
                <asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton>
            </InsertItemTemplate>
            <ItemTemplate>
                Status:
                <asp:Label ID="StatusLabel" runat="server" Text='<%#
Bind("Status") %>'></asp:Label><br />
                Id:
                <asp:Label ID="IdLabel" runat="server" Text='<%# Bind("Id")
%>'></asp:Label><br />
                Name:
                <asp:Label ID="NameLabel" runat="server" Text='<%#
Bind("Name") %>'></asp:Label><br />
            </ItemTemplate>
        </asp:FormView>
        <asp:ObjectDataSource ID="ObjectDataSourceCustomer" runat="server"
SelectMethod="GetCustomer"
            TypeName="CustomerDBClass">
            <SelectParameters>
                <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
QueryStringField="id"
                    Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>

    </div>
    </form>
</body>
</html>

Customer.cs
using System;
public class Customer
{
    public Customer()
    {
    }
    public Customer(int id, string name, string status)
    {
        _id = id;
        _name = name;
        _status = status;
    }

    private int _id;
    private string _name;
    private string _status;

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    public string Status
    {
        get { return _status; }
        set { _status = value; }
    }

}

CustomerStatus.cs
using System;
public class CustomerStatus
{
    public CustomerStatus()
    {
    }
    public CustomerStatus(string status, string Text)
    {
        _status = status;
        _text = Text;
    }
    private string _status;
    private string _text;

    public string Status
    {
        get { return _status; }
        set { _status = value; }
    }

    public string Text
    {
        get { return _text; }
        set { _text = value; }
    }
}

CustomerDBClass.cs
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
public class CustomerDBClass
{
    public CustomerDBClass()
    {
    }
    public List<Customer> GetCustomer(int CustomerId)
    {
        System.Data.IDbConnection dbConnection = new
System.Data.SqlClient.SqlConnection(findConnectionString());
        System.Data.IDbCommand dbCommand = new
System.Data.SqlClient.SqlCommand();

        dbCommand.CommandText = "select id, name, status from customer where
id = @Customerid";
        dbCommand.Connection = dbConnection;

        System.Data.IDataParameter dbParameter_id = new
System.Data.SqlClient.SqlParameter();
        dbParameter_id.ParameterName = "@Customerid";
        dbParameter_id.Value = CustomerId;
        dbParameter_id.DbType = System.Data.DbType.Int16;
        dbCommand.Parameters.Add(dbParameter_id);

        System.Data.IDbDataAdapter dataAdapeter = new
System.Data.SqlClient.SqlDataAdapter();
        dataAdapeter.SelectCommand = dbCommand;
        System.Data.DataSet ds = new System.Data.DataSet();

        dataAdapeter.Fill(ds);


        List<Customer> customerList = new List<Customer>();

        foreach (DataRow row in ds.Tables[0].Rows)
        {
           customerList.Add(new Customer((int)row["id"],
(string)row["name"], (string)row["status"]));
        }

        return customerList;

    }

    public List<CustomerStatus> GetCustomerStatus()
    {
        System.Data.IDbConnection dbConnection = new
System.Data.SqlClient.SqlConnection(findConnectionString());
        System.Data.IDbCommand dbCommand = new
System.Data.SqlClient.SqlCommand();

        dbCommand.CommandText = "select status, text from customerStatus";
        dbCommand.Connection = dbConnection;

        System.Data.IDbDataAdapter dataAdapeter = new
System.Data.SqlClient.SqlDataAdapter();
        dataAdapeter.SelectCommand = dbCommand;
        System.Data.DataSet ds = new System.Data.DataSet();

        dataAdapeter.Fill(ds);


        List<CustomerStatus> customerStatusList = new List<CustomerStatus>();

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            customerStatusList.Add(new CustomerStatus((string)row["status"],
(string)row["text"]));
        }

        return customerStatusList;

    }

    private static string findConnectionString()
    {
        System.Configuration.ConnectionStringSettings connString;
        connString = ConfigurationManager.ConnectionStrings["TestDB"];
        return connString.ConnectionString;
    }
}


Table:
Customer
ID (int)
Name char(30)
Status char(1)

Record in Customer Table:

ID = 1
Name = ‘Customer A’
Status = ‘A’

ID = 2
Name = ‘Customer B’
Status = ‘P’

ID = 3
Name = ‘Customer C’
Status = ‘X’

CustomerStatus:
Status char(1)
Text char(10)

Records in CustomerStatus  Table:
status = ‘A’
text = ‘Activ’

status = ‘P’
text = ‘Passive’


I hope you can help me.

Karl-Inge Reknes


Show quoteHide quote
"Phillip Williams" wrote:

> Hi Karl-Inge,  Can you post the markup that you use for defining the
> FormView?  I need to understand what you are trying to do with the FormView
> (from a business perspective).
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Karl-Inge Reknes" wrote:
>
> > Thanks for helping me Phillip!
> >
> > I have tried your suggestions, but it doesn’t work well.
> >
> > I have run the debugger and found the following.
> >
> > When the DropdownList.DataBinding Event is triggered, the dropdownlist is
> > not populated. Like you told me.
> >
> > If I move the code to check if database value is found in dropdownlist1 to
> > the Databound event, the following error appears before the Databound event
> > is triggered:
> >
> > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > exist in the list of items.
> > Parameter name: value
> >
> > I have tried to set CustomerStatus to a valid value in the Databinding
> > event, but with the same error. I use this code in the Databinding event:
> >
> > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > {
> > (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus="0";
> >
> > //“0” is a valid value in the dropdownlist1 (“Customerstatus not selected”).
> > }
> >
> > I have also tried to add an item in the DataBinding event, but that item is
> > removed when DataBound event is triggered.
> >
> > Is the problem being that I use ObjectDataSource instead of SqlDataSource?
> >
> > Karl-Inge Reknes
> >
> >
> > "Phillip Williams" wrote:
> >
> > >
> > > One issue about this approach though is that you are using
> > > DropDownList.DataBinding event.  But I think this is not correct because at
> > > this point the dropdownlist has not been populated yet.  You need to use the
> > > DropDownList.DataBound.  I have modified my demo to include this snippet of
> > > code too.  http://www.webswapp.com/demos/FormView1.aspx
> > >
> > > (Notice that this demo is based on removing the declarative binding for the
> > > dependent dropdownlist)
> > >
> > >
> > >
> > > --
> > > HTH,
> > > Phillip Williams
> > > http://www.societopia.net
> > > http://www.webswapp.com
> > >
> > >
> > > "Karl-Inge Reknes" wrote:
> > >
> > > > Hi Phillip
> > > >
> > > > Thanks for your help, but it don’t solve my problem.  Her is my code:
> > > >
> > > > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > > > {
> > > > string customerStatus =   
> > > > (Customer)((FormView)((DropDownList)sender).NamingContainer).DataItem).CustomerStatus;
> > > >
> > > > DropDownList dl =
> > > > (DropDownList)((FormView)((DropDownList)sender).NamingContainer).FindControl("DropDownList1");
> > > > dl.ClearSelection();
> > > > ListItem lm = dl.Items.FindByValue(customerStatus);
> > > > if (lm == null)
> > > > {
> > > >   dl.SelectedIndex = 0;
> > > > }
> > > > else
> > > > {
> > > >   lm.Selected = true;
> > > > }
> > > > }
> > > >
> > > > When customerStatus is in the DropDownList1 I got this error:
> > > >
> > > > The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive
> > > >
> > > > When customerStatus is not in the DropDownList I got this error:
> > > > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > > > exist in the list of items.
> > > > Parameter name: value
> > > > I hope you can help me?
> > > >
> > > > Karl-Inge
> > > >
> > > >
> > > > "Phillip Williams" wrote:
> > > >
> > > > > Hi Karl-Inge,
> > > > >
> > > > > You would need to access the DataItem of the NamingContainer(the FormView),
> > > > > e.g.
> > > > > //assuming that the second value in your datasource is the one databound to
> > > > > the
> > > > > string strContainerValue =
> > > > > ((DataRowView)((FormView)((DropDownList)sender).NameingContainer).DataItem)[1];   
> > > > > //Then you can set the selectedItem of the dropdownlist to match this value
> > > > >
> > > > > --
> > > > > HTH,
> > > > > Phillip Williams
> > > > > http://www.societopia.net
> > > > > http://www.webswapp.com
> > > > >
> > > > >
> > > > > "Karl-Inge Reknes" wrote:
> > > > >
> > > > > > I have a Formview with a dropdownlist. The dropdownlist is bind to the
> > > > > > formview datasourceobject. When I retrieve a value that is not in the
> > > > > > dropdownlist I got this error:
> > > > > > “'DropDownList1' has a SelectedValue which is invalid because it does not
> > > > > > exist in the list of items.
> > > > > > Parameter name: value “
> > > > > > I have try this code, but helpless:
> > > > > > protected void DropDownList1_DataBinding(object sender, EventArgs e)
> > > > > > {
> > > > > > DropDownList ddl = (DropDownList)FormView1.FindControl("DropDownList1");
> > > > > > ddl.ClearSelection();
> > > > > > //The string s held the value that not is in the dropdownlist.
> > > > > > ListItem li = ddl.Items.FindByValue(s);
> > > > > > if (lm == null)
> > > > > > {
> > > > > >     dl.SelectedIndex = 0;
> > > > > > }
> > > > > > else
> > > > > > {
> > > > > >    lm.Selected = true;
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > Can anybody help me??
> > > > > >
> > > > > > Karl-Inge
> > > > > >
Author
9 Dec 2005 1:21 AM
Phillip Williams
Hi Karl-Inge,

You might try a SQL Union statement for retrieving the List of
CustomerStatus.  Something like this modification to the markup:

<asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
    runat="server" SelectMethod="GetCustomerStatus"
    TypeName="CustomerDBClass">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
            QueryStringField="id"    Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

and the following modification to the business layer:

public List<CustomerStatus> GetCustomerStatus(int CustomerId)
    {
        System.Data.IDbConnection dbConnection = new
System.Data.SqlClient.SqlConnection(findConnectionString());
        System.Data.IDbCommand dbCommand = new
System.Data.SqlClient.SqlCommand();

        dbCommand.CommandText = "SELECT status, text from (select status,
text from customerStatus UNION (Select Status, status as text from Customer
where id=@customerid) as B Group by status, text";
        dbCommand.Connection = dbConnection;

        System.Data.IDbDataAdapter dataAdapeter = new
System.Data.SqlClient.SqlDataAdapter();
        dataAdapeter.SelectCommand = dbCommand;
        System.Data.DataSet ds = new System.Data.DataSet();

        dataAdapeter.Fill(ds);


        List<CustomerStatus> customerStatusList = new List<CustomerStatus>();

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            customerStatusList.Add(new CustomerStatus((string)row["status"],
(string)row["text"]));
        }

        return customerStatusList;

    }

Show quoteHide quote
"Karl-Inge Reknes" wrote:

> Hi Phillip
>
> Here is an example that shows the problem.
>
> When I retrieve a Customer that has a Customer.Status that exists in table
> CustomerStatus everything works fin (Customer.ID = 1 or 2), but when a
> retrieve a Customer (Customer.id = 3) with Customer.Status than not exist in
> table CustomerStatus a got this error:
>
> 'DropDownList1' has a SelectedValue which is invalid because it does not
> exist in the list of items.
> Parameter name: value
>
> You can say that this is an inconsistent database. Yes, but that is not the
> question. I want to find a solution that handles this situation. In this
> example I want to set customer.status to a valid value when the database is
> inconsistent.
>
> Here is the code:
> Default.aspx
> <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"
> Inherits="_Default" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
>     <title>Untitled Page</title>
> </head>
> <body>
>     <form id="form1" runat="server">
>     <div>
>         <asp:FormView ID="FormView1" runat="server"
> DataSourceID="ObjectDataSourceCustomer"
>             DefaultMode="Edit">
>             <EditItemTemplate>
>                 Status:
>                 <asp:DropDownList ID="DropDownList1" runat="server"
> DataSourceID="ObjectDataSourceCustomerStatus"
>                     DataTextField="Text" DataValueField="Status"
> SelectedValue='<%# Bind("Status") %>'>
>                 </asp:DropDownList>
>                 <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
> runat="server" SelectMethod="GetCustomerStatus"
>                     TypeName="CustomerDBClass"></asp:ObjectDataSource>
>                 <br />
>                 Id:
>                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> Bind("Id") %>'></asp:TextBox><br />
>                 Name:
>                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> Bind("Name") %>'></asp:TextBox><br />
>                 <asp:LinkButton ID="UpdateButton" runat="server"
> CausesValidation="True" CommandName="Update"
>                     Text="Update"></asp:LinkButton>
>                 <asp:LinkButton ID="UpdateCancelButton" runat="server"
> CausesValidation="False" CommandName="Cancel"
>                     Text="Cancel"></asp:LinkButton>
>             </EditItemTemplate>
>             <InsertItemTemplate>
>                 Status:
>                 <asp:TextBox ID="StatusTextBox" runat="server" Text='<%#
> Bind("Status") %>'>
>                 </asp:TextBox><br />
>                 Id:
>                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> Bind("Id") %>'>
>                 </asp:TextBox><br />
>                 Name:
>                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> Bind("Name") %>'>
>                 </asp:TextBox><br />
>                 <asp:LinkButton ID="InsertButton" runat="server"
> CausesValidation="True" CommandName="Insert"
>                     Text="Insert">
>                 </asp:LinkButton>
>                 <asp:LinkButton ID="InsertCancelButton" runat="server"
> CausesValidation="False" CommandName="Cancel"
>                     Text="Cancel">
>                 </asp:LinkButton>
>             </InsertItemTemplate>
>             <ItemTemplate>
>                 Status:
>                 <asp:Label ID="StatusLabel" runat="server" Text='<%#
> Bind("Status") %>'></asp:Label><br />
>                 Id:
>                 <asp:Label ID="IdLabel" runat="server" Text='<%# Bind("Id")
> %>'></asp:Label><br />
>                 Name:
>                 <asp:Label ID="NameLabel" runat="server" Text='<%#
> Bind("Name") %>'></asp:Label><br />
>             </ItemTemplate>
>         </asp:FormView>
>         <asp:ObjectDataSource ID="ObjectDataSourceCustomer" runat="server"
> SelectMethod="GetCustomer"
>             TypeName="CustomerDBClass">
>             <SelectParameters>
>                 <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
> QueryStringField="id"
>                     Type="Int32" />
>             </SelectParameters>
>         </asp:ObjectDataSource>
>    
>     </div>
>     </form>
> </body>
> </html>
>
> Customer.cs
> using System;
> public class Customer
> {
>     public Customer()
>     {
>     }
>     public Customer(int id, string name, string status)
>     {
>         _id = id;
>         _name = name;
>         _status = status;
>     }
>
>     private int _id;
>     private string _name;
>     private string _status;
>
>     public int Id
>     {
>         get { return _id; }
>         set { _id = value; }
>     }
>     public string Name
>     {
>         get { return _name; }
>         set { _name = value; }
>     }
>     public string Status
>     {
>         get { return _status; }
>         set { _status = value; }
>     }
>    
> }
>
> CustomerStatus.cs
> using System;
> public class CustomerStatus
> {
>     public CustomerStatus()
>     {
>     }
>     public CustomerStatus(string status, string Text)
>     {
>         _status = status;
>         _text = Text;
>     }
>     private string _status;
>     private string _text;
>
>     public string Status
>     {
>         get { return _status; }
>         set { _status = value; }
>     }
>
>     public string Text
>     {
>         get { return _text; }
>         set { _text = value; }
>     }
> }
>
> CustomerDBClass.cs
> using System.Web;
> using System.Data;
> using System.Collections.Generic;
> using System.Configuration;
> public class CustomerDBClass
> {
>     public CustomerDBClass()
>     {
>     }
>     public List<Customer> GetCustomer(int CustomerId)
>     {
>         System.Data.IDbConnection dbConnection = new
> System.Data.SqlClient.SqlConnection(findConnectionString());
>         System.Data.IDbCommand dbCommand = new
> System.Data.SqlClient.SqlCommand();
>
>         dbCommand.CommandText = "select id, name, status from customer where
> id = @Customerid";
>         dbCommand.Connection = dbConnection;
>
>         System.Data.IDataParameter dbParameter_id = new
> System.Data.SqlClient.SqlParameter();
>         dbParameter_id.ParameterName = "@Customerid";
>         dbParameter_id.Value = CustomerId;
>         dbParameter_id.DbType = System.Data.DbType.Int16;
>         dbCommand.Parameters.Add(dbParameter_id);
>
>         System.Data.IDbDataAdapter dataAdapeter = new
> System.Data.SqlClient.SqlDataAdapter();
>         dataAdapeter.SelectCommand = dbCommand;
>         System.Data.DataSet ds = new System.Data.DataSet();
>
>         dataAdapeter.Fill(ds);
>
>
>         List<Customer> customerList = new List<Customer>();
>
>         foreach (DataRow row in ds.Tables[0].Rows)
>         {
>            customerList.Add(new Customer((int)row["id"],
> (string)row["name"], (string)row["status"]));
>         }
>
>         return customerList;
>        
>     }
>
>     public List<CustomerStatus> GetCustomerStatus()
>     {
>         System.Data.IDbConnection dbConnection = new
> System.Data.SqlClient.SqlConnection(findConnectionString());
>         System.Data.IDbCommand dbCommand = new
> System.Data.SqlClient.SqlCommand();
>
>         dbCommand.CommandText = "select status, text from customerStatus";
>         dbCommand.Connection = dbConnection;
>
>         System.Data.IDbDataAdapter dataAdapeter = new
> System.Data.SqlClient.SqlDataAdapter();
>         dataAdapeter.SelectCommand = dbCommand;
>         System.Data.DataSet ds = new System.Data.DataSet();
>
>         dataAdapeter.Fill(ds);
>
>
>         List<CustomerStatus> customerStatusList = new List<CustomerStatus>();
>
>         foreach (DataRow row in ds.Tables[0].Rows)
>         {
>             customerStatusList.Add(new CustomerStatus((string)row["status"],
> (string)row["text"]));
>         }
>
>         return customerStatusList;
>
>     }
>
>     private static string findConnectionString()
>     {
>         System.Configuration.ConnectionStringSettings connString;
>         connString = ConfigurationManager.ConnectionStrings["TestDB"];
>         return connString.ConnectionString;
>     }
> }
>
>
> Table:
> Customer
> ID (int)
> Name char(30)
> Status char(1)
>
> Record in Customer Table:
>
> ID = 1
> Name = ‘Customer A’
> Status = ‘A’
>
> ID = 2
> Name = ‘Customer B’
> Status = ‘P’
>
> ID = 3
> Name = ‘Customer C’
> Status = ‘X’
>
> CustomerStatus:
> Status char(1)
> Text char(10)
>
> Records in CustomerStatus  Table:
> status = ‘A’
> text = ‘Activ’
>
> status = ‘P’
> text = ‘Passive’
>
>
> I hope you can help me.
>
> Karl-Inge Reknes
>
>
> "Phillip Williams" wrote:
>
> > Hi Karl-Inge,  Can you post the markup that you use for defining the
> > FormView?  I need to understand what you are trying to do with the FormView
> > (from a business perspective).
Author
9 Dec 2005 1:27 AM
Phillip Williams
Correction for the UNION sql syntax:
======
dbCommand.CommandText = "SELECT status, text from (select status, text from
customerStatus UNION Select Status, status as text from Customer where
id=@customerid) as B Group by status, text";

and of course you need to add a parameter:
============================
        dbParameter_id.ParameterName = "@Customerid";
        dbParameter_id.Value = CustomerId;
        dbParameter_id.DbType = System.Data.DbType.Int16;
        dbCommand.Parameters.Add(dbParameter_id);

Show quoteHide quote
"Phillip Williams" wrote:

> Hi Karl-Inge,
>
> You might try a SQL Union statement for retrieving the List of
> CustomerStatus.  Something like this modification to the markup:
>
> <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
>     runat="server" SelectMethod="GetCustomerStatus"
>     TypeName="CustomerDBClass">
>     <SelectParameters>
>         <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
>             QueryStringField="id"    Type="Int32" />
>     </SelectParameters>
> </asp:ObjectDataSource>
>
> and the following modification to the business layer:
>
> public List<CustomerStatus> GetCustomerStatus(int CustomerId)
>     {
>         System.Data.IDbConnection dbConnection = new
> System.Data.SqlClient.SqlConnection(findConnectionString());
>         System.Data.IDbCommand dbCommand = new
> System.Data.SqlClient.SqlCommand();
>
>         dbCommand.CommandText = "SELECT status, text from (select status,
> text from customerStatus UNION (Select Status, status as text from Customer
> where id=@customerid) as B Group by status, text";
>         dbCommand.Connection = dbConnection;
>
>         System.Data.IDbDataAdapter dataAdapeter = new
> System.Data.SqlClient.SqlDataAdapter();
>         dataAdapeter.SelectCommand = dbCommand;
>         System.Data.DataSet ds = new System.Data.DataSet();
>
>         dataAdapeter.Fill(ds);
>
>
>         List<CustomerStatus> customerStatusList = new List<CustomerStatus>();
>
>         foreach (DataRow row in ds.Tables[0].Rows)
>         {
>             customerStatusList.Add(new CustomerStatus((string)row["status"],
> (string)row["text"]));
>         }
>
>         return customerStatusList;
>
>     }
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Karl-Inge Reknes" wrote:
>
> > Hi Phillip
> >
> > Here is an example that shows the problem.
> >
> > When I retrieve a Customer that has a Customer.Status that exists in table
> > CustomerStatus everything works fin (Customer.ID = 1 or 2), but when a
> > retrieve a Customer (Customer.id = 3) with Customer.Status than not exist in
> > table CustomerStatus a got this error:
> >
> > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > exist in the list of items.
> > Parameter name: value
> >
> > You can say that this is an inconsistent database. Yes, but that is not the
> > question. I want to find a solution that handles this situation. In this
> > example I want to set customer.status to a valid value when the database is
> > inconsistent.
> >
> > Here is the code:
> > Default.aspx
> > <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"
> > Inherits="_Default" %>
> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> > <html xmlns="http://www.w3.org/1999/xhtml" >
> > <head runat="server">
> >     <title>Untitled Page</title>
> > </head>
> > <body>
> >     <form id="form1" runat="server">
> >     <div>
> >         <asp:FormView ID="FormView1" runat="server"
> > DataSourceID="ObjectDataSourceCustomer"
> >             DefaultMode="Edit">
> >             <EditItemTemplate>
> >                 Status:
> >                 <asp:DropDownList ID="DropDownList1" runat="server"
> > DataSourceID="ObjectDataSourceCustomerStatus"
> >                     DataTextField="Text" DataValueField="Status"
> > SelectedValue='<%# Bind("Status") %>'>
> >                 </asp:DropDownList>
> >                 <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
> > runat="server" SelectMethod="GetCustomerStatus"
> >                     TypeName="CustomerDBClass"></asp:ObjectDataSource>
> >                 <br />
> >                 Id:
> >                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> > Bind("Id") %>'></asp:TextBox><br />
> >                 Name:
> >                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> > Bind("Name") %>'></asp:TextBox><br />
> >                 <asp:LinkButton ID="UpdateButton" runat="server"
> > CausesValidation="True" CommandName="Update"
> >                     Text="Update"></asp:LinkButton>
> >                 <asp:LinkButton ID="UpdateCancelButton" runat="server"
> > CausesValidation="False" CommandName="Cancel"
> >                     Text="Cancel"></asp:LinkButton>
> >             </EditItemTemplate>
> >             <InsertItemTemplate>
> >                 Status:
> >                 <asp:TextBox ID="StatusTextBox" runat="server" Text='<%#
> > Bind("Status") %>'>
> >                 </asp:TextBox><br />
> >                 Id:
> >                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> > Bind("Id") %>'>
> >                 </asp:TextBox><br />
> >                 Name:
> >                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> > Bind("Name") %>'>
> >                 </asp:TextBox><br />
> >                 <asp:LinkButton ID="InsertButton" runat="server"
> > CausesValidation="True" CommandName="Insert"
> >                     Text="Insert">
> >                 </asp:LinkButton>
> >                 <asp:LinkButton ID="InsertCancelButton" runat="server"
> > CausesValidation="False" CommandName="Cancel"
> >                     Text="Cancel">
> >                 </asp:LinkButton>
> >             </InsertItemTemplate>
> >             <ItemTemplate>
> >                 Status:
> >                 <asp:Label ID="StatusLabel" runat="server" Text='<%#
> > Bind("Status") %>'></asp:Label><br />
> >                 Id:
> >                 <asp:Label ID="IdLabel" runat="server" Text='<%# Bind("Id")
> > %>'></asp:Label><br />
> >                 Name:
> >                 <asp:Label ID="NameLabel" runat="server" Text='<%#
> > Bind("Name") %>'></asp:Label><br />
> >             </ItemTemplate>
> >         </asp:FormView>
> >         <asp:ObjectDataSource ID="ObjectDataSourceCustomer" runat="server"
> > SelectMethod="GetCustomer"
> >             TypeName="CustomerDBClass">
> >             <SelectParameters>
> >                 <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
> > QueryStringField="id"
> >                     Type="Int32" />
> >             </SelectParameters>
> >         </asp:ObjectDataSource>
> >    
> >     </div>
> >     </form>
> > </body>
> > </html>
> >
> > Customer.cs
> > using System;
> > public class Customer
> > {
> >     public Customer()
> >     {
> >     }
> >     public Customer(int id, string name, string status)
> >     {
> >         _id = id;
> >         _name = name;
> >         _status = status;
> >     }
> >
> >     private int _id;
> >     private string _name;
> >     private string _status;
> >
> >     public int Id
> >     {
> >         get { return _id; }
> >         set { _id = value; }
> >     }
> >     public string Name
> >     {
> >         get { return _name; }
> >         set { _name = value; }
> >     }
> >     public string Status
> >     {
> >         get { return _status; }
> >         set { _status = value; }
> >     }
> >    
> > }
> >
> > CustomerStatus.cs
> > using System;
> > public class CustomerStatus
> > {
> >     public CustomerStatus()
> >     {
> >     }
> >     public CustomerStatus(string status, string Text)
> >     {
> >         _status = status;
> >         _text = Text;
> >     }
> >     private string _status;
> >     private string _text;
> >
> >     public string Status
> >     {
> >         get { return _status; }
> >         set { _status = value; }
> >     }
> >
> >     public string Text
> >     {
> >         get { return _text; }
> >         set { _text = value; }
> >     }
> > }
> >
> > CustomerDBClass.cs
> > using System.Web;
> > using System.Data;
> > using System.Collections.Generic;
> > using System.Configuration;
> > public class CustomerDBClass
> > {
> >     public CustomerDBClass()
> >     {
> >     }
> >     public List<Customer> GetCustomer(int CustomerId)
> >     {
> >         System.Data.IDbConnection dbConnection = new
> > System.Data.SqlClient.SqlConnection(findConnectionString());
> >         System.Data.IDbCommand dbCommand = new
> > System.Data.SqlClient.SqlCommand();
> >
> >         dbCommand.CommandText = "select id, name, status from customer where
> > id = @Customerid";
> >         dbCommand.Connection = dbConnection;
> >
> >         System.Data.IDataParameter dbParameter_id = new
> > System.Data.SqlClient.SqlParameter();
> >         dbParameter_id.ParameterName = "@Customerid";
> >         dbParameter_id.Value = CustomerId;
> >         dbParameter_id.DbType = System.Data.DbType.Int16;
> >         dbCommand.Parameters.Add(dbParameter_id);
> >
> >         System.Data.IDbDataAdapter dataAdapeter = new
> > System.Data.SqlClient.SqlDataAdapter();
> >         dataAdapeter.SelectCommand = dbCommand;
> >         System.Data.DataSet ds = new System.Data.DataSet();
> >
> >         dataAdapeter.Fill(ds);
> >
> >
> >         List<Customer> customerList = new List<Customer>();
> >
> >         foreach (DataRow row in ds.Tables[0].Rows)
> >         {
> >            customerList.Add(new Customer((int)row["id"],
> > (string)row["name"], (string)row["status"]));
> >         }
> >
> >         return customerList;
> >        
> >     }
> >
> >     public List<CustomerStatus> GetCustomerStatus()
> >     {
> >         System.Data.IDbConnection dbConnection = new
> > System.Data.SqlClient.SqlConnection(findConnectionString());
> >         System.Data.IDbCommand dbCommand = new
> > System.Data.SqlClient.SqlCommand();
> >
> >         dbCommand.CommandText = "select status, text from customerStatus";
> >         dbCommand.Connection = dbConnection;
> >
> >         System.Data.IDbDataAdapter dataAdapeter = new
> > System.Data.SqlClient.SqlDataAdapter();
> >         dataAdapeter.SelectCommand = dbCommand;
> >         System.Data.DataSet ds = new System.Data.DataSet();
> >
> >         dataAdapeter.Fill(ds);
> >
> >
> >         List<CustomerStatus> customerStatusList = new List<CustomerStatus>();
> >
> >         foreach (DataRow row in ds.Tables[0].Rows)
> >         {
> >             customerStatusList.Add(new CustomerStatus((string)row["status"],
> > (string)row["text"]));
Author
9 Dec 2005 1:26 PM
Karl-Inge Reknes
Thanks for your help Phillip

Your suggestion was not the solutions that I was looking for.

Is it not possible to set a valid value before the error is triggered?

Karl-Inge Reknes


Show quoteHide quote
"Phillip Williams" wrote:

> Correction for the UNION sql syntax:
> ======
> dbCommand.CommandText = "SELECT status, text from (select status, text from
> customerStatus UNION Select Status, status as text from Customer where
> id=@customerid) as B Group by status, text";
>
> and of course you need to add a parameter:
> ============================
>         dbParameter_id.ParameterName = "@Customerid";
>         dbParameter_id.Value = CustomerId;
>         dbParameter_id.DbType = System.Data.DbType.Int16;
>         dbCommand.Parameters.Add(dbParameter_id);
>
> "Phillip Williams" wrote:
>
> > Hi Karl-Inge,
> >
> > You might try a SQL Union statement for retrieving the List of
> > CustomerStatus.  Something like this modification to the markup:
> >
> > <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
> >     runat="server" SelectMethod="GetCustomerStatus"
> >     TypeName="CustomerDBClass">
> >     <SelectParameters>
> >         <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
> >             QueryStringField="id"    Type="Int32" />
> >     </SelectParameters>
> > </asp:ObjectDataSource>
> >
> > and the following modification to the business layer:
> >
> > public List<CustomerStatus> GetCustomerStatus(int CustomerId)
> >     {
> >         System.Data.IDbConnection dbConnection = new
> > System.Data.SqlClient.SqlConnection(findConnectionString());
> >         System.Data.IDbCommand dbCommand = new
> > System.Data.SqlClient.SqlCommand();
> >
> >         dbCommand.CommandText = "SELECT status, text from (select status,
> > text from customerStatus UNION (Select Status, status as text from Customer
> > where id=@customerid) as B Group by status, text";
> >         dbCommand.Connection = dbConnection;
> >
> >         System.Data.IDbDataAdapter dataAdapeter = new
> > System.Data.SqlClient.SqlDataAdapter();
> >         dataAdapeter.SelectCommand = dbCommand;
> >         System.Data.DataSet ds = new System.Data.DataSet();
> >
> >         dataAdapeter.Fill(ds);
> >
> >
> >         List<CustomerStatus> customerStatusList = new List<CustomerStatus>();
> >
> >         foreach (DataRow row in ds.Tables[0].Rows)
> >         {
> >             customerStatusList.Add(new CustomerStatus((string)row["status"],
> > (string)row["text"]));
> >         }
> >
> >         return customerStatusList;
> >
> >     }
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Karl-Inge Reknes" wrote:
> >
> > > Hi Phillip
> > >
> > > Here is an example that shows the problem.
> > >
> > > When I retrieve a Customer that has a Customer.Status that exists in table
> > > CustomerStatus everything works fin (Customer.ID = 1 or 2), but when a
> > > retrieve a Customer (Customer.id = 3) with Customer.Status than not exist in
> > > table CustomerStatus a got this error:
> > >
> > > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > > exist in the list of items.
> > > Parameter name: value
> > >
> > > You can say that this is an inconsistent database. Yes, but that is not the
> > > question. I want to find a solution that handles this situation. In this
> > > example I want to set customer.status to a valid value when the database is
> > > inconsistent.
> > >
> > > Here is the code:
> > > Default.aspx
> > > <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"
> > > Inherits="_Default" %>
> > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> > > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> > > <html xmlns="http://www.w3.org/1999/xhtml" >
> > > <head runat="server">
> > >     <title>Untitled Page</title>
> > > </head>
> > > <body>
> > >     <form id="form1" runat="server">
> > >     <div>
> > >         <asp:FormView ID="FormView1" runat="server"
> > > DataSourceID="ObjectDataSourceCustomer"
> > >             DefaultMode="Edit">
> > >             <EditItemTemplate>
> > >                 Status:
> > >                 <asp:DropDownList ID="DropDownList1" runat="server"
> > > DataSourceID="ObjectDataSourceCustomerStatus"
> > >                     DataTextField="Text" DataValueField="Status"
> > > SelectedValue='<%# Bind("Status") %>'>
> > >                 </asp:DropDownList>
> > >                 <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
> > > runat="server" SelectMethod="GetCustomerStatus"
> > >                     TypeName="CustomerDBClass"></asp:ObjectDataSource>
> > >                 <br />
> > >                 Id:
> > >                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> > > Bind("Id") %>'></asp:TextBox><br />
> > >                 Name:
> > >                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> > > Bind("Name") %>'></asp:TextBox><br />
> > >                 <asp:LinkButton ID="UpdateButton" runat="server"
> > > CausesValidation="True" CommandName="Update"
> > >                     Text="Update"></asp:LinkButton>
> > >                 <asp:LinkButton ID="UpdateCancelButton" runat="server"
> > > CausesValidation="False" CommandName="Cancel"
> > >                     Text="Cancel"></asp:LinkButton>
> > >             </EditItemTemplate>
> > >             <InsertItemTemplate>
> > >                 Status:
> > >                 <asp:TextBox ID="StatusTextBox" runat="server" Text='<%#
> > > Bind("Status") %>'>
> > >                 </asp:TextBox><br />
> > >                 Id:
> > >                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> > > Bind("Id") %>'>
> > >                 </asp:TextBox><br />
> > >                 Name:
> > >                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> > > Bind("Name") %>'>
> > >                 </asp:TextBox><br />
> > >                 <asp:LinkButton ID="InsertButton" runat="server"
> > > CausesValidation="True" CommandName="Insert"
> > >                     Text="Insert">
> > >                 </asp:LinkButton>
> > >                 <asp:LinkButton ID="InsertCancelButton" runat="server"
> > > CausesValidation="False" CommandName="Cancel"
> > >                     Text="Cancel">
> > >                 </asp:LinkButton>
> > >             </InsertItemTemplate>
> > >             <ItemTemplate>
> > >                 Status:
> > >                 <asp:Label ID="StatusLabel" runat="server" Text='<%#
> > > Bind("Status") %>'></asp:Label><br />
> > >                 Id:
> > >                 <asp:Label ID="IdLabel" runat="server" Text='<%# Bind("Id")
> > > %>'></asp:Label><br />
> > >                 Name:
> > >                 <asp:Label ID="NameLabel" runat="server" Text='<%#
> > > Bind("Name") %>'></asp:Label><br />
> > >             </ItemTemplate>
> > >         </asp:FormView>
> > >         <asp:ObjectDataSource ID="ObjectDataSourceCustomer" runat="server"
> > > SelectMethod="GetCustomer"
> > >             TypeName="CustomerDBClass">
> > >             <SelectParameters>
> > >                 <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
> > > QueryStringField="id"
> > >                     Type="Int32" />
> > >             </SelectParameters>
> > >         </asp:ObjectDataSource>
> > >    
> > >     </div>
> > >     </form>
> > > </body>
> > > </html>
> > >
> > > Customer.cs
> > > using System;
> > > public class Customer
> > > {
> > >     public Customer()
> > >     {
> > >     }
> > >     public Customer(int id, string name, string status)
> > >     {
> > >         _id = id;
> > >         _name = name;
> > >         _status = status;
> > >     }
> > >
> > >     private int _id;
> > >     private string _name;
> > >     private string _status;
> > >
> > >     public int Id
> > >     {
> > >         get { return _id; }
> > >         set { _id = value; }
> > >     }
> > >     public string Name
> > >     {
> > >         get { return _name; }
> > >         set { _name = value; }
> > >     }
> > >     public string Status
> > >     {
> > >         get { return _status; }
> > >         set { _status = value; }
> > >     }
> > >    
> > > }
> > >
> > > CustomerStatus.cs
> > > using System;
> > > public class CustomerStatus
> > > {
> > >     public CustomerStatus()
> > >     {
> > >     }
> > >     public CustomerStatus(string status, string Text)
> > >     {
> > >         _status = status;
> > >         _text = Text;
> > >     }
> > >     private string _status;
> > >     private string _text;
> > >
> > >     public string Status
> > >     {
> > >         get { return _status; }
> > >         set { _status = value; }
> > >     }
> > >
> > >     public string Text
> > >     {
> > >         get { return _text; }
> > >         set { _text = value; }
> > >     }
> > > }
> > >
> > > CustomerDBClass.cs
> > > using System.Web;
> > > using System.Data;
> > > using System.Collections.Generic;
> > > using System.Configuration;
> > > public class CustomerDBClass
> > > {
> > >     public CustomerDBClass()
> > >     {
> > >     }
> > >     public List<Customer> GetCustomer(int CustomerId)
> > >     {
> > >         System.Data.IDbConnection dbConnection = new
> > > System.Data.SqlClient.SqlConnection(findConnectionString());
> > >         System.Data.IDbCommand dbCommand = new
> > > System.Data.SqlClient.SqlCommand();
> > >
> > >         dbCommand.CommandText = "select id, name, status from customer where
> > > id = @Customerid";
> > >         dbCommand.Connection = dbConnection;
> > >
> > >         System.Data.IDataParameter dbParameter_id = new
> > > System.Data.SqlClient.SqlParameter();
> > >         dbParameter_id.ParameterName = "@Customerid";
> > >         dbParameter_id.Value = CustomerId;
> > >         dbParameter_id.DbType = System.Data.DbType.Int16;
> > >         dbCommand.Parameters.Add(dbParameter_id);
> > >
> > >         System.Data.IDbDataAdapter dataAdapeter = new
> > > System.Data.SqlClient.SqlDataAdapter();
> > >         dataAdapeter.SelectCommand = dbCommand;
> > >         System.Data.DataSet ds = new System.Data.DataSet();
> > >
> > >         dataAdapeter.Fill(ds);
> > >
> > >
> > >         List<Customer> customerList = new List<Customer>();
> > >
> > >         foreach (DataRow row in ds.Tables[0].Rows)
> > >         {
> > >            customerList.Add(new Customer((int)row["id"],
> > > (string)row["name"], (string)row["status"]));
> > >         }
> > >
> > >         return customerList;
> > >        
> > >     }
> > >
> > >     public List<CustomerStatus> GetCustomerStatus()
> > >     {
> > >         System.Data.IDbConnection dbConnection = new
> > > System.Data.SqlClient.SqlConnection(findConnectionString());
> > >         System.Data.IDbCommand dbCommand = new
> > > System.Data.SqlClient.SqlCommand();
> > >
> > >         dbCommand.CommandText = "select status, text from customerStatus";
> > >         dbCommand.Connection = dbConnection;
Author
9 Dec 2005 6:03 PM
Phillip Williams
Hi Karl-Inge, 

I will give some reasons as to why I recommend to use the Business Layer in
the scenario you presented.

The challenges here are:
1-    Bad data (data that violates reference checking)
2-    Timing of 2 actions that happen in the same event handling: During
DataBinding the dropdownlist, the ASP.NET engine creates the list items and
binds the CustomerStatus value from the DataItem.  We cannot interfere in
between those 2 actions.

I think the best approach is to check the validity of the data within the
business layer before attempting to use it in the UI. 

1-    While retrieving a list of Customers: create Left Outer Join, e.g.

Select id, name, status from customer as A LEFT OUTER JOIN CustomerStatus AS
B On A.status = B.status where id = @Customerid

This would replace the invalid customer.status values with null, which would
work perfectly with your declarative syntax without the need for writing
crafty code while databinding to the web server controls.

2-    While retrieving the list of CustomerStatus (as I explained in the
previous post) one would add the invalid value to the list of items.

Show quoteHide quote
"Karl-Inge Reknes" wrote:

> Thanks for your help Phillip
>
> Your suggestion was not the solutions that I was looking for.
>
> Is it not possible to set a valid value before the error is triggered?
>
> Karl-Inge Reknes
>
>
> "Phillip Williams" wrote:
>
> > Correction for the UNION sql syntax:
> > ======
> > dbCommand.CommandText = "SELECT status, text from (select status, text from
> > customerStatus UNION Select Status, status as text from Customer where
> > id=@customerid) as B Group by status, text";
> >
> > and of course you need to add a parameter:
> > ============================
> >         dbParameter_id.ParameterName = "@Customerid";
> >         dbParameter_id.Value = CustomerId;
> >         dbParameter_id.DbType = System.Data.DbType.Int16;
> >         dbCommand.Parameters.Add(dbParameter_id);
> >
> > "Phillip Williams" wrote:
> >
> > > Hi Karl-Inge,
> > >
> > > You might try a SQL Union statement for retrieving the List of
> > > CustomerStatus.  Something like this modification to the markup:
> > >
> > > <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
> > >     runat="server" SelectMethod="GetCustomerStatus"
> > >     TypeName="CustomerDBClass">
> > >     <SelectParameters>
> > >         <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
> > >             QueryStringField="id"    Type="Int32" />
> > >     </SelectParameters>
> > > </asp:ObjectDataSource>
> > >
> > > and the following modification to the business layer:
> > >
> > > public List<CustomerStatus> GetCustomerStatus(int CustomerId)
> > >     {
> > >         System.Data.IDbConnection dbConnection = new
> > > System.Data.SqlClient.SqlConnection(findConnectionString());
> > >         System.Data.IDbCommand dbCommand = new
> > > System.Data.SqlClient.SqlCommand();
> > >
> > >         dbCommand.CommandText = "SELECT status, text from (select status,
> > > text from customerStatus UNION (Select Status, status as text from Customer
> > > where id=@customerid) as B Group by status, text";
> > >         dbCommand.Connection = dbConnection;
> > >
> > >         System.Data.IDbDataAdapter dataAdapeter = new
> > > System.Data.SqlClient.SqlDataAdapter();
> > >         dataAdapeter.SelectCommand = dbCommand;
> > >         System.Data.DataSet ds = new System.Data.DataSet();
> > >
> > >         dataAdapeter.Fill(ds);
> > >
> > >
> > >         List<CustomerStatus> customerStatusList = new List<CustomerStatus>();
> > >
> > >         foreach (DataRow row in ds.Tables[0].Rows)
> > >         {
> > >             customerStatusList.Add(new CustomerStatus((string)row["status"],
> > > (string)row["text"]));
> > >         }
> > >
> > >         return customerStatusList;
> > >
> > >     }
> > >
> > > --
> > > HTH,
> > > Phillip Williams
> > > http://www.societopia.net
> > > http://www.webswapp.com
> > >
> > >
> > > "Karl-Inge Reknes" wrote:
> > >
> > > > Hi Phillip
> > > >
> > > > Here is an example that shows the problem.
> > > >
> > > > When I retrieve a Customer that has a Customer.Status that exists in table
> > > > CustomerStatus everything works fin (Customer.ID = 1 or 2), but when a
> > > > retrieve a Customer (Customer.id = 3) with Customer.Status than not exist in
> > > > table CustomerStatus a got this error:
> > > >
> > > > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > > > exist in the list of items.
> > > > Parameter name: value
> > > >
> > > > You can say that this is an inconsistent database. Yes, but that is not the
> > > > question. I want to find a solution that handles this situation. In this
> > > > example I want to set customer.status to a valid value when the database is
> > > > inconsistent.
> > > >
> > > > Here is the code:
> > > > Default.aspx
> > > > <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"
> > > > Inherits="_Default" %>
> > > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> > > > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> > > > <html xmlns="http://www.w3.org/1999/xhtml" >
> > > > <head runat="server">
> > > >     <title>Untitled Page</title>
> > > > </head>
> > > > <body>
> > > >     <form id="form1" runat="server">
> > > >     <div>
> > > >         <asp:FormView ID="FormView1" runat="server"
> > > > DataSourceID="ObjectDataSourceCustomer"
> > > >             DefaultMode="Edit">
> > > >             <EditItemTemplate>
> > > >                 Status:
> > > >                 <asp:DropDownList ID="DropDownList1" runat="server"
> > > > DataSourceID="ObjectDataSourceCustomerStatus"
> > > >                     DataTextField="Text" DataValueField="Status"
> > > > SelectedValue='<%# Bind("Status") %>'>
> > > >                 </asp:DropDownList>
> > > >                 <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
> > > > runat="server" SelectMethod="GetCustomerStatus"
> > > >                     TypeName="CustomerDBClass"></asp:ObjectDataSource>
> > > >                 <br />
> > > >                 Id:
> > > >                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> > > > Bind("Id") %>'></asp:TextBox><br />
> > > >                 Name:
> > > >                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> > > > Bind("Name") %>'></asp:TextBox><br />
> > > >                 <asp:LinkButton ID="UpdateButton" runat="server"
> > > > CausesValidation="True" CommandName="Update"
> > > >                     Text="Update"></asp:LinkButton>
> > > >                 <asp:LinkButton ID="UpdateCancelButton" runat="server"
> > > > CausesValidation="False" CommandName="Cancel"
> > > >                     Text="Cancel"></asp:LinkButton>
> > > >             </EditItemTemplate>
> > > >             <InsertItemTemplate>
> > > >                 Status:
> > > >                 <asp:TextBox ID="StatusTextBox" runat="server" Text='<%#
> > > > Bind("Status") %>'>
> > > >                 </asp:TextBox><br />
> > > >                 Id:
> > > >                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> > > > Bind("Id") %>'>
> > > >                 </asp:TextBox><br />
> > > >                 Name:
> > > >                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> > > > Bind("Name") %>'>
> > > >                 </asp:TextBox><br />
> > > >                 <asp:LinkButton ID="InsertButton" runat="server"
> > > > CausesValidation="True" CommandName="Insert"
> > > >                     Text="Insert">
> > > >                 </asp:LinkButton>
> > > >                 <asp:LinkButton ID="InsertCancelButton" runat="server"
> > > > CausesValidation="False" CommandName="Cancel"
> > > >                     Text="Cancel">
> > > >                 </asp:LinkButton>
> > > >             </InsertItemTemplate>
> > > >             <ItemTemplate>
> > > >                 Status:
> > > >                 <asp:Label ID="StatusLabel" runat="server" Text='<%#
> > > > Bind("Status") %>'></asp:Label><br />
> > > >                 Id:
> > > >                 <asp:Label ID="IdLabel" runat="server" Text='<%# Bind("Id")
> > > > %>'></asp:Label><br />
> > > >                 Name:
> > > >                 <asp:Label ID="NameLabel" runat="server" Text='<%#
> > > > Bind("Name") %>'></asp:Label><br />
> > > >             </ItemTemplate>
> > > >         </asp:FormView>
> > > >         <asp:ObjectDataSource ID="ObjectDataSourceCustomer" runat="server"
> > > > SelectMethod="GetCustomer"
> > > >             TypeName="CustomerDBClass">
> > > >             <SelectParameters>
> > > >                 <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
> > > > QueryStringField="id"
> > > >                     Type="Int32" />
> > > >             </SelectParameters>
> > > >         </asp:ObjectDataSource>
> > > >    
> > > >     </div>
> > > >     </form>
> > > > </body>
> > > > </html>
> > > >
> > > > Customer.cs
> > > > using System;
> > > > public class Customer
> > > > {
> > > >     public Customer()
> > > >     {
> > > >     }
> > > >     public Customer(int id, string name, string status)
> > > >     {
> > > >         _id = id;
> > > >         _name = name;
> > > >         _status = status;
> > > >     }
> > > >
> > > >     private int _id;
> > > >     private string _name;
> > > >     private string _status;
> > > >
> > > >     public int Id
> > > >     {
> > > >         get { return _id; }
> > > >         set { _id = value; }
> > > >     }
> > > >     public string Name
> > > >     {
> > > >         get { return _name; }
> > > >         set { _name = value; }
> > > >     }
> > > >     public string Status
> > > >     {
> > > >         get { return _status; }
> > > >         set { _status = value; }
> > > >     }
> > > >    
> > > > }
> > > >
> > > > CustomerStatus.cs
> > > > using System;
> > > > public class CustomerStatus
> > > > {
> > > >     public CustomerStatus()
> > > >     {
> > > >     }
> > > >     public CustomerStatus(string status, string Text)
> > > >     {
> > > >         _status = status;
> > > >         _text = Text;
> > > >     }
> > > >     private string _status;
> > > >     private string _text;
> > > >
> > > >     public string Status
> > > >     {
> > > >         get { return _status; }
> > > >         set { _status = value; }
> > > >     }
> > > >
> > > >     public string Text
> > > >     {
> > > >         get { return _text; }
> > > >         set { _text = value; }
> > > >     }
> > > > }
> > > >
> > > > CustomerDBClass.cs
> > > > using System.Web;
> > > > using System.Data;
> > > > using System.Collections.Generic;
> > > > using System.Configuration;
> > > > public class CustomerDBClass
> > > > {
> > > >     public CustomerDBClass()
> > > >     {
> > > >     }
> > > >     public List<Customer> GetCustomer(int CustomerId)
> > > >     {
> > > >         System.Data.IDbConnection dbConnection = new
> > > > System.Data.SqlClient.SqlConnection(findConnectionString());
> > > >         System.Data.IDbCommand dbCommand = new
> > > > System.Data.SqlClient.SqlCommand();
> > > >
> > > >         dbCommand.CommandText = "select id, name, status from customer where
> > > > id = @Customerid";
> > > >         dbCommand.Connection = dbConnection;
> > > >
> > > >         System.Data.IDataParameter dbParameter_id = new
> > > > System.Data.SqlClient.SqlParameter();
> > > >         dbParameter_id.ParameterName = "@Customerid";
> > > >         dbParameter_id.Value = CustomerId;
> > > >         dbParameter_id.DbType = System.Data.DbType.Int16;
> > > >         dbCommand.Parameters.Add(dbParameter_id);
> > > >
> > > >         System.Data.IDbDataAdapter dataAdapeter = new
> > > > System.Data.SqlClient.SqlDataAdapter();
> > > >         dataAdapeter.SelectCommand = dbCommand;
> > > >         System.Data.DataSet ds = new System.Data.DataSet();
> > > >
> > > >         dataAdapeter.Fill(ds);
> > > >
> > > >
> > > >         List<Customer> customerList = new List<Customer>();
> > > >
> > > >         foreach (DataRow row in ds.Tables[0].Rows)
> > > >         {
> > > >            customerList.Add(new Customer((int)row["id"],
> > > > (string)row["name"], (string)row["status"]));
> > > >         }
> > > >
> > > >         return customerList;
> > > >
Author
9 Dec 2005 6:20 PM
Karl-Inge Reknes
Thanks Phillip

I agree.

Karl-Inge Reknes

Show quoteHide quote
"Phillip Williams" wrote:

> Hi Karl-Inge, 
>
> I will give some reasons as to why I recommend to use the Business Layer in
> the scenario you presented.
>
> The challenges here are:
> 1-    Bad data (data that violates reference checking)
> 2-    Timing of 2 actions that happen in the same event handling: During
> DataBinding the dropdownlist, the ASP.NET engine creates the list items and
> binds the CustomerStatus value from the DataItem.  We cannot interfere in
> between those 2 actions.
>
> I think the best approach is to check the validity of the data within the
> business layer before attempting to use it in the UI. 
>
> 1-    While retrieving a list of Customers: create Left Outer Join, e.g.
>
> Select id, name, status from customer as A LEFT OUTER JOIN CustomerStatus AS
> B On A.status = B.status where id = @Customerid
>
> This would replace the invalid customer.status values with null, which would
> work perfectly with your declarative syntax without the need for writing
> crafty code while databinding to the web server controls.
>
> 2-    While retrieving the list of CustomerStatus (as I explained in the
> previous post) one would add the invalid value to the list of items.
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Karl-Inge Reknes" wrote:
>
> > Thanks for your help Phillip
> >
> > Your suggestion was not the solutions that I was looking for.
> >
> > Is it not possible to set a valid value before the error is triggered?
> >
> > Karl-Inge Reknes
> >
> >
> > "Phillip Williams" wrote:
> >
> > > Correction for the UNION sql syntax:
> > > ======
> > > dbCommand.CommandText = "SELECT status, text from (select status, text from
> > > customerStatus UNION Select Status, status as text from Customer where
> > > id=@customerid) as B Group by status, text";
> > >
> > > and of course you need to add a parameter:
> > > ============================
> > >         dbParameter_id.ParameterName = "@Customerid";
> > >         dbParameter_id.Value = CustomerId;
> > >         dbParameter_id.DbType = System.Data.DbType.Int16;
> > >         dbCommand.Parameters.Add(dbParameter_id);
> > >
> > > "Phillip Williams" wrote:
> > >
> > > > Hi Karl-Inge,
> > > >
> > > > You might try a SQL Union statement for retrieving the List of
> > > > CustomerStatus.  Something like this modification to the markup:
> > > >
> > > > <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
> > > >     runat="server" SelectMethod="GetCustomerStatus"
> > > >     TypeName="CustomerDBClass">
> > > >     <SelectParameters>
> > > >         <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
> > > >             QueryStringField="id"    Type="Int32" />
> > > >     </SelectParameters>
> > > > </asp:ObjectDataSource>
> > > >
> > > > and the following modification to the business layer:
> > > >
> > > > public List<CustomerStatus> GetCustomerStatus(int CustomerId)
> > > >     {
> > > >         System.Data.IDbConnection dbConnection = new
> > > > System.Data.SqlClient.SqlConnection(findConnectionString());
> > > >         System.Data.IDbCommand dbCommand = new
> > > > System.Data.SqlClient.SqlCommand();
> > > >
> > > >         dbCommand.CommandText = "SELECT status, text from (select status,
> > > > text from customerStatus UNION (Select Status, status as text from Customer
> > > > where id=@customerid) as B Group by status, text";
> > > >         dbCommand.Connection = dbConnection;
> > > >
> > > >         System.Data.IDbDataAdapter dataAdapeter = new
> > > > System.Data.SqlClient.SqlDataAdapter();
> > > >         dataAdapeter.SelectCommand = dbCommand;
> > > >         System.Data.DataSet ds = new System.Data.DataSet();
> > > >
> > > >         dataAdapeter.Fill(ds);
> > > >
> > > >
> > > >         List<CustomerStatus> customerStatusList = new List<CustomerStatus>();
> > > >
> > > >         foreach (DataRow row in ds.Tables[0].Rows)
> > > >         {
> > > >             customerStatusList.Add(new CustomerStatus((string)row["status"],
> > > > (string)row["text"]));
> > > >         }
> > > >
> > > >         return customerStatusList;
> > > >
> > > >     }
> > > >
> > > > --
> > > > HTH,
> > > > Phillip Williams
> > > > http://www.societopia.net
> > > > http://www.webswapp.com
> > > >
> > > >
> > > > "Karl-Inge Reknes" wrote:
> > > >
> > > > > Hi Phillip
> > > > >
> > > > > Here is an example that shows the problem.
> > > > >
> > > > > When I retrieve a Customer that has a Customer.Status that exists in table
> > > > > CustomerStatus everything works fin (Customer.ID = 1 or 2), but when a
> > > > > retrieve a Customer (Customer.id = 3) with Customer.Status than not exist in
> > > > > table CustomerStatus a got this error:
> > > > >
> > > > > 'DropDownList1' has a SelectedValue which is invalid because it does not
> > > > > exist in the list of items.
> > > > > Parameter name: value
> > > > >
> > > > > You can say that this is an inconsistent database. Yes, but that is not the
> > > > > question. I want to find a solution that handles this situation. In this
> > > > > example I want to set customer.status to a valid value when the database is
> > > > > inconsistent.
> > > > >
> > > > > Here is the code:
> > > > > Default.aspx
> > > > > <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"
> > > > > Inherits="_Default" %>
> > > > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> > > > > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> > > > > <html xmlns="http://www.w3.org/1999/xhtml" >
> > > > > <head runat="server">
> > > > >     <title>Untitled Page</title>
> > > > > </head>
> > > > > <body>
> > > > >     <form id="form1" runat="server">
> > > > >     <div>
> > > > >         <asp:FormView ID="FormView1" runat="server"
> > > > > DataSourceID="ObjectDataSourceCustomer"
> > > > >             DefaultMode="Edit">
> > > > >             <EditItemTemplate>
> > > > >                 Status:
> > > > >                 <asp:DropDownList ID="DropDownList1" runat="server"
> > > > > DataSourceID="ObjectDataSourceCustomerStatus"
> > > > >                     DataTextField="Text" DataValueField="Status"
> > > > > SelectedValue='<%# Bind("Status") %>'>
> > > > >                 </asp:DropDownList>
> > > > >                 <asp:ObjectDataSource ID="ObjectDataSourceCustomerStatus"
> > > > > runat="server" SelectMethod="GetCustomerStatus"
> > > > >                     TypeName="CustomerDBClass"></asp:ObjectDataSource>
> > > > >                 <br />
> > > > >                 Id:
> > > > >                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> > > > > Bind("Id") %>'></asp:TextBox><br />
> > > > >                 Name:
> > > > >                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> > > > > Bind("Name") %>'></asp:TextBox><br />
> > > > >                 <asp:LinkButton ID="UpdateButton" runat="server"
> > > > > CausesValidation="True" CommandName="Update"
> > > > >                     Text="Update"></asp:LinkButton>
> > > > >                 <asp:LinkButton ID="UpdateCancelButton" runat="server"
> > > > > CausesValidation="False" CommandName="Cancel"
> > > > >                     Text="Cancel"></asp:LinkButton>
> > > > >             </EditItemTemplate>
> > > > >             <InsertItemTemplate>
> > > > >                 Status:
> > > > >                 <asp:TextBox ID="StatusTextBox" runat="server" Text='<%#
> > > > > Bind("Status") %>'>
> > > > >                 </asp:TextBox><br />
> > > > >                 Id:
> > > > >                 <asp:TextBox ID="IdTextBox" runat="server" Text='<%#
> > > > > Bind("Id") %>'>
> > > > >                 </asp:TextBox><br />
> > > > >                 Name:
> > > > >                 <asp:TextBox ID="NameTextBox" runat="server" Text='<%#
> > > > > Bind("Name") %>'>
> > > > >                 </asp:TextBox><br />
> > > > >                 <asp:LinkButton ID="InsertButton" runat="server"
> > > > > CausesValidation="True" CommandName="Insert"
> > > > >                     Text="Insert">
> > > > >                 </asp:LinkButton>
> > > > >                 <asp:LinkButton ID="InsertCancelButton" runat="server"
> > > > > CausesValidation="False" CommandName="Cancel"
> > > > >                     Text="Cancel">
> > > > >                 </asp:LinkButton>
> > > > >             </InsertItemTemplate>
> > > > >             <ItemTemplate>
> > > > >                 Status:
> > > > >                 <asp:Label ID="StatusLabel" runat="server" Text='<%#
> > > > > Bind("Status") %>'></asp:Label><br />
> > > > >                 Id:
> > > > >                 <asp:Label ID="IdLabel" runat="server" Text='<%# Bind("Id")
> > > > > %>'></asp:Label><br />
> > > > >                 Name:
> > > > >                 <asp:Label ID="NameLabel" runat="server" Text='<%#
> > > > > Bind("Name") %>'></asp:Label><br />
> > > > >             </ItemTemplate>
> > > > >         </asp:FormView>
> > > > >         <asp:ObjectDataSource ID="ObjectDataSourceCustomer" runat="server"
> > > > > SelectMethod="GetCustomer"
> > > > >             TypeName="CustomerDBClass">
> > > > >             <SelectParameters>
> > > > >                 <asp:QueryStringParameter DefaultValue="1" Name="CustomerId"
> > > > > QueryStringField="id"
> > > > >                     Type="Int32" />
> > > > >             </SelectParameters>
> > > > >         </asp:ObjectDataSource>
> > > > >    
> > > > >     </div>
> > > > >     </form>
> > > > > </body>
> > > > > </html>
> > > > >
> > > > > Customer.cs
> > > > > using System;
> > > > > public class Customer
> > > > > {
> > > > >     public Customer()
> > > > >     {
> > > > >     }
> > > > >     public Customer(int id, string name, string status)
> > > > >     {
> > > > >         _id = id;
> > > > >         _name = name;
> > > > >         _status = status;
> > > > >     }
> > > > >
> > > > >     private int _id;
> > > > >     private string _name;
> > > > >     private string _status;
> > > > >
> > > > >     public int Id
> > > > >     {
> > > > >         get { return _id; }
> > > > >         set { _id = value; }
> > > > >     }
> > > > >     public string Name
> > > > >     {
> > > > >         get { return _name; }
> > > > >         set { _name = value; }
> > > > >     }
> > > > >     public string Status
> > > > >     {
> > > > >         get { return _status; }
> > > > >         set { _status = value; }
> > > > >     }
> > > > >    
> > > > > }
> > > > >
> > > > > CustomerStatus.cs
> > > > > using System;
> > > > > public class CustomerStatus
> > > > > {
> > > > >     public CustomerStatus()
> > > > >     {
> > > > >     }
> > > > >     public CustomerStatus(string status, string Text)
> > > > >     {
> > > > >         _status = status;
> > > > >         _text = Text;
> > > > >     }
> > > > >     private string _status;
> > > > >     private string _text;
> > > > >
> > > > >     public string Status
> > > > >     {
> > > > >         get { return _status; }
> > > > >         set { _status = value; }
> > > > >     }
> > > > >
> > > > >     public string Text
> > > > >     {
> > > > >         get { return _text; }
> > > > >         set { _text = value; }
> > > > >     }
> > > > > }
> > > > >
> > > > > CustomerDBClass.cs
> > > > > using System.Web;
> > > > > using System.Data;
> > > > > using System.Collections.Generic;
> > > > > using System.Configuration;
> > > > > public class CustomerDBClass
> > > > > {
> > > > >     public CustomerDBClass()
> > > > >     {
> > > > >     }