0

I have a simple GridView that uses 3 session variables; CoName that is captured when the user logs in and PurchaseOrderDate, and HeatNumber that are captured in text boxes (both of these are optional) as input parameters for a stored procedure on a SQLServer 2012 database. I have a CustomerLanding.aspx page that has a search button on it that, when clicked, redirects the user to a TestReportLanding.aspx page that currently has the GridView on it. Everything works as it should in this scenario.

I want to move the GridView to CustomerLanding.aspx so I can display the search results on the same page that has the search parameters and the search button. Eventually I want to use AJAX for the update, but for now I'm just putting the GridView in a different div. When I use this setup, the query only returns results when the optional parameters (PurchaseOrderDate and HeatNumber) are provided. My stored procedure only requires CoName for it's search, so I should get a lot more rows returned with the optional parameters left empty.

Being somewhat new to .NET, I'm hoping that I'm missing something simple here.

Here is my GridView code in CustomerLanding along with the SqlDataSource:

<div class="col-xs-12 col-lg-10">
<asp:Label ID="NoRecordsFound" runat="server"  Visible ="false" Text="No records found."></asp:Label>
               <asp:GridView ID="GridView1" runat="server" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" GridLines="Vertical" BackColor="White" ForeColor="Black" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                   <AlternatingRowStyle BackColor="#CCCCCC" />
                   <Columns>
                       <asp:BoundField DataField="doc" HeaderText="doc" SortExpression="doc" />
                       <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
                       <asp:BoundField DataField="Heat" HeaderText="Heat" SortExpression="Heat" />
                       <asp:BoundField DataField="Dir" HeaderText="Dir" SortExpression="Dir" />
                   </Columns>
                </asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:iiConnectionString %>" SelectCommand="sp_test_reports" SelectCommandType="StoredProcedure">
                   <SelectParameters>
                       <asp:SessionParameter Name="CustomerName" SessionField="CoName" Type="String" />
                       <asp:SessionParameter DbType="Date" Name="Date" SessionField="PurchaseOrderDate" />
                       <asp:SessionParameter Name="Heat" SessionField="HeatNumber" Type="String" />
                   </SelectParameters>
               </asp:SqlDataSource>
</div>

And for what it's worth here is my stored procedure (I'm certain this is not the problem because it works when the DataGrid is on a seperate page:

CREATE PROCEDURE [dbo].[sp_test_reports] 
-- Add the parameters for the stored procedure here
@CustomerName varchar(50),
@Date date,
@Heat varchar(50)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here


IF @Heat IS NULL AND @Date IS NULL
    SELECT DISTINCT TOP 1000 d.doc, d.Date, dl.Heat, d.Dir
    FROM documents d
    INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
    WHERE d.type = 2 and d.Customer = @CustomerName;
ELSE IF @Heat IS NOT NULL AND @Date IS NULL
    SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
    FROM documents d
    INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
    WHERE d.type = 2 AND d.Customer = @CustomerName AND dl.Heat = @Heat;
ELSE IF @Heat IS NULL AND @Date IS NOT NULL
    SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
    FROM documents d
    INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
    WHERE d.type = 2 AND d.Customer = @CustomerName AND d.Date = @Date;
ELSE IF @Heat IS NOT NULL AND @Date IS NOT NULL
    SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
    FROM documents d
    INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
    WHERE d.type = 2 AND d.Customer = @CustomerName AND d.Date = @Date AND dl.Heat = @Heat;


END

And here is the code behind my search button: Note that if I uncomment the Response.Redirect here, the GridView works like it should.

protected void TestReports_Click(object sender, EventArgs e)
    {
        Session["PurchaseOrderDate"] = DatePurchaseOrder.Text;
        Session["HeatNumber"] = HeatText.Text;



        GridView1.Visible = true;



        MessageBox.Show(Session["CoName"].ToString());
        MessageBox.Show(Session["HeatNumber"].ToString());

        if (GridView1.Rows.Count == 0)
        {
            NoRecordsFound.Visible = true;
        }
        else
        {
            NoRecordsFound.Visible = false;
        }

        //Response.Redirect("TestReportLanding.aspx", false);

    }

Thanks in advance for the help.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
CuriousOne
  • 51
  • 10

2 Answers2

0

In your stored procedure, initialize your two optional parameters to NULL.

@CustomerName varchar(50),
@Date date = NULL,
@Heat varchar(50) = NULL

Parameters for a stored procedure aren't really "optional" unless they've been initialized to something.

BenM
  • 104
  • 5
  • I just did and still get no records. Keep in mind that this sp works when I redirect the user to another page. I will keep Date and Heat initialized to NULL though, it makes sense. – CuriousOne Mar 22 '16 at 17:31
  • Is there any particular reason you're using session variables for the two optional fields? You could change those parameters to asp:controlparameters instead: – BenM Mar 22 '16 at 17:43
  • I'm also curious if you get any results if you hit the button twice. Might be some sort of odd postback issue which isn't saving the values in your text boxes to the session until the page is posted (or redirected). – BenM Mar 22 '16 at 17:45
  • I honestly didn't know about the ControlParameter control. I will look into it in a few minutes. Hitting the button twice yields the same results. – CuriousOne Mar 22 '16 at 17:51
  • Gotcha. If there's no requirement to retain those values from page-to-page, I'd definitely use ControlParameter's. The "Name" property should be the same as the SP Parameter name. It just occurred to me that you might also be missing a databind() method on the gridview as well. Give that a shot after you've updated the parameters. Gridview1.Databind(); – BenM Mar 22 '16 at 17:55
  • I'm now using asp:ControlParameter for purchase order and heat but with the same results. FYI, I did grab the .text from both text boxes and I am displaying it in MessageBox jut to make sure I'm reading in the text, and I am. The MessageBoxes show the input. I'm looking into Gridview1.Databind() now. – CuriousOne Mar 22 '16 at 18:15
  • I was missing GridView1.Databind() but I'm using it on my TestReportsLanding.aspx page either. I added GridView1.Databind() to the Page_Load of CustomerLanding.aspx and still no records. I don't need to put it in any kind of condition, right? Like if (gridveiw1.rows.count > 0) or anything, correct? – CuriousOne Mar 22 '16 at 18:34
  • You should just need to add the databind method inside your button click. Right under where you're using MessageBoxes in your original question should be fine. Take it out of the page_load method. – BenM Mar 22 '16 at 18:35
  • Might also be worthwhile to add EmptyDataText="No data found!" to your gridview, and ditch the label. The gridview can handle that for you. – BenM Mar 22 '16 at 18:37
  • Thanks, but still same behavior. And thanks for the tip about EmptyDataText. I have a lot to learn yet, I'm learning as I go out of a book :) – CuriousOne Mar 22 '16 at 18:47
  • Just make sure you're not using the databind method inside the page_load method. That'll usually cause undesirable side-effects. Still not working? – BenM Mar 22 '16 at 20:01
  • I did remove it from the page_load and it's still not working. – CuriousOne Mar 22 '16 at 20:22
0

Finally found an answer here:

empty gridview although the sqldatasource has values

Basically set CancelSelectOnNullParameter="false" on the SqlDatasource.

Community
  • 1
  • 1
CuriousOne
  • 51
  • 10