Hello Forum,
I have a GridView, textbox and “search” button. I want to display on the record that is typed in the textbox by clicking the search button. It is working but when I click the button when the textbox is empty, all the data in the table displays, making my GridView to increase its PageSize, which is set to 5 in the C# code window.
The issue is, if I click the search button when the textbook is empty the gridview displays all the records that is in the data table and by doing so, the gridview increases in height since all the records are displayed. The best thing would be for a script message to display that the textbox is empty
SQL
CREATE PROCEDURE [dbo].[Datareg]
@CreatedBy VARCHAR(50)
,@PageIndex INT
,@PageSize INT
,@RecordCount INT OUT
AS
BEGIN
SELECT ROW_NUMBER() OVER(ORDER BY email) RowNumber
,Invoice_no
,fname
,Tname
,email
,CreatedDate
INTO #Temp
FROM InvoiceTable
WHERE CreatedBy = @CreatedBy
SELECT @RecordCount = COUNT(*) FROM #Temp
SELECT * FROM #Temp
WHERE (RowNumber BETWEEN ((@PageIndex-1) * @PageSize) + 1 AND (@PageIndex * @PageSize)) OR @PageIndex = - 1
DROP TABLE #Temp
END
RETURN 0
C#
public partial class InvoiceRecords : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
private int PageSize = 5;
protected void Page_Load(object sender, EventArgs e)
{
createby.Visible = false;
if (Session["user"] == null)
{
Response.Redirect("LoginForm.aspx");
}
else
{
showdata1();
showdata();
user.Visible = true;
named.Visible = true;
showdata2();
}
if (!this.IsPostBack)
{
this.GetCustomersPageWise(1);
}
}
private void GetCustomersPageWise(int pageIndex)
{
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("Datareg", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CreatedBy", createby.Text.Trim());
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
}
}
}
}
private void Getsearch()
{
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand())
{
string sql = "SELECT Invoice_no, fname, Tname, email, CreatedDate FROM InvoiceTable";
if (!string.IsNullOrEmpty(Searchbox.Text.Trim()))
{
sql += " WHERE Invoice_no LIKE @Invoice_no + '%'";
cmd.Parameters.AddWithValue("@Invoice_no", Searchbox.Text.Trim());
}
cmd.CommandText = sql;
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
protected void btnsearch_Click(object sender, EventArgs e)
{
this.Getsearch();
}
}