[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1406
  • Last Modified:

ConvertEmptyStringToNull for Drop Down List Control is not working

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
sacvb
Asked:
sacvb
  • 4
  • 2
1 Solution
 
CoyotesITCommented:
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
 
CoyotesITCommented:
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
 
sacvbAuthor Commented:
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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
CoyotesITCommented:
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
 
sacvbAuthor Commented:
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
 
CoyotesITCommented:
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now