简体   繁体   中英

SQL Server procedure results not populating on Gridview

I am trying to implement a search function with a few input parameters to search on. I have a stored procedure which accepts these parameters to perform the search. When I run the page, Grid view doesn't show. Please guide me in understanding where I might be wrong.

Mark up:

<asp:Content ID="SearchContent" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
  <table width="100%" align="center" style="margin: auto; border-style: solid; border-width: medium;
        line-height: normal;">
    <tr>
      <td height="10" width="10%">
        &nbsp;</td>
      <td height="10">
        <asp:Table ID="Table1" runat="server" GridLines="none">
          <asp:TableRow>
            <asp:TableCell>
              <asp:Label runat="server" Visible="true" title="lblInvoiceNumber" ID="lblInvoiceNumber" Style="font-family: Arial, Helvetica, sans-serif; font-size: large; font-weight: bold;
                                color: #B6A77E">Invoice Number:</asp:Label>
            </asp:TableCell>
            <asp:TableCell ColumnSpan="4">
              <div>
                <asp:TextBox ID="txtInvoiceNumber" runat="server" Width="250px"></asp:TextBox>
              </div>
            </asp:TableCell>
          </asp:TableRow>
          <asp:TableRow>
            <asp:TableCell>
              <asp:Label runat="server" Visible="true" title="lblVendorName" ID="lblVendorName" Style="font-family: Arial, Helvetica, sans-serif; font-size: large; font-weight: bold;
                                color: #B6A77E">Vendor Name:</asp:Label>
            </asp:TableCell>
            <asp:TableCell ColumnSpan="4">
              <div>
                <asp:TextBox ID="txtVendorName" runat="server" Width="250px"></asp:TextBox>
              </div>
            </asp:TableCell>
          </asp:TableRow>
          <asp:TableRow>
            <asp:TableCell>
              <asp:Label runat="server" Visible="true" title="lblTransmittal" ID="lblTransmittal" Style="font-family: Arial, Helvetica, sans-serif; font-size: large; font-weight: bold;
                                color: #B6A77E">Transmittal Date:</asp:Label>
            </asp:TableCell>
            <asp:TableCell>
              <asp:Label runat="server" Visible="true" title="lblFrom" ID="lblFrom" Style="font-family: Arial, Helvetica, sans-serif;
                                font-size: medium; font-weight: bold; color: #B6A77E">From:</asp:Label>
            </asp:TableCell>
            <asp:TableCell>
              <asp:TextBox ID="txtFrom" runat="server" Width="165px"></asp:TextBox>
            </asp:TableCell>
            <asp:TableCell>
              <div class="overlay"></div>
              <div class="modal">
                <asp:Calendar ID="FromCalender" runat="server" OnSelectionChanged="FromCalender_SelectionChanged" Visible="False" BackColor="White" BorderColor="#999999" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px" CellPadding="4"
                  DayNameFormat="Shortest">
                  <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                  <SelectorStyle BackColor="#CCCCCC" />
                  <WeekendDayStyle BackColor="#FFFFCC" />
                  <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                  <OtherMonthDayStyle ForeColor="#808080" />
                  <NextPrevStyle VerticalAlign="Bottom" />
                  <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
                  <TitleStyle BackColor="#E4BA70" BorderColor="Black" Font-Bold="True" />
                </asp:Calendar>
              </div>
            </asp:TableCell>
            <asp:TableCell>
              <asp:ImageButton ID="btnfrom" runat="server" ImageUrl="Images/calender.png" OnClick="btnFrom_Click" Height="20" Width="20" />
            </asp:TableCell>
          </asp:TableRow>
          <asp:TableRow>
            <asp:TableCell>
            </asp:TableCell>
            <asp:TableCell>
              <asp:Label runat="server" Visible="true" title="lblTo" ID="lblTo" Style="font-family: Arial, Helvetica, sans-serif;
                                font-size: medium; font-weight: bold; color: #B6A77E">To:</asp:Label>
            </asp:TableCell>
            <asp:TableCell>
              <asp:TextBox ID="txtTo" runat="server" Width="165px"></asp:TextBox>
            </asp:TableCell>
            <asp:TableCell>
              <div class="overlay"></div>
              <div class="modal">
                <asp:Calendar ID="ToCalender" runat="server" OnSelectionChanged="ToCalender_SelectionChanged" Visible="False" BackColor="White" BorderColor="#999999" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px" CellPadding="4" DayNameFormat="Shortest">
                  <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                  <SelectorStyle BackColor="#CCCCCC" />
                  <WeekendDayStyle BackColor="#FFFFCC" />
                  <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                  <OtherMonthDayStyle ForeColor="#808080" />
                  <NextPrevStyle VerticalAlign="Bottom" />
                  <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
                  <TitleStyle BackColor="#E4BA70" BorderColor="Black" Font-Bold="True" />
                </asp:Calendar>
              </div>
            </asp:TableCell>
            <asp:TableCell>
              <asp:ImageButton ID="btnTo" runat="server" ImageUrl="Images/calender.png" OnClick="btnTo_Click" Height="20" Width="20" />
            </asp:TableCell>
          </asp:TableRow>
          <asp:TableRow>
            <asp:TableCell ColumnSpan="5">
              <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" Style="border: thin solid #E4BA70;
                                cursor: hand; background-color: #E4BA70; font-family: Arial, Helvetica, sans-serif;
                                font-size: medium; font-weight: bold; color: #FFFFFF;" />
            </asp:TableCell>
          </asp:TableRow>
        </asp:Table>
      </td>
      <td height="10" width="10%">
        &nbsp;</td>
    </tr>
    <tr>
      <td height="10" colspan="3">
        <asp:Label ID="Error" runat="server" Text="Label" Visible="False" Font-Names="Arial" ForeColor="#FF3300"></asp:Label>
      </td>
    </tr>
    <tr>
      <td height="10" colspan="3">

        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
          <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
          <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
          <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
          <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <EditRowStyle BackColor="#999999" />
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

      </td>
    </tr>
  </table>
</asp:Content>

Code behind:

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

private void DoSearch()
{
    using (SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["local"].ToString()))
    {                
        myConnection.Open();

        using (SqlCommand searchcmd = new SqlCommand("dbo.SearchInvoice", myConnection))
        {                    
            try
            {
                searchcmd.CommandType = CommandType.StoredProcedure;

                searchcmd.Parameters.Add(new SqlParameter("@VendorName", SqlDbType.VarChar));
                searchcmd.Parameters["@VendorName"].Value = txtVendorName.Text;
                searchcmd.Parameters.Add(new SqlParameter("@Invoice", SqlDbType.VarChar));
                searchcmd.Parameters["@Invoice"].Value = txtInvoiceNumber.Text;
                searchcmd.Parameters.Add(new SqlParameter("@TransmittalFrom", SqlDbType.VarChar));
                searchcmd.Parameters["@TransmittalFrom"].Value = txtFrom.Text;
                searchcmd.Parameters.Add(new SqlParameter("@TransmittalTo", SqlDbType.VarChar));
                searchcmd.Parameters["@TransmittalTo"].Value = txtTo.Text;

                SqlDataReader reader = searchcmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        GridView1.EmptyDataText = "No records Found";
                        GridView1.DataSource = reader;
                        GridView1.DataBind();
                    }
                }

                reader.Close();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally 
            {  
                myConnection.Close(); 
            }
        }
    }
}

SQL Server procedure:

ALTER PROCEDURE [dbo].[SearchInvoice]
    @VendorName VARCHAR(max),
    @TransmittalFrom VARCHAR(max), 
    @TransmittalTo VARCHAR(max),
    @Invoice VARCHAR(max)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT *
    FROM dbo.vw_InvoiceTransmittal
    WHERE (@VendorName IS NULL OR VendorName = @VendorName)
       AND (@Invoice IS NULL OR InvoiceNumber = @Invoice)
       AND (@TransmittalFrom IS NULL
            AND @TransmittalTo IS NULL
            OR TransmittalDate BETWEEN @TransmittalFrom AND @TransmittalTo)
    ORDER BY TransmittalDate
END

Your code need below changes:

1.Remove GridView1.DataBind(); in btnSearch_Click

protected void btnSearch_Click(object sender, EventArgs e)
{
    DoSearch();
}

2.In DoSearch() replace this following snippet:

  DataTable table = new DataTable();
  table.Load(searchcmd.ExecuteReader());
  GridView1.DataSource = table;
  GridView1.DataBind();

In place of below code: (basically you don't need to iterate through reader using while loop for your gridview binding scenario)

          SqlDataReader reader = searchcmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    GridView1.EmptyDataText = "No records Found";
                    GridView1.DataSource = reader;
                    GridView1.DataBind();
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM