Solved

ConvertEmptyStringToNull for Drop Down List Control is not working

Posted on 2008-06-25
6
1,245 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

13 Experts available now in Live!

Get 1:1 Help Now