locked
how i can display this query in gridview RRS feed

  • Question

  • hi all.

       i have this method

     

            public    string      getProductBy()
            {
              


                var query = (from n in db.Products

                                 select new {  n.ProductName }).First();
              

                return query.ProductName;
            }

    i want to get the first product name in the database the query is returning the productname but what should i specify on text of <asp:Label id="lbl" Text=<%#Bind("WhatComeshere"%> runat="server"> which is in gridview so that productname should be displayed in gridview

     

    Friday, August 20, 2010 5:53 PM

Answers

  • Lets imagine that you want to return both the product name and product price.  Further I am going to show how to use a custom class called ProductModel (can be called anything).

     

    public class ProductModel
    {
      public string ProductName
      {
       get;
       private set;
      }
      public double Price
      {
       get;
       private set;
      }
    
      public ProductModel(string name, double price)
      {
       this.ProductName = name;
       this.Price = price;
      }
    }
    

     

    After creating this class, you will adjust your getProductBy method as follows:

     

    public ProductModel getProductBy()
    {
      var query = (from n in db.Products
         select new { n.ProductName, n.Price }).First();
       
      return new ProductModel(query.ProductName, query.Price);
    }
    

     

    And finally you will add an additional label tag to the asp.net page and modify your existing one like this:

    <asp:Label id="lbl" Text='<%#getProductBy().ProductName  %>'  runat="server"/>

    <asp:Label id="lbl" Text='<%#getProductBy().Price  %>'  runat="server"/>

     

    As an alternative to all this, you can also just remove the select new section of the linq statement and thereby return the entire Product object.  It all depends on how many "columns" the product object has and how efficient you want your page to be.

    Sunday, August 22, 2010 3:36 PM

All replies

  • Firstly what are you assigning as the datasource for the gridview?  The datasource will determine what should be placed in the bind statement on the label.

    Regards.

    Friday, August 20, 2010 7:28 PM
  • something like this to bind to the gridview

    <asp:Label id="lbl" Text='<%#getProductBy()  %>'  runat="server"/>

    Friday, August 20, 2010 7:38 PM
  • Firstly what are you assigning as the datasource for the gridview?  The datasource will determine what should be placed in the bind statement on the label.

    Regards.

    I am using object datasource control to get data and assigning its id to gridview datasourceid
    Saturday, August 21, 2010 1:46 PM
  • something like this to bind to the gridview

    <asp:Label id="lbl" Text='<%#getProductBy()  %>'  runat="server"/>


    what if i want to get two  column what will be  the query look like your way is fine if i have to return single column

     

    public    string      getProductBy()
            {
              


                var query = (from n in db.Products

                                 select new {  n.ProductName }).First();
              

                return query.ProductName;
            }

    Thanks

    Saturday, August 21, 2010 1:49 PM
  • To get two columns you will have to change the return type and return statement of the getProductBy() method.  You will need to add the second column to the select new { ... } portion of the linq statement.  For the return statement you will need to use an anonymous class type (or custom class) to return both of the columns' info.

    If both columns contain string values, then you can maybe return an array.

    Regards.

    Saturday, August 21, 2010 5:06 PM
  • To get two columns you will have to change the return type and return statement of the getProductBy() method.  You will need to add the second column to the select new { ... } portion of the linq statement.  For the return statement you will need to use an anonymous class type (or custom class) to return both of the columns' info.

    If both columns contain string values, then you can maybe return an array.

    Regards.

    can you please give me an suitable example. so it can be more clear to me.

    Thank you very much

    Sunday, August 22, 2010 1:24 PM
  • Lets imagine that you want to return both the product name and product price.  Further I am going to show how to use a custom class called ProductModel (can be called anything).

     

    public class ProductModel
    {
      public string ProductName
      {
       get;
       private set;
      }
      public double Price
      {
       get;
       private set;
      }
    
      public ProductModel(string name, double price)
      {
       this.ProductName = name;
       this.Price = price;
      }
    }
    

     

    After creating this class, you will adjust your getProductBy method as follows:

     

    public ProductModel getProductBy()
    {
      var query = (from n in db.Products
         select new { n.ProductName, n.Price }).First();
       
      return new ProductModel(query.ProductName, query.Price);
    }
    

     

    And finally you will add an additional label tag to the asp.net page and modify your existing one like this:

    <asp:Label id="lbl" Text='<%#getProductBy().ProductName  %>'  runat="server"/>

    <asp:Label id="lbl" Text='<%#getProductBy().Price  %>'  runat="server"/>

     

    As an alternative to all this, you can also just remove the select new section of the linq statement and thereby return the entire Product object.  It all depends on how many "columns" the product object has and how efficient you want your page to be.

    Sunday, August 22, 2010 3:36 PM