 |
| 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
|
|
|
|

March 31st, 2004, 01:48 AM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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()
|
|

March 31st, 2004, 10:31 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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()
|
|

March 31st, 2004, 12:52 PM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
:)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()
|
|

March 31st, 2004, 03:12 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|

March 31st, 2004, 05:40 PM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
:)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()
|
|
 |