Solved

ConvertEmptyStringToNull for Drop Down List Control is not working

Posted on 2008-06-25
6
1,301 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
itextsharp with c# 3 18
Installing .NET 3.5 on Windows Server 2012 1 62
VB.net Filesystem watcher not working 5 37
YouTube API get the Video Tags - vb.net 7 32
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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