SQLDataSource : Passing Null Parameters

by Sumant 26. August 2008 13:29

Could not have gone more crazy than what a simple query made me today. Got into two problems, and then got a single solution for both.

The task was to bind a Grid View to a SQLDataSource which pulls data using a store procedure based on the parameters passed. (okay, such a silly task! max 30 mins including writing stored proc and doing UI, who would want to waste time on this? lets get started!) After creating the required components and doing the needed plumbing, ran my application to check out the result. What? Got no data? Why?

I am using Control Parameters to set filters, just two of them.

ASP.NET-Code: SqlDataSource
<asp:SqlDataSource ID="sdsUsers" runat="server"
 ConnectionString="<%$ ConnectionStrings:MyDB %>" 
 SelectCommand="GetUsers"                     
 SelectCommandType = "StoredProcedure"        
 >
 <SelectParameters>
  <asp:ControlParameter Name="Name" ControlID="txtName" DefaultValue=""/>
  <asp:ControlParameter Name="IsActive" ControlID="chkActive" DefaultValue="false"/>
 </SelectParameters>
</asp:SqlDataSource>

I quickly ran SQL Profiler to check what's happening. Woa, no backend hit! I quickly blamed on my laziness to not to set all the properties of Control Parameter. After a little debugging using the trial & error approach, I found that it fetches data when the 'DefaultValue' is not set to an empty string for the 'Name' parameter. I was expecting it to send NULL or '' to the stored procedure which handles such a scenario. But it didn't! After reviewing other remaining properties of the ControlParameter, it didn't take me any time to find 'ConvertEmptyStringToNull' property. So I set its value to 'false' for the 'Name' parameter and it started sending empty string - '' - to the stored procedure.

So far so good, it started working and I wanted to quickly finish off this sample so I didn't bother much to dig about NULL. But couldn't avoid that for long! (Usually, you end up spending more time on silly issues only when you don't have time for them! And you never keep time for silly issues! huh!)

Now I thought of changing the 'Active' choice input control from a CheckBox to a DropDownList, reason being IsActive is a Bit field in the db table and stored procedure and I wanted to give user an option of stating "doesn't matter if Active or Not". The related stored procedure understands this when 'NULL' is passed for the @IsActive parameter.

So, here I am again trying to pass 'NULL' to my sp. This time I created a DropDownList with three ListItems - 'Yes' ("true"), 'No' ("false"), Ignore (""). Now taking full advantage of the recent experience, I set the 'ConvertEmptyStringToNull' to True for the 'IsActive' parameter. Here is the new code -

ASP.NET-Code: SqlDataSource
<asp:SqlDataSource ID="sdsUsers" runat="server"
        ConnectionString="<%$ ConnectionStrings:MyDB %>" 
        SelectCommand="GetUsers"                     
        SelectCommandType = "StoredProcedure"        
        >
 <SelectParameters>
  <asp:ControlParameter Name="Name" Type="String" ControlID="txtName" PropertyName="Text" DefaultValue="" ConvertEmptyStringToNull="false" />
  <asp:ControlParameter Name="IsActive" Type="String" ControlID="ddlActive" PropertyName="SelectedValue" DefaultValue=""  ConvertEmptyStringToNull="true"/>  
 </SelectParameters>
</asp:SqlDataSource>

Did this and there I am back to the square one! It stopped hitting the db again. I again did few changes to the 'Ignore' list item, tried various values, and at least got yellow error page to understand that I am on the wrong way.

Finally, I reviewed the SqlDataSource's properties again and then encountered this magical property 'CancelSelectOnNullParameter' which looked like the key to my issues. Now I set the property to 'False', the final code looks like -

ASP.NET-Code: SqlDataSource
<asp:SqlDataSource ID="sdsUsers" runat="server"
        ConnectionString="<%$ ConnectionStrings:MyDB %>" 
        SelectCommand="GetUsers"                     
        SelectCommandType = "StoredProcedure"  
        CancelSelectOnNullParameter="false"
        >
 <SelectParameters>
  <asp:ControlParameter Name="Name" Type="String" ControlID="txtName" PropertyName="Text" DefaultValue="" ConvertEmptyStringToNull="false" />
  <asp:ControlParameter Name="IsActive" Type="String" ControlID="ddlActive" PropertyName="SelectedValue" DefaultValue=""  ConvertEmptyStringToNull="true"/>  
 </SelectParameters>
</asp:SqlDataSource>

Execute.... Bingo! it Worked!

Moral of the story - use 'CancelSelectOnNullParameter' to control if you want to pass null parameter or not. Also use 'ConvertEmptyStringToNull' to convert an empty string (from the bound control) to 'NULL' or to just pass '' (empty string).

 

 

Share   

Tags:

.NET | ASP.NET | Web Development

Comments (15) -

3/10/2011 4:20:56 PM #

Mickey

THANK YOU!

I have searched for the solution to NULL passing for a couple of days on the net...

THANK YOU!

Mickey Israel

3/26/2011 12:21:56 AM #

Rhonda

Thank you!!

Rhonda United States

3/30/2011 1:24:54 AM #

Paul Marangoni

Thanks!

Paul Marangoni United States

4/9/2011 12:44:30 AM #

Artur

I'm glad I found your article. It helped me too.
Thanks

Artur Canada

5/4/2011 2:28:41 AM #

Paula

Thanks guy. I have been trying to figure this out for 1.5 days. Ug. You're right, should have been simple. Thanks!

Paula United States

9/12/2011 6:29:32 PM #

Jasna

I did the same..('ConvertEmptyStringToNull'  Sets to 'False')

Still, I'm not able to pass an empty string.

It gives me no result when am not passing any string.

Please help

Jasna U.A.E.

9/30/2011 12:48:55 PM #

Sumant

@Jasna: did you check other params? if you could share the code snippet, may be we can help.

Sumant India

10/2/2011 10:49:13 AM #

Jasna

Hi Sumanth,

Thanks for you reply.

I was trying to create a datasource that binds with a gridview.

I'm able to get the result if i pass atleast one character(instead of empty string) to the query.

Hope this helps.

Regards,

Jasna

Jasna U.A.E.

10/2/2011 10:57:45 AM #

Jasna

Sumanth,
Here is the code snippet.

<asp:SqlDataSource ID="SqlDataSource_Result" runat="server"
                ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
                SelectCommand="spGetAllTicketDetails" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:ControlParameter ControlID="ddlStatus" ConvertEmptyStringToNull="False"
                        DefaultValue="ALL" Name="tcTicketStatus" PropertyName="SelectedValue"
                        Type="String" />
                    <asp:ControlParameter ControlID="txtTicketNo" ConvertEmptyStringToNull="False"
                        DefaultValue="" Name="tcTicketNo" PropertyName="Text" Type="String" />
                    <asp:SessionParameter DefaultValue="0" Name="tnDepartmentID"
                        SessionField="DepartmentID" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>

Please help,

Jasna

Jasna U.A.E.

10/2/2011 12:48:52 PM #

Sumant

Jasna,

Your code looks all fine. I tested it locally  and it works. So, what I suspect is that the problem is with your stored procedure and not the sqldatasource. You may want to check -

1. If your sql is being hit or not. (trace with sql profiler)
2. Check stored proc's parameter and their usage in the select query.

Let me know if you need any other help. Smile

Good Luck!

Sumant India

10/2/2011 1:10:20 PM #

Jasna

But Sumanth,

When I pass atleast one character (say 'c') I'm getting the result.

I'm using Visual Web Developer 2010.

Also I am able to use the same stored procedure in my code.

Any idea?

Jasna

Jasna U.A.E.

10/2/2011 2:46:04 PM #

Sumant

Did you try executing your sp in the SSMS with empty params and check? Could you post here / mail me your stored procedure code?

Sumant India

10/2/2011 4:23:44 PM #

Jasna

wtz ur mail id?

Jasna U.A.E.

10/2/2011 4:37:57 PM #

Sumant

Sent you an email.

Sumant India

10/6/2011 12:03:23 PM #

Nadav

Great tips, man.
sure helped me solve my problem passing null to stored procedure!!!

Nadav United States

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

© 2008 Sumant Dubey

About this blog

Absolutely technical! The posts are about concepts, tricks, articles and links on technical subjects / problems which are not very obvious to understand / resolve. Purpose is to have this as the first-aid in the time of need.

RecentComments

Disclaimer

The opinions expressed on this website are my own personal opinions and do not represent my employer's view in anyway.