1
Answer

The GridView 'GridView1' fired event...

Mufid Bagdadi

Mufid Bagdadi

8y
238
1
I have created a gridview in my web application but when i try to go to next page of grid view it gives an error, I am trying to fetch data from database through c# code.
following is the exception...
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
 
gridview asp Code,
 
 
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
AutoGenerateColumns="false" OnSelectedIndexChanged = "OnSelectedIndexChanged" AllowPaging="true"
PageSize="10" emptydatatext="No data available." >
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<Columns>
<asp:BoundField DataField="transactionid" HeaderText="Transaction ID"
ItemStyle-Width="150" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="empCode" HeaderText="Employee Code"
ItemStyle-Width="150" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="empName" HeaderText="Employee Name"
ItemStyle-Width="250" >
<ItemStyle Width="200px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="locationApproval" HeaderText="Status"
ItemStyle-Width="150" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" >
<ItemStyle Width="150px"></ItemStyle>
</asp:ButtonField>
</Columns>
</asp:GridView>
 
C# code:
 
protected void quality_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
head.Text = "Following are the employee code who had applied for 'Quality (Sigma - The mark of Quality)' award.";
head.Visible = true;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
string sql = null;
string connetionString = WebConfigurationManager.ConnectionStrings["umangDatabase"].ConnectionString;
sql = "select transactionid, empcode, empname, locationApproval from appData where awardtype='Quality (Sigma- The mark of the Quality)' and location='API_Thane'";
SqlConnection connection = new SqlConnection(connetionString);
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Transaction ID";
e.Row.Cells[1].Text = "Empoyee Name";
e.Row.Cells[2].Text = "Empoyee Code";
e.Row.Cells[3].Text = "Status";
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='#9A3334';";
e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
e.Row.ToolTip = "Click last column for selecting this row.";
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
//Accessing BoundField Column
string transID = GridView1.SelectedRow.Cells[0].Text;
Session["employee"] = transID;
Response.Redirect("print.aspx");
}
 
 
 
Answers (1)
0
Marcel Hertel

Marcel Hertel

NA 54 0 14y
hi,

assume that you have the columns emp_id and emp_name in your emp-table you can use the following procedure:

create procedure sp_Sortdata
(
    @Orderbyclause varchar(50)
)
as
select
    *
from
    emp e
order by   
    case when @Orderbyclause = 'emp_id' then e.emp_id end asc
    ,case when @Orderbyclause = 'emp_name' then e.emp_name end asc
   
if you execute exec sp_Sortdata 'emp_id' the procedure returns the emp-table ordered by emp_id

i hope this helps

Marcel Hertel