Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 31st, 2004, 01:48 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to connect multiple data source?

:)Hi, I tried to run two sql command in one form. Is that possible? and how?
Here followes my code:
it works when I just request one sql command. How can I add a more sql command, such as
Dim strAddition As String = "SELECT year, year_id FROM year"

  Dim strConnection As String = ConfigurationSettings.AppSettings("dan")
  objConnection = New OleDBConnection(strConnection)

  Dim strListBox = "SELECT season_descr, season_id FROM season"
  Dim objCommand As New OleDBCommand(strListBox, objconnection)
  objConnection.Open()
  LBSemester.DataSource = objCommand.ExecuteReader()
  LBSemester.DataTextField = "season_descr"
  LBSemester.DataValueField = "season_id"
  LBSemester.DataBind()
  objConnection.Close()

  #2 (permalink)  
Old March 31st, 2004, 10:31 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You certainly can. You just need to execute another query. Whether you require the use of a different command or connection object is entirely up to you and to the scenario at hand.

Dim strConnection As String = ConfigurationSettings.AppSettings("dan")
Dim objCommand As OleDBCommand(strListBox, objconnection)
objConnection = New OleDBConnection(strConnection)

Dim strListBox = "SELECT season_descr, season_id FROM season"
Dim strAddition As String = "SELECT year, year_id FROM year"

objCommand = New OleDBCommand(strListBox, objconnection)

objConnection.Open()

LBSemester.DataSource = objCommand.ExecuteReader()
LBSemester.DataTextField = "season_descr"
LBSemester.DataValueField = "season_id"
LBSemester.DataBind()

objCommand.CommandText = strAddition
'Do something here with objCommand.ExecuteReader()

objConnection.Close()
  #3 (permalink)  
Old March 31st, 2004, 12:52 PM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:)Thank you, Planoie,
It works just as you said.
But I have to close the connection and reopen it before I can execute the second sql command.
Sth. like that:

Dim strConnection As String = ConfigurationSettings.AppSettings("dan")
Dim objCommand As OleDBCommand(strListBox, objconnection)
objConnection = New OleDBConnection(strConnection)

Dim strListBox = "SELECT season_descr, season_id FROM season"
Dim strAddition As String = "SELECT year, year_id FROM year"

objCommand = New OleDBCommand(strListBox, objconnection)

objConnection.Open()

LBSemester.DataSource = objCommand.ExecuteReader()
LBSemester.DataTextField = "season_descr"
LBSemester.DataValueField = "season_id"
LBSemester.DataBind()
objConnection.Close()

objConnection.Open()
objCommand.CommandText = strAddition
'Do something here with objCommand.ExecuteReader()
LBYear.DataSource = objCommand.ExecuteReader()
LBYear.DataTextField = "Year"
LBYear.DataValueField = "Year_id"
LBYear.DataBind()

objConnection.Close()

  #4 (permalink)  
Old March 31st, 2004, 03:12 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Sorry, my code example wasn't completely correct:

You can use the same open connection if you close the datareader. You can only have one reader open at a time.

Easiest way is to Dim a DataReader object, set the result ExecuteReader() to that object, use it as the datasource to your listbox. Bind the control, close the reader, execute the second query, bind to second control. This way you don't need to close and reopen the connection.

Peter
------------------------------------------------------
Work smarter, not harder.
  #5 (permalink)  
Old March 31st, 2004, 05:40 PM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:)Hi, Peter,

I work it out as you suggestions. Great thanks.

Here's my code:

  Dim strConnection As String = ConfigurationSettings.AppSettings("dan")
  objConnection = New OleDBConnection(strConnection)

  Dim strListBox As String = "SELECT season_descr, season_id FROM season"
  Dim strListBox2 As String = "SELECT Year, Year_id FROM [year] ORDER BY year"

  Dim objCommand As New OleDBCommand(strListBox, objconnection)

  objConnection.Open()
  'Fill the first List Box Semester
  Dim objReader As OleDBDataReader = objCommand.ExecuteReader()
  LBSemester.DataSource = objReader
  LBSemester.DataTextField = "season_descr"
  LBSemester.DataValueField = "season_id"
  LBSemester.DataBind()
  objReader.Close()

  'Fill the second List Box Year
  objCommand.CommandText = strListBox2
  objReader = objCommand.ExecuteReader()
  LBYear.DataSource = objReader
  LBYear.DataTextField = "Year"
  LBYear.DataValueField = "Year_id"
  LBYear.DataBind()
  objConnection.Close()


 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Datasets from multiple Data Sources SQL_Hacker Reporting Services 1 June 18th, 2008 02:25 PM
Connect a package of SSIS (dtsx) like data source Edwin2008 SQL Server DTS 1 January 7th, 2008 11:49 PM
Selecting multiple elements from the source XML mkansal XSLT 15 June 18th, 2007 09:40 AM
How feasible to connect to multiple dbs in MySQL Janardhana M MySQL 2 June 7th, 2005 04:59 AM
How Can I #INCLUDE Multiple Source Files Like in A llyal2000 ASP.NET 1.0 and 1.1 Basics 2 May 24th, 2004 08:44 PM



All times are GMT -4. The time now is 05:23 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.