【发布时间】:2015-12-29 19:14:05
【问题描述】:
在 asp.net 中使用存储过程实现动态搜索时遇到问题 我有一个用于考勤系统的数据表和数据库 这是我的 Default.aspx.cs 代码
public partial class _Default : Page
{
static string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
this.gridData();
this.search();
}
}
//--------------------------------------------------
private void gridData()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT EventUserId, EventDate, EventTime, Eventtype, Readerid FROM V_EventLogs"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
DataTable dt = new DataTable();
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
private DataTable search()
{
DataTable sdt = new DataTable();
SqlConnection con = new SqlConnection(constr);
try
{
SqlCommand cmd = new SqlCommand("userSearch", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 300;
cmd.Parameters.AddWithValue("@EventUserId", TextBox1.Text);
cmd.Parameters.AddWithValue("@EventDateFrm", ASPxDateEdit1.Text);
cmd.Parameters.AddWithValue("@EventDateTo", ASPxDateEdit2.Text);
cmd.Parameters.AddWithValue("@Readerid", txtReader.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
// passs parameter to data adapter
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(sdt);
}
catch (Exception ex) {
Response.Write(ex);
}
return sdt;
}
//---------------------------------------------------
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.gridData();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
GridView1.DataSource = search();
GridView1.DataBind();
}
}
}
这是我的 Default.aspx 页面:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<div class="jumbotron">
<h1>BioWeb TimeTel</h1>
<p class="lead">Time Attendance</p>
<%--<p><a href="http://www.asp.net" class="btn btn-primary btn-lg">Learn more »</a></p>--%>
</div>
<div>
User ID:
<asp:TextBox ID="TextBox1" runat="server" CssClass="form-control"></asp:TextBox>
<br />
<br />
Event Date:
<br />
<dx:ASPxDateEdit ID="ASPxDateEdit1" runat="server" DisplayFormatString="MM/dd/yyyy" EditFormatString="MM/dd/yyyy">
</dx:ASPxDateEdit>
<br />
Event Time:
<br />
<dx:ASPxDateEdit ID="ASPxDateEdit2" runat="server" DisplayFormatString="MM/dd/yyyy" EditFormatString="MM/dd/yyyy">
</dx:ASPxDateEdit>
<br />
<br />
Reader:
<asp:TextBox ID="txtReader" runat="server" CssClass="form-control" AutoCompleteType="Disabled"></asp:TextBox>
<br />
<br />
 
<br />
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" CssClass="btn btn-primary btn-lg" OnLoad="Page_Load" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" PageSize="10" UseAccessibleHeader="True" CssClass="table table-hover">
<Columns>
<asp:BoundField DataField="EventUserId" HeaderText="User ID" />
<asp:BoundField DataField="EventDate" HeaderText="Date IN" />
<asp:BoundField DataField="EventTime" HeaderText="Time IN" />
<asp:BoundField DataField="Eventtype" HeaderText="Type" />
<asp:BoundField DataField="Readerid" HeaderText="Reader" />
</Columns>
</asp:GridView>
</div>
我的存储过程是:
CREATE procedure userSearch
(
@EventUserId int = NULL,
@EventDateFrm nvarchar(10) = NULL,
@EventDateTo nvarchar(10) = NULL,
@Readerid int = NULL
)
as
select V_EventLogs.EventUserId,V_EventLogs.EventDate,V_EventLogs.EventTime,V_EventLogs.Eventtype,V_EventLogs.Readerid
from V_EventLogs
where
(@EventUserId is NULL OR EventUserId = @EventUserId )
AND
(@EventDateFrm is NULL OR Convert(varchar(10),EventDate,110) >= Convert(varchar(10),@EventDateFrm,110))
AND
(@EventDateTo is NULL OR Convert(varchar(10),EventDate,110) <= Convert(varchar(10),@EventDateTo,110))
AND
(@Readerid is NULL OR Readerid = @Readerid)
现在,当我想搜索员工时,网格数据表仍然是空的并且没有得到任何值,但是当我在 SQL Server 上测试我的存储过程运行良好时 - 第二个问题是当我选择任何其他页面时,分页数据表动态搜索仅适用于第一页,搜索实现不起作用并且数据表显示所有数据库值!为什么??
【问题讨论】:
-
为什么不在方法外声明 Datatable 并在从
gridData()方法返回数据后进行绑定更改签名以返回数据表也在 Page_Load 中有一个 else并将那里的数据也绑定到数据网格..应该是一个简单的修复..您还可以在调试时确认调用Fill方法时数据是否实际加载到数据表中 -
你的分页也应该是下面的
GridView1.CurrentPageIndex = e.NewPageIndex;然后像这样分配数据源和数据绑定GridView1.DataSource = sdt;//将数据表实例移到方法之外使其成为公共或静态GridView1.DataBind(); -
您熟悉 PostBacks..吗?但是在您的 Page_Load 事件中有一个断点,您会看到哪里出错了..您需要一个
if (IsPostBack) else { }您不需要检查 == false 要么做if(!IsPostBack){} else{}要么做if(IsPostBack){} else{}
标签: c# asp.net sql-server stored-procedures datatable