473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Select from multiple tables

Hi.
myds.Reset();
mycommand.SelectCommand.CommandText=
"Select att1 from Ing as Ingredient, Pro as Product "+
"where Pro.ad='apple' and Pro.id=Ing.id";
mycommand.Fill(myds, "Product"); // Here is the problem
listbox.DataSource = myds.Tables["Product"].DefaultView; // Here again
listbox.DataTextField = "invid"; // Here again
listbox.DataBind();
mycon.Close()
////////////////////
As you see, I want to put data coming as a result of joining of tables into
a dataset. Then I want to display the result of the join in a list box;
because the result will be one column.

Please help me, I am just a beginner
Nov 16 '05 #1
1 11536
Ahmet,

What you are doing is not returning multiple tables. You are returning a
single table with the column att1. If you want to have multiple tables
returned, you need to use multiple select statements. Your best bet
would be to use a stored procedure in this case.

Stored Proc Example:
<code>
In SQL Server (or whatever RDBMS you are using):

CREATE PROCEDURE MultTableExample AS

SELECT col1, col2, col3 FROM Table1
SELECT col1, col2, col3 FROM Table2

GO
</code>

in C#:
<code>
SqlConnection connection = new SqlConnection("connection string");

DataTable table1 = new DataTable("Table1");
DataTable table2 = new DataTable("Table2");
DataSet multTables = new DataSet("MultTables");

SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT col1, col2, col3
FROM Table1",connection);
dataAdapter.Fill(table1);

dataAdapter.SelectCommand.CommandText = "SELECT col1, col2, col3 FROM
Table2";
dataAdapter.Fill(table2);

multTables.Tables.Add(table1);
multTables.Tables.Add(table2);

listbox.DataSource = multTables.Tables["Table2"];
listbox.DataTextField = "col1";
listbox.DataBind();
</code>

If you don't want to use stored procedures, then you can do it this way.
<code>
SqlConnection connection = new SqlConnection("connection string");
DataSet multTables = new DataSet("MultTables");
SqlDataAdapter dataAdapter = new SqlDataAdapter("MultTableExample
",connection);
dataAdapter.SelectCommand.CommadType = CommandType.StoredProcedure;
dataAdapter.Fill(multTables);

listbox.DataSource = multTables.Tables["Table2"];
listbox.DataTextField = "col1";
listbox.DataBind();
</code>

HTH,
~d

Ahmet Karaca wrote:
Hi.
myds.Reset();
mycommand.SelectCommand.CommandText=
"Select att1 from Ing as Ingredient, Pro as Product "+
"where Pro.ad='apple' and Pro.id=Ing.id";
mycommand.Fill(myds, "Product"); // Here is the problem
listbox.DataSource = myds.Tables["Product"].DefaultView; // Here again
listbox.DataTextField = "invid"; // Here again
listbox.DataBind();
mycon.Close()
////////////////////
As you see, I want to put data coming as a result of joining of tables into
a dataset. Then I want to display the result of the join in a list box;
because the result will be one column.

Please help me, I am just a beginner

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mfyahya | last post by:
I have two tables, both containing an 'authors' column. Is there a way to get a unique list of authors from the two tables? I tried SELECT DISTINCT `authors` from `table1`, `table2`; but I got an...
0
by: jamiemcc | last post by:
Hi, I would like to have 1 stored procedure call another stored procedure (which contains multiple select statements) and then be able to access the 3 result sets. Example Create Procedure ....
6
by: Eric Robinson | last post by:
Hi all, I'm having real trouble wrapping my newbie brain around this problem. Can someone please tell me the most efficient (or any!) way to write a SELECT statement to return a set of rows from...
3
by: syounger | last post by:
Hi. I have a report in Access 2000 that is based on selection made from a series of interdependent list boxes. The boxes I have right now are Source, Table, Column, Date. The user chooses Source...
3
by: rallykarro | last post by:
Hi, How do I at the best way perform select statements over multiple databases? I have a couple of databases containing the same table definitions with diffrent data. Now I want them to act...
2
by: chopin | last post by:
I am using Microsoft Access, and VBA. I was wondering if it was possible to select multiple tables using DAO. For example, here is the code I am thinking should work, but doesn't: sSQL =...
1
by: nullGumby | last post by:
I'm trying to get a UNION of UserIDs from multiple subselects. The original query--which puts all the found UserIDs into separate columns, looks like this: ...
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have coded multiple select statements in a single stored procedure, and when I execute this procedure on SQL Server Management Express, I correctly get multiple result sets. But, if I try to add...
5
by: MARIEDB2 | last post by:
Hello, I am struggling to build a query on a DB2 db wich does an update for multiple fields which are the result of a select query with where clause (using multiple tables in the condition). ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.