468,107 Members | 1,263 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Drop Down List Selected Value is Null Problem

I am using ASP.net 2.0 and trying to take advantage of the updated data
editing facilities provided through the SQLDataSource control and the
DetailsView control. The data is a record from a customer complaints table
and one of the fields on the DetailsView control is called ComplaintType.
The field is a template field and in insert mode and edit mode i have used a
DropDownList control bound to a set of keys and values in a ComplaintTypes
table and the SelectedValue is bound to the foreign key in the complaints
table that links to the complaint types table. The problem i am having is
that the way the complaints procedure works is that, the complaint comes in
by phone and the receptionist simply takes basic details like name address
and a text description of the complaint The complaint then gets passed to
the QA department who then assign it a complaint type among other things.
When the receptionist enters the initial data they simply do not get an
option to set the compaint type, it defaults to NULL and all is well.
However when someone from the QA dept. clicks a link to turn the DetailsView
control into edit mode, it generates a runtime error because the
SelectedValue property is bound to a field that is set to NULL and NULL isn't
an value in the dataset bound to the DataValueField property. This error is
iritating because all it means is that ASP.net is not able to determine which
item in the HMTL drop down list to set to "selected" but instead of doing the
sensible thing and just not setting anything and carrying on, it generates a
run time error. The only way round this i can think of is to set an item in
the ComplaintTypes table to "Not Set" and set this as the default value for
the foreign key in the complaints table. This works but is an inelegant
solution especialy as i want to give the QA department the ability to update
the complaint types table and if they delete the "Not Set" record it will
make the whole thing fall over, so i then have to make sure that they can't
do that. This problem also applies to a number of other fields that work in
exactly the same way so it is not just a one field problem. Is there a
better solution that i have missed out on. One that doesn't require there
to be a matching record in the foreign table for the each foreign key in the
local table, especially as all these foreign keys default to NULL.
Mar 22 '06 #1
2 8179
When you define the dropdownlist in your detailsView, try adding a static
listitem with value ="" and use the AppendDataBoundItems property of the
DropDownList
http://msdn2.microsoft.com/en-us/lib...ms(VS.80).aspx

For example,
<asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountries"
SelectedValue='<%# Bind("Country")
%>'>
<asp:ListItem Value="">Not Set</asp:ListItem>
</asp:DropDownList>

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"clickon" wrote:
I am using ASP.net 2.0 and trying to take advantage of the updated data
editing facilities provided through the SQLDataSource control and the
DetailsView control. The data is a record from a customer complaints table
and one of the fields on the DetailsView control is called ComplaintType.
The field is a template field and in insert mode and edit mode i have used a
DropDownList control bound to a set of keys and values in a ComplaintTypes
table and the SelectedValue is bound to the foreign key in the complaints
table that links to the complaint types table. The problem i am having is
that the way the complaints procedure works is that, the complaint comes in
by phone and the receptionist simply takes basic details like name address
and a text description of the complaint The complaint then gets passed to
the QA department who then assign it a complaint type among other things.
When the receptionist enters the initial data they simply do not get an
option to set the compaint type, it defaults to NULL and all is well.
However when someone from the QA dept. clicks a link to turn the DetailsView
control into edit mode, it generates a runtime error because the
SelectedValue property is bound to a field that is set to NULL and NULL isn't
an value in the dataset bound to the DataValueField property. This error is
iritating because all it means is that ASP.net is not able to determine which
item in the HMTL drop down list to set to "selected" but instead of doing the
sensible thing and just not setting anything and carrying on, it generates a
run time error. The only way round this i can think of is to set an item in
the ComplaintTypes table to "Not Set" and set this as the default value for
the foreign key in the complaints table. This works but is an inelegant
solution especialy as i want to give the QA department the ability to update
the complaint types table and if they delete the "Not Set" record it will
make the whole thing fall over, so i then have to make sure that they can't
do that. This problem also applies to a number of other fields that work in
exactly the same way so it is not just a one field problem. Is there a
better solution that i have missed out on. One that doesn't require there
to be a matching record in the foreign table for the each foreign key in the
local table, especially as all these foreign keys default to NULL.

Mar 22 '06 #2
Hi
Thanks i didn't think of that, i didn't realise "" would map to null, but i
supose it must. I got round it by adding:

UNION
SELECT NULL AS ComplaintTypeID, NULL AS ComplaintTypeDescription

to the Select statment in the SQL datasource and it seems to work well.

"Phillip Williams" wrote:
When you define the dropdownlist in your detailsView, try adding a static
listitem with value ="" and use the AppendDataBoundItems property of the
DropDownList
http://msdn2.microsoft.com/en-us/lib...ms(VS.80).aspx

For example,
<asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountries"
SelectedValue='<%# Bind("Country")
%>'>
<asp:ListItem Value="">Not Set</asp:ListItem>
</asp:DropDownList>

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"clickon" wrote:
I am using ASP.net 2.0 and trying to take advantage of the updated data
editing facilities provided through the SQLDataSource control and the
DetailsView control. The data is a record from a customer complaints table
and one of the fields on the DetailsView control is called ComplaintType.
The field is a template field and in insert mode and edit mode i have used a
DropDownList control bound to a set of keys and values in a ComplaintTypes
table and the SelectedValue is bound to the foreign key in the complaints
table that links to the complaint types table. The problem i am having is
that the way the complaints procedure works is that, the complaint comes in
by phone and the receptionist simply takes basic details like name address
and a text description of the complaint The complaint then gets passed to
the QA department who then assign it a complaint type among other things.
When the receptionist enters the initial data they simply do not get an
option to set the compaint type, it defaults to NULL and all is well.
However when someone from the QA dept. clicks a link to turn the DetailsView
control into edit mode, it generates a runtime error because the
SelectedValue property is bound to a field that is set to NULL and NULL isn't
an value in the dataset bound to the DataValueField property. This error is
iritating because all it means is that ASP.net is not able to determine which
item in the HMTL drop down list to set to "selected" but instead of doing the
sensible thing and just not setting anything and carrying on, it generates a
run time error. The only way round this i can think of is to set an item in
the ComplaintTypes table to "Not Set" and set this as the default value for
the foreign key in the complaints table. This works but is an inelegant
solution especialy as i want to give the QA department the ability to update
the complaint types table and if they delete the "Not Set" record it will
make the whole thing fall over, so i then have to make sure that they can't
do that. This problem also applies to a number of other fields that work in
exactly the same way so it is not just a one field problem. Is there a
better solution that i have missed out on. One that doesn't require there
to be a matching record in the foreign table for the each foreign key in the
local table, especially as all these foreign keys default to NULL.

Mar 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan | last post: by
2 posts views Thread by Yoshitha | last post: by
5 posts views Thread by Vigneshwar Pilli via DotNetMonster.com | last post: by
4 posts views Thread by nz mark in aus | last post: by
2 posts views Thread by millertime90 | last post: by
1 post views Thread by abTech | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.