How to Get Data From Data Set
-
Hello Everybody, I have two tables Emp and Dept in DataSet. In which have followind fields.
Emp Dept
Emp_No Emp_No
Emp_Name Dept_No
Emp_Sal Dep_NameNow i want the result in datagridview from both dataset. Result will be look like this.....
Result_In_dataGridview
Emp_no Emp_Name Emp_Sal Dept_no Dept_Name
So please describe me how can i do that by using Code. Thanks
If you can think then I Can.
-
Hello Everybody, I have two tables Emp and Dept in DataSet. In which have followind fields.
Emp Dept
Emp_No Emp_No
Emp_Name Dept_No
Emp_Sal Dep_NameNow i want the result in datagridview from both dataset. Result will be look like this.....
Result_In_dataGridview
Emp_no Emp_Name Emp_Sal Dept_no Dept_Name
So please describe me how can i do that by using Code. Thanks
If you can think then I Can.
You need to construct a SQL JOIN between the two tables. Here is a simple example which combines the two into a new table:
static void Main(string\[\] args) { Console.WriteLine("Started"); SqlConnection con = new SqlConnection(strCon); con.Open(); SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT \* FROM myTable", con); SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT \* FROM myOtherTable", con); SqlDataAdapter daJoined = new SqlDataAdapter("SELECT \* FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con); DataSet ds = new DataSet(); daMyTable.Fill(ds, "My Table"); daMyOtherTable.Fill(ds, "My Other Table"); daJoined.Fill(ds, "My Joined Table"); ShowDataSet(ds); con.Close(); Console.WriteLine("Stopped"); Console.ReadLine(); } private static void ShowDataSet(DataSet ds) { foreach (DataTable dt in ds.Tables) { Console.WriteLine(dt.TableName); ShowDataTable(" ", dt); } } private static void ShowDataTable(string p, DataTable dt) { int cols = dt.Columns.Count; foreach (DataRow dr in dt.Rows) { Console.Write(p); string sep = ""; for (int i = 0; i < cols; i++) { Console.Write(sep + dr\[i\].ToString()); sep = ", "; } Console.WriteLine(); } }
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
Hello Everybody, I have two tables Emp and Dept in DataSet. In which have followind fields.
Emp Dept
Emp_No Emp_No
Emp_Name Dept_No
Emp_Sal Dep_NameNow i want the result in datagridview from both dataset. Result will be look like this.....
Result_In_dataGridview
Emp_no Emp_Name Emp_Sal Dept_no Dept_Name
So please describe me how can i do that by using Code. Thanks
If you can think then I Can.
-
You need to construct a SQL JOIN between the two tables. Here is a simple example which combines the two into a new table:
static void Main(string\[\] args) { Console.WriteLine("Started"); SqlConnection con = new SqlConnection(strCon); con.Open(); SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT \* FROM myTable", con); SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT \* FROM myOtherTable", con); SqlDataAdapter daJoined = new SqlDataAdapter("SELECT \* FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con); DataSet ds = new DataSet(); daMyTable.Fill(ds, "My Table"); daMyOtherTable.Fill(ds, "My Other Table"); daJoined.Fill(ds, "My Joined Table"); ShowDataSet(ds); con.Close(); Console.WriteLine("Stopped"); Console.ReadLine(); } private static void ShowDataSet(DataSet ds) { foreach (DataTable dt in ds.Tables) { Console.WriteLine(dt.TableName); ShowDataTable(" ", dt); } } private static void ShowDataTable(string p, DataTable dt) { int cols = dt.Columns.Count; foreach (DataRow dr in dt.Rows) { Console.Write(p); string sep = ""; for (int i = 0; i < cols; i++) { Console.Write(sep + dr\[i\].ToString()); sep = ", "; } Console.WriteLine(); } }
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
just use innerjoin .. select Emp.*, Dept.* from Emp inner join Dept on Emp.Emp_No = Dept.Emp_No
-
You need to construct a SQL JOIN between the two tables. Here is a simple example which combines the two into a new table:
static void Main(string\[\] args) { Console.WriteLine("Started"); SqlConnection con = new SqlConnection(strCon); con.Open(); SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT \* FROM myTable", con); SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT \* FROM myOtherTable", con); SqlDataAdapter daJoined = new SqlDataAdapter("SELECT \* FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con); DataSet ds = new DataSet(); daMyTable.Fill(ds, "My Table"); daMyOtherTable.Fill(ds, "My Other Table"); daJoined.Fill(ds, "My Joined Table"); ShowDataSet(ds); con.Close(); Console.WriteLine("Stopped"); Console.ReadLine(); } private static void ShowDataSet(DataSet ds) { foreach (DataTable dt in ds.Tables) { Console.WriteLine(dt.TableName); ShowDataTable(" ", dt); } } private static void ShowDataTable(string p, DataTable dt) { int cols = dt.Columns.Count; foreach (DataRow dr in dt.Rows) { Console.Write(p); string sep = ""; for (int i = 0; i < cols; i++) { Console.Write(sep + dr\[i\].ToString()); sep = ", "; } Console.WriteLine(); } }
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT * FROM myTable", con); SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT * FROM myOtherTable", con); SqlDataAdapter daJoined = new SqlDataAdapter("SELECT * FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con); I am curious why did you get the data seperately instead of using a one query with a "join", getting all the data all once and bing it to gridview?
-
SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT * FROM myTable", con); SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT * FROM myOtherTable", con); SqlDataAdapter daJoined = new SqlDataAdapter("SELECT * FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con); I am curious why did you get the data seperately instead of using a one query with a "join", getting all the data all once and bing it to gridview?
Purely to show what I was doing, and that I had two tables in the database. Seemed worth doing for a beginner in this stuff... :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."