Hi guys ..
I have a page with a drop down list, a button and a grid view. Depending on the drop down list option chosen, the grid view gets filled from the database. The problem I am facing arises when multiple entries spanning more than 1 page are returned - on clicking the second or another page index, the page loads again BUT the grid view page index and returned rows remain the same.
The code is as follows (I only submit the relevant code as there are many other details - irrelevant to this - on the page) :
.aspx code
<table style=" width:100%; height:23px; margin:0px; padding:0px; ">
<tr>
<td style=" width:16.67%; text-align:center; height:23px;" >
EMPLOYEE NAME
</td>
<td style=" width:33.34%; text-align:center; height:23px;">
<asp:DropDownList ID="DropDownList1" ToolTip="Employee Name" Height="23px" Width="100%" runat="server">
</asp:DropDownList>
</td>
</tr><tr>
<td colspan="2" style=" width:50%; height:23px; text-align:center; padding:10px;">
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
</td>
<tr>
<td style=" width:50%; height:200px; border-color:Black; border-style:inset;border-width:1px; text-align:center;">
<asp:GridView ID="GridView1" Height="200px" Width="100%" runat="server"
AutoGenerateColumns="False" PageSize="8" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="coType" HeaderText="Company Type"
SortExpression="coType" />
<asp:BoundField DataField="meetDate" DataFormatString="{0:d}" HeaderText="Meeting Date (mm/dd/yy)"
SortExpression="meetDate" />
<asp:BoundField DataField="clientname" HeaderText="Co Name"
SortExpression="clientname" />
<asp:BoundField DataField="pocName1" HeaderText="CR Name1"
SortExpression="pocName1" />
<asp:BoundField DataField="coStatus" HeaderText="Comp. Status"
SortExpression="coStatus" />
<asp:BoundField DataField="prodname" HeaderText="Prod Name"
SortExpression="prodname" />
<asp:BoundField DataField="targetDate" DataFormatString="{0:d}" HeaderText="Target Date(mm/dd/yy)"
SortExpression="targetDate" />
<asp:BoundField DataField="empLoginId" HeaderText="RM Id"
SortExpression="empLoginId" />
</Columns>
</asp:GridView>
</td>
</tr>
The .aspx.cs code is as follows -
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "empFname";
DropDownList1.DataValueField = "empLoginId";
DropDownList1.DataBind();
using (SqlConnection con1 = new SqlConnection())
{
con1.Open();
string u = Session["User"].ToString().ToLower();
using (SqlDataAdapter a = new SqlDataAdapter("SELECT [targetDate],[coType], [meetDate], [clientname], [pocName1], [coStatus], [prodname], [empLoginId] FROM [coMet] WHERE (([coType] = @coType) AND ([empLoginId]= @empLoginId))", con1))
{
a.SelectCommand.Parameters.AddWithValue("@empLoginId", u);
a.SelectCommand.Parameters.AddWithValue("@coType", "CLIENT");
DataTable t = new DataTable();
a.Fill(t);
GridView1.DataSource = t;
GridView1.DataBind();
con1.Close();
if (t.Rows.Count > 0)
{
GridView1.Visible = true;
}
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
set = "1";
string u = DropDownList1.SelectedValue.ToString().ToUpper();
using (SqlConnection con1 = new SqlConnection())
{
con1.Open();
using (SqlDataAdapter a = new SqlDataAdapter("SELECT [targetDate],[coType], [meetDate], [clientname], [pocName1], [coStatus], [prodname], [empLoginId] FROM [coMet] WHERE (([coType] = @coType) AND (([empLoginId]= @empLoginId) or ([empLoginId]= @empLogin)))", con1))
{
a.SelectCommand.Parameters.AddWithValue("@empLoginId", u);
a.SelectCommand.Parameters.AddWithValue("@empLogin", u.ToString().ToLower());
a.SelectCommand.Parameters.AddWithValue("@coType", "CLIENT");
DataTable t = new DataTable();
a.Fill(t);
GridView1.DataSource = t;
GridView1.DataBind();
con1.Close();
if (t.Rows.Count > 0)
{
GridView1.Visible = true;
}
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Button1_Click(sender, e);
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Somewhere ... there is some error - probably even a silly one ... but i'm an amateur coder :)
so please help out .. :)
p.s. the new sqlconnection part as been edited out .. for obvious reasons .. so that is not an error ....