SqlDataAdapter with 2 queries doesnt give the right o/p
SqlDataAdapter adap = new SqlDataAdapter("Select * from Contentpagenav;Select * from Employees",conn);
DataSet ds = new DataSet();
conn.Open();
adap.TableMappings.Add("ContentPagenav", "Table1");
adap.TableMappings.Add("Employees", "Table2");
adap.Fill(ds, "Table1");
adap.Fill(ds, "Table2");
GridView1.DataSource = ds.Tables["Table1"];
GridView1.DataBind();
GridView2.DataSource = ds.Tables["Table2"];
GridView2.DataBind();
conn.Close();
I am getting the first table data in both the开发者_C百科 gridviews. What am I doing wrong?
did you tried by doing this
SqlDataAdapter adap = new SqlDataAdapter("Select * from Contentpagenav;Select * from Employees",conn);
DataSet ds = new DataSet();
conn.Open();
// don't map
//adap.TableMappings.Add("ContentPagenav", "Table1");
//adap.TableMappings.Add("Employees", "Table2");
//adap.Fill(ds, "Table1");
//adap.Fill(ds, "Table2");
// access tables by index instead of name
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView2.DataSource = ds.Tables[1];
GridView2.DataBind();
conn.Close();
Edit 1: There are some mistakes with your code
SqlDataAdapter adap = new SqlDataAdapter("Select * from Contentpagenav;Select * from Employees",conn);
DataSet ds = new DataSet();
conn.Open();
// the select statement only return Table, Table1, Table2 and so on
// it don't know About ContentPagenav or Employees
// so I am maping Table to MyTable1
//and Table1 to MyTable2
adap.TableMappings.Add("Table", "MyTable1");
adap.TableMappings.Add("Table1", "MyTable2");
// don't do this, it will discard your mapping,
// it will return
// Table1, Table2, and so on
//adap.Fill(ds, "Table1");
// just fill your dataset, and it will keep your mapping
adap.Fill(ds);
// access tables by your suggested name
GridView1.DataSource = ds.Tables["MyTable1"];
GridView1.DataBind();
GridView2.DataSource = ds.Tables["MyTable2"];
GridView2.DataBind();
conn.Close();
For more understandings Table Mapping in ADO.NET
精彩评论