Dear Friends
I have a problem. I want to fill dataset by executing query on multiple tables and then capture data in comboboxes.
and also combox1.datasource=ds.tables["which table name or what should i write here?"]
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Suresh PaldiaPosted Nov 12, 2010, 3:50 AM
Suresh PaldiaPosted Nov 12, 2010, 3:48 AM
Mayur GujrathiPosted Nov 12, 2010, 2:19 AM
Suresh PaldiaPosted Nov 12, 2010, 2:12 AM
a.Fill(ds,"resulttable");
Now, in future you can refer/map to that table by using
ComboBox1.DataSource = ds.Table["resulttable"]
But in case my query itself is returning more than 1 tables, for example
string stmt = "SELECT name FROM Customer
SELECT ordernumber FROM Order
SELECT recieptnumber FROM RecieveDetails";
Or you might use a stored procedure that returns multiple tables.
Code above will result in 3 DataTables called "Table", "Table1", "Table2" by default if i use -
a.Fill(ds);
ComboBox1.DataSource = ds.Tables["Table"];
ComboBox2.DataSource = ds.Tables["Table1"];
ComboBox3.DataSource = ds.Tables["Table2"];
And it will return 3 datatables called "resulttable", "resulttable1" and "resulttable2" if i use -
a.Fill(ds,"resulttable");
ComboBox1.DataSource = ds.Tables["resulttable"];
ComboBox2.DataSource = ds.Tables["resulttable1"];
ComboBox3.DataSource = ds.Tables["resulttable2"];
Accordingly, you will have to refer these tables by these names only.
But, i recommend you to add a TableMapping for each table returned by your statement.
You can do this as,
SqlDataAdapter da= new SqlDataAdapter(stmt, sqlConnection);
da.TableMappings.Add("Table", "Customer");
da.TableMappings.Add("Table1", "Order");
da.TableMappings.Add("Table2", "RecieveDetails");
da.Fill(dataset);
Then you can use;
ComboBox1.DataSource = ds.Tables["Customer"];
ComboBox2.DataSource = ds.Tables["Order"];
ComboBox3.DataSource = ds.Tables["RecieveDetails"];
More meaningful... isn't it!
Hope you got this concept...
For more about Table Mapping, you can visit
http://msdn.microsoft.com/en-us/library/ms810286.aspx
Happy Learning...
Mayur GujrathiPosted Nov 11, 2010, 11:43 PM
Whats This "resulttable". Please give me some brief exposure,Its Urgent. I didnt understood
Thank You
String query = "SELECT A.firstname FROM
EMPLOYEETABLE AS A
INNER/LEFT/RIGHT/FULL JOIN
ADDRESSTABLE AS B
ON somecondition";
SqlDataAdapter a = new SqlDataAdapter(query,con);
DataSet ds = new DataSet();
a.Fill(ds,"resulttable");
ComboBox1.DataSource = ds;
Suresh PaldiaPosted Nov 11, 2010, 7:35 AM
Example:
String query = "SELECT A.firstname FROM
EMPLOYEETABLE AS A
INNER/LEFT/RIGHT/FULL JOIN
ADDRESSTABLE AS B
ON somecondition";
SqlDataAdapter a = new SqlDataAdapter(query,con);
DataSet ds = new DataSet();
a.Fill(ds,"resulttable");
ComboBox1.DataSource = ds;
Mahesh ChandPosted Nov 11, 2010, 7:27 AM
comboBox1.DataSource = ds.Tables["Customers];
So what you need to do is, either create a relationship in your database and use the JOIN SQL query OR you can add a relationship in the DataRelation (you probably don't need this).
Manikavelu VelayuthamPosted Nov 11, 2010, 7:21 AM
There you can join multiple table and fetch the needed values.
Finally you can fill it in a dataset.