我正在尝试使用一些输入参数来实现搜索功能。我有一个存储过程接受这些参数来执行搜索。请指导我理解我可能出错的地方。当我运行页面时,网格视图不显示。 这是我的标记:
<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程序:
ALTER PROCEDURE [dbo].[SearchInvoice]
-- Add the parameters for the stored procedure here
@VendorName varchar(max),
@TransmittalFrom varchar(max),
@TransmittalTo varchar(max),
@Invoice varchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
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