Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2004, 01:48 AM
Authorized User
 
Join Date: Mar 2004
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()

 
Old March 31st, 2004, 10:31 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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()
 
Old March 31st, 2004, 12:52 PM
Authorized User
 
Join Date: Mar 2004
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()

 
Old March 31st, 2004, 03:12 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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.
 
Old March 31st, 2004, 05:40 PM
Authorized User
 
Join Date: Mar 2004
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()







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





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