我正在尝试使用一些输入参数来实现搜索功能。我有一个存储过程接受这些参数来执行搜索。当我运行页面时,网格视图不显示。请指导我理解我可能出错的地方。
标记:
<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%">
</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%">
</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>
代码背后:
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程序:
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
答案 0 :(得分:0)
您的代码需要进行以下更改:
1.在GridView1.DataBind();
btnSearch_Click
protected void btnSearch_Click(object sender, EventArgs e)
{
DoSearch();
}
2.在DoSearch()
中替换以下代码段:
DataTable table = new DataTable();
table.Load(searchcmd.ExecuteReader());
GridView1.DataSource = table;
GridView1.DataBind();
代替下面的代码:(基本上你不需要为你的gridview绑定场景使用while循环遍历阅读器)
SqlDataReader reader = searchcmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
GridView1.EmptyDataText = "No records Found";
GridView1.DataSource = reader;
GridView1.DataBind();
}
}