locked
Binding WCF result (List<>) to GridView RRS feed

  • Question

  • User140495362 posted

    Hi i have created a service to display oracle table data into gridview in asp page. The service is working perfectly and returning a List<> which contains table data. But on the front-end( asp page) i am unable to bind the list which i am returning through service to GridView. Below is the code of the service.

    public List<Employee> getEmployees()

            {

    List<Employee> employees = new List<Employee>();

    string cs = "DATA SOURCE=172.25.4.115;PERSIST SECURITY INFO=True;USER ID=SAMPLEPROJECT;Password=SampleProject";

    OracleConnection conn = new OracleConnection();

                conn.ConnectionString = cs;

                conn.Open();

    OracleCommand cmd = new OracleCommand();

                cmd.Connection = conn;

    string stmt = "SELECT * FROM MyEmployees";

                cmd.CommandText = stmt;

    OracleDataReader reader = cmd.ExecuteReader();      

    while (reader.Read())

                {

    Employee employee = new Employee();

                    employee.EmployeeId =

    Convert.ToInt32(reader["EMP_ID"]);

                    employee.FirstName =

    Convert.ToString(reader["FIRST_NAME"]);

                    employee.LastName =

    Convert.ToString(reader["LAST_NAME"]);            

                    employees.Add(employee);

         }

    return employees.ToList();

            }

    Above is the service which is returning a List.. Now how can i bind the method with GridView ???

    Monday, October 21, 2013 5:54 AM

Answers

  • User346117707 posted

    Go to your web application.

    1.Add Service reference -> Give your service url -> click "Go" -> IF there is no error then your service added into your application -> you can give service name ex(Service1)
    2.Create the object of your service in the aspx.cs page (service1."YourClassName" obj = new service1."YourClassName"() ) Example (service1.EmployeesClass = new service1.EmployeesClass())
    3.Call your method like -> var employeeList = obj.getEmployees();
    4.Gridview1.DataSource = getEmployees(); -> Gridview1.DataBind();

    For More

    http://msdn.microsoft.com/en-us/library/bb628652.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 6:48 AM
  • User-211766943 posted

    Use data property EmployeeId  instead of EMP_ID

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 7:21 AM
  • User-211766943 posted

    FirstName for FIRST_NAME and LastName  for LAST_NAME 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 7:22 AM
  • User-211766943 posted

    Set Datafield as EmployeeId 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 7:23 AM
  • User-211766943 posted

    try this 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField HeaderText="Employee Id" DataField="EmployeeId" />
                    <asp:BoundField HeaderText="First Name" DataField="FirstName " />
                    <asp:BoundField HeaderText="Last name" DataField="LastName" />
                </Columns>
            </asp:GridView>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 7:24 AM

All replies

  • User346117707 posted
    You can directly bind a list with grid view

    For example you are having the gridview with name "Gridview1" means you can bind like below

    Gridview1.DataSource = getEmployees();
    Gridview1.DataBind();

    you can create the custom column/ autogenerate columns in your gridview.

    For more
    http://stackoverflow.com/questions/17148583/asp-net-bind-a-list-to-a-gridview
    http://forums.asp.net/t/1109165.aspx
    Monday, October 21, 2013 6:16 AM
  • User-211766943 posted

    Create refrence for service by adding service reference. Create intsnace of refrence you will find your getEmployees() method in that call it and bind output to gridview.

    Monday, October 21, 2013 6:17 AM
  • User346117707 posted

    Go to your web application.

    1.Add Service reference -> Give your service url -> click "Go" -> IF there is no error then your service added into your application -> you can give service name ex(Service1)
    2.Create the object of your service in the aspx.cs page (service1."YourClassName" obj = new service1."YourClassName"() ) Example (service1.EmployeesClass = new service1.EmployeesClass())
    3.Call your method like -> var employeeList = obj.getEmployees();
    4.Gridview1.DataSource = getEmployees(); -> Gridview1.DataBind();

    For More

    http://msdn.microsoft.com/en-us/library/bb628652.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 6:48 AM
  • User140495362 posted

    Thanks but when i am using below code it gives error as it says the first field "EMP_ID"(Column name of oracle table) which i have assigned to gridview column1 with DataField property is not found or unknown.

    Gridview1.DataSource = getEmployees();

     Gridview1.DataBind();

     

    And when i remove DataBind() statement it displays blank page.

    Monday, October 21, 2013 7:11 AM
  • User346117707 posted

    You should give the Gridview1.DataBind(); property this is only bind your datasource with gridview. 

    You said you are getting error. Can you post your code. both gridview and bind...

    Monday, October 21, 2013 7:17 AM
  • User-211766943 posted

    Use data property EmployeeId  instead of EMP_ID

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 7:21 AM
  • User140495362 posted

    This is the code of asp page -

    protected void Page_Load(object sender, EventArgs e)

            {

                DBReference.GetDataClient obj = new DBReference.GetDataClient();

    var mylist = obj.getEmployees();

                GridView1.DataSource = mylist;  or    GridView1.DataSource = obj.getEmployees();

                GridView1.DataBind();

            }

    and below is the source Code of asp page -

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

    <Columns>

    <asp:BoundField HeaderText="Employee Id" DataField="EMP_ID"/>   

    <asp:BoundField HeaderText="First Name" DataField="FIRST_NAME"/>

    <asp:BoundField HeaderText="Last name" DataField="LAST_NAME"/>

    </Columns>      

    </asp:GridView>

    Where, EMP_ID, FIRST_NAME, LAST_NAME are column names of oracle table

    Monday, October 21, 2013 7:21 AM
  • User-211766943 posted

    FirstName for FIRST_NAME and LastName  for LAST_NAME 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 7:22 AM
  • User-211766943 posted

    Set Datafield as EmployeeId 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 7:23 AM
  • User-211766943 posted

    try this 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField HeaderText="Employee Id" DataField="EmployeeId" />
                    <asp:BoundField HeaderText="First Name" DataField="FirstName " />
                    <asp:BoundField HeaderText="Last name" DataField="LastName" />
                </Columns>
            </asp:GridView>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 7:24 AM
  • User140495362 posted

    Thanks Shridhar Sir.. i replaced column names with Properties.. Now data is getting displayed.. :)

    Thanks everyone..

    Monday, October 21, 2013 7:25 AM