Dropdownlist and Paging

Hi!

I am trying to execute different "Where" statements using a dropdownlist and using a datagrid to view each result. It works fine when first populate the datagrid but when you use the paging and index to the next set of data it does not pass the correct SQL query. Whatever am doing wrong I know am overlooking something. Thanks...

If you have SQL Server with Northwind database you can try it use the code below.

Code C#: (change database parameters)

string dbPath = "server=localhost;uid= ;pwd= ;database=Northwind;";

string sqlQuery = "SELECT employeeid, lastname, firstname, title FROM employees";

int iTitle;

SqlConnection cnObj;

SqlDataAdapter daObj;

DataSet dsObj;

public void dgData()

{

try

{

dgGrid.DataSource = getData(sqlQuery);

dgGrid.DataBind();

}

catch (Exception e)

{

dgGrid.CurrentPageIndex = 0;

}

int pageIndex = dgGrid.CurrentPageIndex + 1;

lblPageView.Text = "Page Viewing " + pageIndex.ToString() + " of " + dgGrid.PageCount.ToString();

lblRctCnt.Text = "Request Found: " + dsObj.Tables[0].Rows.Count;

}

public DataSet getData(string sqlQuery)

{

cnObj = new SqlConnection(dbPath);

cnObj.Open();

daObj = new SqlDataAdapter(sqlQuery, cnObj);

dsObj = new DataSet();

daObj.Fill(dsObj);

return dsObj;

}

public void btnGo_Click(Object s, EventArgs e)

{

iTitle = ddlTitle.SelectedIndex;

switch (iTitle)

{

case 0:

sqlQuery = sqlQuery + " WHERE title = '" + ddlTitle.SelectedItem.Text + "'";

break;

case 1:

sqlQuery = sqlQuery + " WHERE title = '" + ddlTitle.SelectedItem.Text + "'";

break;

case 2:

//do nothing...

break;

}

dgData();

}

public void dg_Chg(Object s, DataGridPageChangedEventArgs e)

{

dgGrid.CurrentPageIndex = e.NewPageIndex;

dgData();

}

Create the following webcontrols:

DropDownList - ddlTitle (ListItem: Value = 0 Sales Representative, Value= 1 Sales Manager, Value= 2 Show all)

DataGrid - dgGrid

Button - btnGo

Label (lblPageView, lblRctCnt)



Answer this question

Dropdownlist and Paging

  • Ade S. Miller - MSFT

    hi,

    i guess you have to fill your dropdownlist if your page is not postback like that you will have all the employees names at once

    you can ask in asp.net forums for webpages developing http://forums.asp.net

    hope this helps



  • Dropdownlist and Paging