473,379 Members | 1,530 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,379 software developers and data experts.

problems adding a second table to a dataset

TB
Hi All:

As a newbie to ASP.NET, I think I have understood a dataset as a kind
of container where I can store several datatables to used during the
lifetime of a page. With that in mind I written the following code for
a page (simplified here):

Dim myConnection As MySqlConnection = New
MySQLConnection(""server=myserver.com; user id=myself; password=myself;
database=mydatabase; pooling=false;"
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet = New dataset
Dim Row As DataRow
Dim strSQL as String
Dim irecord as integer
Dim result1 as String
Dim result2 as String

'Loading the first table into the dataset
irecord = 3 'For the sake of this example
strSQL = "Select firstname, lastname from table1 where ID = " & irecord
MydataAdapter = New MysqlDataAdapter(StrSQL, MyConnection)
MyDataAdapter.Fill(MyDataSet, "tbl1")

'Loading the second table into the dataset
irecord = 28 'For the sake of this example
strSQL = "Select firstname, lastname from table2 where ID = " & irecord
MyDataAdapter.Fill(MyDataSet, "tbl2")

Debugging, I can see that after executing the above, MyDataSet contains
two tables (MyDataSet.Tables.Count), however they appear to contain the
same data because

if do this:
result1 = myDataSet.Tables("tbl1").Rows(0).Item(0) & " " &
myDataSet.Tables("tbl1").Rows(0).Item(1)
result2 = myDataSet.Tables("tbl2").Rows(0).Item(0) & " " &
myDataSet.Tables("tbl2").Rows(0).Item(1)

or this

result1 = myDataSet.Tables(0).Rows(0).Item(0) & " " &
myDataSet.Tables(0).Rows(0).Item(1)
result2 = myDataSet.Tables(1).Rows(0).Item(0) & " " &
myDataSet.Tables(1).Rows(0).Item(1)

it turns out that result1 and result2 contain the same data, namely
that of tbl1.

What am I doing wrong here?

Any advice will be highly appreciated.

Thanks,

TB

Jan 24 '06 #1
4 1280
Call the Fill method one time, with both SQL Statements as the CommandText,
one right after the other, and separated with a semicolon. Better yet, use a
Stored procedure that has two separate select statements.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"TB" wrote:
Hi All:

As a newbie to ASP.NET, I think I have understood a dataset as a kind
of container where I can store several datatables to used during the
lifetime of a page. With that in mind I written the following code for
a page (simplified here):

Dim myConnection As MySqlConnection = New
MySQLConnection(""server=myserver.com; user id=myself; password=myself;
database=mydatabase; pooling=false;"
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet = New dataset
Dim Row As DataRow
Dim strSQL as String
Dim irecord as integer
Dim result1 as String
Dim result2 as String

'Loading the first table into the dataset
irecord = 3 'For the sake of this example
strSQL = "Select firstname, lastname from table1 where ID = " & irecord
MydataAdapter = New MysqlDataAdapter(StrSQL, MyConnection)
MyDataAdapter.Fill(MyDataSet, "tbl1")

'Loading the second table into the dataset
irecord = 28 'For the sake of this example
strSQL = "Select firstname, lastname from table2 where ID = " & irecord
MyDataAdapter.Fill(MyDataSet, "tbl2")

Debugging, I can see that after executing the above, MyDataSet contains
two tables (MyDataSet.Tables.Count), however they appear to contain the
same data because

if do this:
result1 = myDataSet.Tables("tbl1").Rows(0).Item(0) & " " &
myDataSet.Tables("tbl1").Rows(0).Item(1)
result2 = myDataSet.Tables("tbl2").Rows(0).Item(0) & " " &
myDataSet.Tables("tbl2").Rows(0).Item(1)

or this

result1 = myDataSet.Tables(0).Rows(0).Item(0) & " " &
myDataSet.Tables(0).Rows(0).Item(1)
result2 = myDataSet.Tables(1).Rows(0).Item(0) & " " &
myDataSet.Tables(1).Rows(0).Item(1)

it turns out that result1 and result2 contain the same data, namely
that of tbl1.

What am I doing wrong here?

Any advice will be highly appreciated.

Thanks,

TB

Jan 24 '06 #2
TB
How exactly (using my sample code as a reference)?

Does that mean that I cannot use the Fill method more than once for
each dataset? (not very practical, because one may not know both SQL
statements at the same time - the second statement may depend on
additional code to be correctly generated.)

TB

Jan 24 '06 #3
Hi,

I am no expert but I think that you need to reload MydataAdapter with
contents of 2nd SQL query before adding it to the dataset

TABLE1:
strSQL = "Select firstname, lastname from table1 where ID = " & irecord
MydataAdapter = New MysqlDataAdapter(StrSQL, MyConnection)
MyDataAdapter.Fill(MyDataSet, "tbl1")
TABLE2: strSQL = "Select firstname, lastname from table2 where ID = " & irecord
(ADD THIS LINE HERE)
MydataAdapter= New MysqlDataAdapter(StrSQL,MyConnection)
MyDataAdapter.Fill(MyDataSet, "tbl2")


HTH
Ken
Jan 25 '06 #4
TB
You got it right. Thanks a lot.

Cheers,

TB

Jan 25 '06 #5

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

Similar topics

10
by: Eric Petruzzelli | last post by:
If I fill my dataset and there is no data. The dataset is still created with zero rows (all columns are there). When I add my first row using the script below, it takes over 2 seconds to add??? If...
0
by: Duncan Spence | last post by:
Hi all, I'm sure I'm doing something silly here, but can't see it! I'm creating a series of combo boxes on a Windows Form in VB.NET. The lists of all of the comboboxes are identical and are...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
0
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a...
13
by: Lars Netzel | last post by:
hi! myDataSet that is fillled from an Access 2000 db and includes ONE table From that Table in myDataSet I create myDataView and use a Rowfilter to get a few rows that i work with (i need a...
16
by: Geoff Jones | last post by:
Hi Can anybody help me with the following, hopefully simple, question? I have a table which I've connected to a dataset. I wish to add a new column to the beginning of the table and to fill...
1
by: Manuel Canas | last post by:
Hey guys, This code was posted by Cor Ligther some rows below here. hey bud if you read this one can you help me out here or anybody that know the answer to this one. How would I add a second...
2
by: Niels Jensen | last post by:
I have the following code in a Sub which is called by a do loop statement for each line starting with unit info in an e-mail based game that I play. I'm exctracting the keywords from the text and...
6
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.