views:

142

answers:

2

The two procedures, SearchMaster and SearchDetails work if I run it in Sql Management Studio and SearchMaster even runs fine when I am Testing the Query when I set up the stored procedure in design view, but when I run it, no rows are created if I enter just info for the frn or business. If I enter for both, I do get data if there is a match. Are the parameters etup correctly? Also, if I am initializing the parameters to null in the procedure, is it still necessary to use ConvertEmptyStringToNull?

Here is the SearchMaster StoredProcedure:

CREAETE PROCEDURE SearchMaster
@business nvarchar(300) = NULL,
@frn nvarchar(10) = NULL
AS
SELECT h.Business,
       hrl.frn
FROM registration hrl
INNER JOIN holder h on h.call = hrl.call
WHERE (@business IS NULL OR h.Business like '%' + @business + '%') 
AND (@frn IS NULL OR hrl.frn = @frn)

Here is the SearchDetails StoredProcedure:

CREATE PROCEDURE SearchDetails
@business nvarchar(300) = NULL,
@frn nvarchar(10) = NULL
AS
SELECT hrl.call 
FROM registration hrl 
INNER JOIN holder h ON h.call = hrl.call
WHERE (@business IS NULL OR h.Business LIKE '%' + @business + '%') 
  AND (@frn IS NULL OR hrl.frn = @frn)

Here is the SqlDataSource for the SearchMaster procedure:

<asp:SqlDataSource ID="sqlDsDetails" 
               runat="server" 
               ConnectionString="<%$ ConnectionStrings:cnxString %>
               SelectCommandType="StoredProcedure" 
               SelectCommand="SearchMaster">
<SelectParameters>
<asp:ControlParameter Name="business" ControlID="txtBusiness" 
                  Type="String" PropertyName="Text"  
                  ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="frn" ControlID="txtFRN" 
                  Type="String" PropertyName="Text"
                  ConvertEmptyStringToNull="true"/>
</SelectParameters>
</asp:SqlDataSource>

Here is the SqlDataSource for the SearchDetails procedure:

<asp:SqlDataSource ID="sqlDsDetails" 
               runat="server" 
               ConnectionString="<%$ ConnectionStrings:cnxString %>
               SelectCommandType="StoredProcedure" 
               SelectCommand="SearchDetails">
<SelectParameters>
<asp:Parameter Name="frn" Type="String" DefaultValue="" 
           ConvertEmptyStringToNull="true" />
<asp:Parameter Name="business" Type="String" DefaultValue="" 
           ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>

Here is the button click that binds the SqlDsMaster:

protected void btnSearch_Click(object sender, EventArgs e)
{
    sqlDsMaster.DataBind();
}

Here is the gvMaster_RowCreated that creates the rows for the details:

protected void gvMaster_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        SqlDataSource ctrl = 
        e.Row.FindControl("sqlDsDetails") as SqlDataSource;

        if (ctrl != null && e.Row.DataItem != null)
        {
            ctrl.SelectParameters["frn"].DefaultValue = 
            ((DataRowView)e.Row.DataItem)["frn"].ToString();

            ctrl.SelectParameters["business"].DefaultValue = 
            ((DataRowView)e.Row.DataItem)["business"].ToString();
         }
     }
 }

I ran the SQL Profiler and when only one parameter was entered, the profiler did not even show the procedure running. When I entered both parameters, the procedure was shown running in the profiler.

+2  A: 

My guess would be that it is still passing in an empty string for what you consider the null case. You could rule this out by doing:

CREATE PROCEDURE SearchDetails
@business nvarchar(300) = NULL,
@frn nvarchar(10) = NULL
AS

SELECT @business = NULLIF(@business, ''),
       @frn = NULLIF(@frn, '')

 SELECT hrl.call // etc

If it starts working, it was passing in empty strings.

Marc Gravell
Thanks Marc, I will try this out now and let you know.
Xaisoft
Since I am not to up on t-sql, can you post where I would put this to test it.
Xaisoft
I also have ConvertEmptyStringsToNull = "true" which I thought would prevent empty strings from being passed, plus in the procedure, I am initializing the parameters to null. In addition, if I run the procedure in Sql Management Studio with an empty string for 1 parameter, it still returns data.
Xaisoft
wow I've been using tsql for 10 years and never used NullIf, I use IsNull frequently, but NullIf must have slipped by me somehow.
Neil N
+1  A: 

I suspect Marc Gravell is right on the money.

Try the following tests:

 SearchMaster @business = null        , @frn = null
 SearchMaster @business = 'something' , @frn = null
 SearchMaster @business = 'something' , @frn = 'something'
 SearchMaster @business = null        , @frn = 'something'
 SearchMaster @business = 'something' , @frn = 'something'
 SearchMaster @business = 'something' , @frn = ''
 SearchMaster @business = ''          , @frn = 'something'

Note that the last 2 will not return anything - unless you have blank businesses and frn in the DB ;)

If you place the select statement from Marc just after the AS line your statement it should work as you expect it to.

So something like this:

CREATE PROCEDURE SearchMaster
@business nvarchar(300) = NULL,
@frn nvarchar(10) = NULL
AS
SELECT @business = NULLIF(@business,'') , @frn = NULLIF(@frn,'')

SELECT h.Business,
       hrl.frn
FROM registration hrl
INNER JOIN holder h on h.call = hrl.call
WHERE (@business IS NULL OR h.Business like '%' + @business + '%') 
AND (@frn IS NULL OR hrl.frn = @frn)

And make sure to set CancelSelectOnNullParameter="False" on your DataSource

feihtthief
Only the second to last one did not return anything because I have no blank frn's. I'm still a little confused on how to put the nullif after the AS line. Can you provide an example using my sql?
Xaisoft
Also, SearchMaster @business = '', @frn = 'something' works, but not when I run it in the code.
Xaisoft
Ok, I will try this out and let you know
Xaisoft
Ok that worked. Why would it do that if I am initializing the @business = NULL and @frn = NULL in the procedure and in the code, I am telling it to ConvertEmptyStrings to null. Also, I am not sure who to give the answer to: You or Marc.
Xaisoft
Well, I just entered frn into a textbox and left business blank and it still does not work even after making the changes to the stored procedures by doing the NULLIF.
Xaisoft
The NULLIF makes it work in Sql Management Studio when I run the stored procedures
Xaisoft
OK. Time to use Profiler, and look at what the SP is actually getting as a parameter...Run a Trace with the TSQL_SPs profile and run the page, then let us know what the parameter values are that the SP actually gets.
feihtthief
Ok, Interesting thing happened. If I run the profiler and run the page with just one parameter (business or frn), the SearchMaster does not even run in the profiler. If I run the page with data for both parameters, the procedure runs, so it is not even being executed.
Xaisoft
set CancelSelectOnNullParameter="False" on your data source :)
feihtthief
I have tried that and it runs when I do that, but my search is enabled by a Search Button click and when I set CancelSelectOnNullParameter = "False", it executes the procedure on page_load.
Xaisoft
and since there is so much data when everything is null, it times out.
Xaisoft
So you only want the search to run if criteria has been provided and the search button has been clicked?
feihtthief