Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new in LINQ & filling GridView through LINQ. Please see my code :

C#
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["mas"]);
    DataSet ds = new DataSet();
    string Query = string.Empty;

 private void Bind_LINQ_To_DataSet()
    {
        Query = "select * from dbo.Movie";
        SqlDataAdapter da = new SqlDataAdapter(Query, connection);
        da.Fill(ds, "dbo.Movie");
        var tbl = from p in ds.Tables["dbo.Movie"].AsEnumerable()
                  where p.Field<int>("ID") == Convert.ToInt32(TextBox1.Text)
                  select p;
            GridView1.Visible = true;
            GridView1.DataSource = tbl;
            GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Bind_LINQ_To_DataSet();
    }


My GridVIew comes empty. Any suggestion really appreciate.
Posted
Comments
That means the LINQ is not correct.

C#
Query = "select * from dbo.Movie";
        SqlDataAdapter da = new SqlDataAdapter(Query, connection);
        da.Fill(ds, "dbo.Movie");
        var tbl = from p in ds.Tables["dbo.Movie"].AsEnumerable()
                  where p.Field<int>("ID") == Convert.ToInt32(TextBox1.Text)
                  select new p;
            GridView1.Visible = true;
            GridView1.DataSource = tbl.Tabels[0].DefaultView;
            GridView1.DataBind();

or
C#
DataTable table = (DataTable)Session["Save"];
var query = from c in table.AsEnumerable()

select new { c.Week,c.Person,c.Guess,c.Actual};
GridView2.DataSource = query;

GridView2.DataBind();
 
Share this answer
 
v2
Comments
Mas11 20-Sep-13 5:03am    
Thanks !
May be you can try like this and bind the datatable to the Gridview datasource.

C#
DataTable movie = ds.Tables["dbo.Movie"];

IEnumerable<DataRow> query =
                  from movie in movie.AsEnumerable()
                  where movie.Field<int>("ID") == Convert.ToInt32(TextBox1.Text)
                  select movie;

// Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();
GridView1.Visible = true;
GridView1.DataSource = boundTable ;
GridView1.DataBind();
</datarow></int></datarow>
 
Share this answer
 
v2
Comments
Mas11 20-Sep-13 3:33am    
Yes, exactly.. Thanks a lot.
Thomas ktg 20-Sep-13 3:35am    
Happy to help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900