Solved

ConvertEmptyStringToNull for Drop Down List Control is not working

Posted on 2008-06-25
6
1,268 Views
Last Modified: 2013-11-07
I have drop down control ddlType which SelectedValue is used in data source dsOrders.
First item in dropdown list is "Any" with value "" (empty string), other items are appended (AppendDataBoundItems="True") from dsType datasource.

DropDown renders to (from source code):
<select name="ddlType" id="ddlType">
   <option selected="selected" value="">Any</option>
   <option value="2">Delivery</option>
   <option value="4">In-House</option>
   <option value="1">Mail Out</option>
</select>

dsOrders uses selected value in SQL statement: SELECT * FROM tblWOOrder WHERE OrderType =  ISNULL(@typestr, OrderType)

The problem is dsOrders datasource is not working when I select "Any" in the drop down. SelectedValue of item "Any" is empty string which should be converted to null and as a result following statement should be executed: SELECT * FROM tblWOOrder WHERE OrderType =  ISNULL(null, OrderType). It seems to me that ConvertEmptyStringToNull doesn't work with drop down list control.

For testing, I changed SQL in dsOrders to SELECT * FROM tblWOOrder WHERE OrderType =  ISNULL(null, OrderType) (all records should be showing and as you can see I am not even using the parameter), but as soon as I select "Any" on the list, gvOrders GridView doesn't produce any result. If I select some other item, GridView shows all the records as it is supposed.

So, what I am doing wrong, and is there any way around this problem?

I tested this with TextBox control, and if value is empty string, it would be converted to null and everything would work fine.

Thanks.

....
<asp:DropDownList 
         ID="ddlType" 
         runat="server" 
         DataSourceID="dsType" 
         DataTextField="typename" 
         DataValueField="typeID" 
         AppendDataBoundItems="True">
         <asp:ListItem Value="">Any</asp:ListItem>
</asp:DropDownList>
....
<asp:GridView ID="gvOrders" runat="server" DataSourceID="dsOrders">
</asp:GridView>
....
<asp:SqlDataSource ID="dsType" runat="server" ConnectionString="<%$ ConnectionStrings:strCnn %>"
        SelectCommand="SELECT * FROM tblWOType ORDER BY TypeName"></asp:SqlDataSource>
....
<asp:SqlDataSource ID="dsOrders" runat="server" ConnectionString="<%$ ConnectionStrings:strCnn %>"
        SelectCommand="SELECT * FROM tblWOOrder WHERE OrderType =  ISNULL(@typestr, OrderType)" OnSelecting="dsOrders_Selecting">
   <SelectParameters>                         
      <asp:ControlParameter Type="String" ControlID="ddlType" Name="typestr" PropertyName="SelectedValue" ConvertEmptyStringToNull = "true" />
   </SelectParameters>            
</asp:SqlDataSource>

Open in new window

0
Comment
Question by:sacvb
  • 4
  • 2
6 Comments
 
LVL 8

Expert Comment

by:CoyotesIT
ID: 21867643
Can you try putting your parameter above your SelectCommand.

<asp:SqlDataSource ID="dsOrders" runat="server" ConnectionString="<%$ ConnectionStrings:strCnn %>"
       
<SelectParameters>                        
      <asp:ControlParameter Type="String" ControlID="ddlType" Name="typestr" PropertyName="SelectedValue" ConvertEmptyStringToNull = "true" />
</SelectParameters>  

SelectCommand="SELECT * FROM tblWOOrder WHERE OrderType =  ISNULL(@typestr, OrderType)" OnSelecting="dsOrders_Selecting">
             
</asp:SqlDataSource>
0
 
LVL 8

Expert Comment

by:CoyotesIT
ID: 21868027
I see that does not work, why not just put


<asp:DropDownList
         ID="ddlType"
         runat="server"
         DataSourceID="dsType"
         DataTextField="typename"
         DataValueField="typeID"
         AppendDataBoundItems="True">
         <asp:ListItem Value="null">Any</asp:ListItem>
</asp:DropDownList>

That way when your value is passed in your sql will be filled with what you are looking for, right now if you are trying to pass a null value into your sql string isn't it showing up as invalid sql?

I got this value when just running a test

SELECT * FROM table WHERE id = ISNULL(,id)

but when I added the value "null" to my drop down item it showed up as:

SELECT * FROM table WHERE id = ISNULL(null,id) which then parsed and gave me the correct data.

~CoyotesIT



0
 

Author Comment

by:sacvb
ID: 21868806
Thanks for your suggestions CoyotesIT.
Unfortunately non of them worked.

Your first suggestion: SelectParameter cannot be moved in front SelectCommand. It's illegal thing to do and VS reports an error if you attempt to do so.

Second suggestion: Cannot add "null" as a value. I receive an error: Syntax error converting the nvarchar value 'null' to a column of data type int.
Simply, content of the @typestr parameter becomes string "null" instead of value null and SELECT statement therefore reports an error for trying to compare string vs. int.

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 8

Accepted Solution

by:
CoyotesIT earned 250 total points
ID: 21870397
Okay. Here you go. The reason this is not working for you is in your SqlDataSource the CancelSelectOnNullParameter is default to true, you must specify this attribute and set it to false. Once you do this it will pass the null parameter and give you your data back.
<asp:SqlDataSource ID="dsOrders" runat="server" ConnectionString="<%$ ConnectionStrings:strCnn %>"
        SelectCommand="SELECT * FROM tblWOOrder WHERE OrderType =  ISNULL(@typestr, OrderType)" OnSelecting="dsOrders_Selecting"
CancelSelectOnNullParameter="false"
>
   <SelectParameters>                         
      <asp:ControlParameter Type="String" ControlID="ddlType" Name="typestr" PropertyName="SelectedValue" ConvertEmptyStringToNull = "true" />
   </SelectParameters>            
</asp:SqlDataSource>

Open in new window

0
 

Author Comment

by:sacvb
ID: 21875479
Thanks a lot CoyotesIT.

Now it works.

The only weird thing is that if parameter receives value from TextBox control instead of from DropDownList, data source does not care about CancelSelectOnNullParameter="false".

0
 
LVL 8

Expert Comment

by:CoyotesIT
ID: 21875511
Yeah, I know you mentioned that earlier that it worked with the textbox control. That does seem a bit strange. I'm just guessing that there is a difference in how that gets interpreted and is most likely unintentional. The way that the dropdown list works given that the select command has the cancelselectonnull parameter would seem that was the right way.

Glad you got it working though.

Take care.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now