Solved

ConvertEmptyStringToNull for Drop Down List Control is not working

Posted on 2008-06-25
6
1,333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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