locked
display data in grid-view RRS feed

  • Question

  • User639567535 posted

     i am going to minimize no. of code lines and directly bind instead of add columns when i do this show error

     public DataTable vehcile(string id)
            {
                Entities1 wr = new Entities1();
                GridView1.DataSource=wr.spvechile(id);
                GridView1.DataBind();
                return ;
            }
     protected void Page_Load(object sender, EventArgs e)
            {
               if(!this.IsPostBack)
                {
                    this.vehcile(id);
                }
    
            }

    this show error

    Error 10 An object of a type convertible to 'System.Data.DataTable' is required
    Error 9 The name 'id' does not exist in the current context now how i return data and how i set data source?

    any solution's?

    Tuesday, August 30, 2016 6:00 AM

Answers

  • User1724605321 posted

    Hi ,

    Bakhtawar Ashiq

    Error 10 An object of a type convertible to 'System.Data.DataTable' is required

    What is your spvechile function doing ? You could convert your entity information to a DataTable object , after that bind gridview with the DataTable object:

    http://stackoverflow.com/a/11156328 

    http://stackoverflow.com/questions/13606343/entity-class-to-datatable 

    Bakhtawar Ashiq

    Error 9 The name 'id' does not exist in the current context now how i return data and how i set data source?

    In your page_load event . you haven't defined the "id"  parameter .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 30, 2016 7:37 AM
  • User-1404113929 posted

    hi,

    replace bellow code in vehcile method.

    Entities1 wr = new Entities1();
                GridView1.DataSource=wr.spvechile(id).Select(x=>new {x.Name,x.Id}).CopyToDataTable();
                GridView1.DataBind();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2016 8:16 AM

All replies

  • User1724605321 posted

    Hi ,

    Bakhtawar Ashiq

    Error 10 An object of a type convertible to 'System.Data.DataTable' is required

    What is your spvechile function doing ? You could convert your entity information to a DataTable object , after that bind gridview with the DataTable object:

    http://stackoverflow.com/a/11156328 

    http://stackoverflow.com/questions/13606343/entity-class-to-datatable 

    Bakhtawar Ashiq

    Error 9 The name 'id' does not exist in the current context now how i return data and how i set data source?

    In your page_load event . you haven't defined the "id"  parameter .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 30, 2016 7:37 AM
  • User-1404113929 posted

    hi,

    replace bellow code in vehcile method.

    Entities1 wr = new Entities1();
                GridView1.DataSource=wr.spvechile(id).Select(x=>new {x.Name,x.Id}).CopyToDataTable();
                GridView1.DataBind();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2016 8:16 AM