This sample code is for populating a GridView from an oracle table. It uses OracleConnection OracleCommand OracleDataAdapter DataSet GridView Control
Code for PopulateGridViewEx1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PopulateGridViewEx1.aspx.cs" Inherits="PopulateGridViewEx1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> C# code for PopulateGridViewEx1.aspx.cs using System; using System.Web; public partial class PopulateGridViewEx1 : System.Web.UI.Page OracleConnection conn = new OracleConnection(oradb); OracleDataAdapter adapter = new OracleDataAdapter(cmd); When you are running the above code, you may get the following error: Compiler Error Message: CS0234: The type or namespace name 'OracleClient' does not exist in the namespace 'System.Data' For this error, you have to add reference to the oracle client by doing
the following clicks: Website menu>Add Reference..>.Net Tab > When the above thing is done, the following line is automatically included in your web.config file. <add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies>
Code of web.config <configuration>
Once the above code was made working, you can format the following thigs: 1. The Header Values of the grid are Just the Field Names of the Database Table. (eg. emp_id,emp_name). Instead of Emp_Name I want to display NAME OF EMPLOYEE. How this can be done. 2. Then,do you want to allow SORTING FACILITY In the propertites, just make ALLOWSORTNIG=TRUE Now the Datagrid source code will look like this: <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
But, when you click the sorting link,you may get the error: The GridView 'GridView1' fired event Sorting which wasn't handled If you are able to set the DataSourceID property of GridView, this problem will not be there. But I am using oracleclient and using the codes such as GridView1.DataSource=ds; GridView1.DataBind(); <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
But this is not Enough
3. Then do you want to display the report page by page when records are more. In the properties, just make ALLOWPAGING=TRUE
|
No comments:
Post a Comment