Reputation: 401
I have 7 records in my database table which is called EntryTable. I set page limit to 5 records for each page. So page 1 will have 5 records, and page2 will have 2 records. But after I click Search All and click page 2, the whole gridview disappear.
After Searching for All
After clicking page 2
public partial class Search : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
bindResultGridView();
}
string memName = (String)Session["UserName"];
if (Session["Username"] != null && Session["Username"] != String.Empty)
{
//txtUserName.Text = "Welcome, " + memName + "!";
}
}
protected void SearchBlog(object sender, EventArgs e)
{
String ConStr = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(ConStr);
try
{
string invalid = txtSearch.Text;
String SQL = null;
if (invalid == "all")
{
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
}
else
{
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable] WHERE AdminNumber LIKE @searchAdminNumber OR BlogType LIKE @searchBlogType OR Name LIKE @searchName";
}
SqlCommand cmd = new SqlCommand(SQL, con);
con.Open();
cmd.Parameters.Add("@searchBlogType", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchBlogType"].Value = txtSearch.Text + "%";
cmd.Parameters.Add("@searchName", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchName"].Value = txtSearch.Text + "%";
cmd.Parameters.Add("@searchAdminNumber", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchAdminNumber"].Value = txtSearch.Text + "%";
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
grdResult.DataSource = dt;
grdResult.DataBind();
lblError.Text = "";
if (dt.Rows.Count > 0)
{
lblError.Text = null;
}
else
{
lblError.Text = "Record not found";
}
reader.Close();
}
catch (Exception)
{
lblError.Text = "Error!";
//lblOrderError.Text = ex.Message;
}
finally
{
con.Close();
}
}
private void bindResultGridView()
{
String ConStr = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(ConStr);
try
{
string invalid = txtSearch.Text;
String SQL = null;
if (invalid == "all")
{
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
}
else
{
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable] WHERE AdminNumber LIKE @searchAdminNumber OR BlogType LIKE @searchBlogType OR Name LIKE @searchName";
}
SqlCommand cmd = new SqlCommand(SQL, con);
con.Open();
cmd.Parameters.Add("@searchBlogType", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchBlogType"].Value = txtSearch.Text;
cmd.Parameters.Add("@searchName", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchName"].Value = txtSearch.Text;
cmd.Parameters.Add("@searchAdminNumber", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchAdminNumber"].Value = txtSearch.Text;
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
grdResult.DataSource = dt;
grdResult.DataBind();
lblError.Text = "";
reader.Close();
}
catch (SqlException ex)
{
lblError.Text = "Error:" + ex.Message.ToString();
}
finally
{
con.Close();
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
}
protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (string.Compare(e.CommandName, "Page", true) != 0)
{
Response.Redirect("~/ResultDetails.aspx?cat=" + e.CommandArgument);
}
}
protected void grdResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int newPageIndex = e.NewPageIndex;
grdResult.PageIndex = newPageIndex;
bindResultGridView();
}
}
EDITED
AFTER clicking on page 2
protected void Page_Load(object sender, EventArgs e)
{
bindResultGridView();
string memName = (String)Session["UserName"];
if (Session["Username"] != null && Session["Username"] != String.Empty)
{
//txtUserName.Text = "Welcome, " + memName + "!";
}
}
Upvotes: 2
Views: 1794
Reputation: 8184
It should work. Maybe the grdResult_PageIndexChanging
event doesn't fire
So create a break point on
int newPageIndex = e.NewPageIndex;
or on this line
grdResult.PageIndex = newPageIndex;
If this event doesn't run, maybe your result grid has not been attached this event. Please try to reattach it again and I think, chances will work.
EDITS:
finally edit with this
else if (invalid == "") {
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
}
Upvotes: 1