call function with parameters in the Page Load .NET - c#

I'm doing a filter form by 3 criteria, I'm having problems on the pagination since I do not load the data from the second page of the GridView, apparently I have to call my function that fills the GridView in the Load page event, but this function receives parameters that are the ones that the user enters in the search filter, How can I then call the function sending the parameters to that function in the page load event every time the page is changed in the GridView?
function
public DataTable AdvertSearch(string tittle, DateTime datel, DateTime date2)
{
SqlConnection con = new SqlConnection(Util.GetConnectionString("ConnectionString"));
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select*from table where titAdvert=#tittle or dateStart=#dateS" or dateEnd=#dateE;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#tittle", tittle== "" ? (object)DBNull.Value : tittle);
cmd.Parameters.AddWithValue("#dateS", date1== "" ? (object)DBNull.Value : date1);
cmd.Parameters.AddWithValue("#dateE", date2== "" ? (object)DBNull.Value : date2);
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
search button
protected void BtnSearch_Click(object sender, EventArgs e)
{
GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
GridView1.DataBind();
}

You need to specify a pageIndexChanging event to trigger the grid to be reloaded when the user clicks to the next page. Check out the code below:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
GridView1.DataBind();
}
And please write your code in english, as it makes it more readable for all programmers.

Related

ASP.NET C# GridView paging options

I am currently attempting to make gridview load 1 question at a time, when I set the paging to 1 it gives me this error.
Also using the VS15 SQL server
Try using an SqlAdapter instead of SqlDataReader. The reason is because you cannot Page back if you use SqlDataReader. SqlAdapter supports bidirectional traversal.
You were not using a Data table to retrieve the results form the select query. Use this instead
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
loadgrid();
}
}
private void loadgrid() {
con.Open();
cmd.CommandText = "SELECT TOP 4 * FROM [Question] ORDER BY NEWID()";
cmd.Connection = con;
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
GridView1.DataSource = dt;
GridView1.AllowPaging = true;
GridView1.DataBind();
}

label does not behave properly

I am devleoping a online airline reservation system in which i have two dropdownlists to select source and destinations and a label .this label will show " there are no flights" if there are no matching routes retrieved from the database (in this case its sqlserver 2008).i have written the following code which tries to do so, but when i postback or refresh the page the label with " there are no flights" is till visible.what is wrong with my code please anyone help me with that.
public partial class Dropdndemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("Data Source=KUNDAN-PC\\SQLEXPRESS;Initial Catalog=Ars2.1.2;Integrated Security=True");
//string Sqlcmnd="select Source from Ars2.1.2.dbo.Scheduling";
con.Open();
if (!Page.IsPostBack)
{
SqlCommand com = new SqlCommand("select distinct Source from Schedulings", con);
SqlCommand comn=new SqlCommand("select distinct Destination from Schedulings", con);
//SqlDataReader readr;
DropDownList1.DataSource = com.ExecuteReader();
DropDownList1.DataTextField = "Source";
// DropDownList1.DataTextField = "Destination";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "select Source");
con.Close();
con.Open();
DropDownList2.DataSource = comn.ExecuteReader();
DropDownList2.DataTextField = "Destination";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, "select Destination");
con.Close();
}
//con.Close();
// DropDownList1.DataBind();
//con.Close();
if (IsPostBack)
Label3.Text = "";
//Label1.Visible = false;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// string Source = DropDownList1.SelectedValue.ToString();
// Label1.Text = Source;
}
protected void Button1_Click(object sender, EventArgs e)
{
string src = DropDownList1.SelectedItem.ToString();
string desti = DropDownList2.SelectedItem.ToString();
if ((src == desti) && IsPostBack)
{
Label1.Text = "Source And Destination cant be same!";
}
SqlConnection lop = new SqlConnection("Data Source=KUNDAN-PC\\SQLEXPRESS;Initial Catalog=Ars2.1.2;Integrated Security=True");
lop.Open();
SqlCommand cmd = new SqlCommand("select * from Schedulings where Source=#Source and Destination=#Destination", lop);
cmd.Parameters.AddWithValue("Source", DropDownList1.SelectedItem.Text);
cmd.Parameters.AddWithValue("Destination", DropDownList2.SelectedItem.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
Label3.Text = "No planes available in this route!!!";
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
I suppose you are refreshing page using F5 or by right clicking on page and opting refresh option or using refresh button of browser. If this is the case then I am scared it's not refresh, it's repeating previous action. That means if you were searching for flight and refreshing using above options, it will again search for flight using same search criteria. You can confirm it by putting debugger on search event. You can avoid this behavior by setting and clearing Session or ViewState and manage label text using it.

Using SqlDataAdapter to page a SqlDataReader source

This question seems to be common and I went through this answer already.
Unfortunately, my page still isn't being paged. Here's what my code looks like in C#:
SqlCommand command = new SqlCommand("(SELECT ......", Connection);
SqlDataAdapter myAdapter = new SqlDataAdapter(command);
DataTable dt = new DataTable();
myAdapter.Fill(dt);
command.Connection = connection;
command.Connection.Open();
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.AllowPaging = true;
GridView1.PageSize = 15;
command.Connection.Close();
command.Connection.Dispose();
Unfortunately, when I do this, my paging doesn't show up. Am I doing something wrong?
Thanks
Set all of the Paging-related properties before the Databind() method is called. When you use Custom Paging you will have to handle the GridView1_PageIndexChanging event. You need to change the current PageIndex, and re-bind your GridView like this:
void bindGridview()
{
SqlCommand command = new SqlCommand("(SELECT ......", Connection);
SqlDataAdapter myAdapter = new SqlDataAdapter(command);
DataTable dt = new DataTable();
myAdapter.Fill(dt);
command.Connection = connection;
command.Connection.Open();
GridView1.AllowPaging = true;
GridView1.PageSize = 15;
GridView1.DataSource = dt;
GridView1.DataBind();
command.Connection.Close();
command.Connection.Dispose();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridview();
}
If you are also binding the GridView on Page_Load, do it like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
bindGridview();
}
You need to add the PageIndexChanging event of GridView to enable paging.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridview();
}

paging of gridview

i have a gridview where i have allowed paging.
but when i click second page the gridview disappears
here is the c# code :
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
OdbcDataAdapter adpState = new OdbcDataAdapter("SELECT CALL_NO,TDATE,
ID_NO,NAME,CONTACT,DEPARTMENT,ISSUE,STATUS FROM TBL_ITHELPDESK
WHERE (STATUS IS NULL OR STATUS <> 'CLOSED') AND TDATE= TO_DATE('" +
txtDate.Text.ToString().Trim() + "','MM-DD-YYYY')", con1);
DataSet ds = new DataSet();
adpState.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
can anyone help to find where i'm going wrong
You have to check multiple things.
Check the query with the one that is working, Is it Same?
Check the con1 variable is defined outside of any other method.
Define con1 in a class not in a method, something like that
odbcConnection con = new odbcConnection(ConectionString);

Gridview with DDl selected value

I had gridview which in load it will get data from database .And I added option for user to filter this grid view by DDl I did my code and the grid get data when load but when I selected DDl it didnot get any data and I made break point I noticed that Gridview1.Databind() hadnot any action on grid.So please any one help me
protected void Page_Load(object sender, EventArgs e)
{
DataTable DT = new DataTable();
if (DDlCity.SelectedIndex<0)
{
using (SqlConnection con = Connection.GetConnection())
{
SqlCommand Com = new SqlCommand("GetDealers", con);
Com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter DA = new SqlDataAdapter(Com);
DA.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
}
}
protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable DT = new DataTable();
using (SqlConnection con = Connection.GetConnection())
{
SqlCommand Com = new SqlCommand("GetDealersByArea", con);
Com.CommandType = CommandType.StoredProcedure;
Com.Parameters.Add(Parameter.NewInt("#DEALERAREA_ID", DDlCity.SelectedValue));
SqlDataAdapter DA = new SqlDataAdapter(Com);
DA.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
}
i suppose you got confused on what i said...no worries
here is a working example of you give example code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridFunction();
}
}
private void BindGridFunction()
{
try
{
DataTable DT = new DataTable();
using (SqlConnection con = Connection.GetConnection())
{
if(DDlCity.SelectedIndex <0)
{
SqlCommand Com = new SqlCommand("GetDealers", con);
Com.CommandType = CommandType.StoredProcedure;
}
else
{
SqlCommand Com = new SqlCommand("GetDealersByArea", con);
Com.CommandType = CommandType.StoredProcedure;
Com.Parameters.Add(Parameter.NewInt("#DEALERAREA_ID", DDlCity.SelectedItem.Value));
}
SqlDataAdapter DA = new SqlDataAdapter(Com);
DA.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
}
catch(Exception ex)
{
DT = null; // etc...etc.. clear objects created
}
}
protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
BindGridFunction();
}
I hope you get what i was trying to say. You can change the code according to you need.
Not tested yet but m sure will work.
Note : i woud suggest to use "DDlCity.SelectedItem.Value" instead of " DDlCity.SelectedValue"
In your post back you can put the binding code in condition
if (!IsPostBack){
// Bind grid here looking for or used call to function something like BindGrid()
}
and in BindGrid() function you can write binding code for grid view.
and on ddl selected index changed event you can again call the BindGrid() method to bind again accordingly.
also check that in your dropdownlist you have EnablePostBack - true.

Categories