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

multiple database connections?

i am binding a datagrid do a sqlcommand, which uses a sql connection.

fine.

if i want to run another query and bind another datagrid, do i have to
create new connections and commands? because i can't seem to get it to
work using just one connection
Nov 16 '05 #1
8 3973
You aren't binding to a Command or a SqlConnection per se. I'm guessing you
are using cmd.ExecuteReader witht he ASP.NET Web Grid? The main thing to
remember is close the connection as soon as you're done - because of pooling
you can create another connection at very little cost

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Mortar" <a@b.com> wrote in message
news:41***************@nntp.wlfdle.phub.net.cable. rogers.com...
i am binding a datagrid do a sqlcommand, which uses a sql connection.

fine.

if i want to run another query and bind another datagrid, do i have to
create new connections and commands? because i can't seem to get it to
work using just one connection

Nov 16 '05 #2
yes i am creating a connection and a command, then binding the
datagrid to the command's executeReader.

you suggest that creating another connection is little cost. Is this
because I can't use the existing connection? If I can use it, please
explain how. Thanks.
On Wed, 13 Oct 2004 19:36:44 -0400, "W.G. Ryan eMVP"
<Wi*********@NoSpam.gmail.com> wrote:
You aren't binding to a Command or a SqlConnection per se. I'm guessing you
are using cmd.ExecuteReader witht he ASP.NET Web Grid? The main thing to
remember is close the connection as soon as you're done - because of pooling
you can create another connection at very little cost

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Mortar" <a@b.com> wrote in message
news:41***************@nntp.wlfdle.phub.net.cable .rogers.com...
i am binding a datagrid do a sqlcommand, which uses a sql connection.

fine.

if i want to run another query and bind another datagrid, do i have to
create new connections and commands? because i can't seem to get it to
work using just one connection



Nov 16 '05 #3
What I'm actually advocating is not reusing the same one. You have the
postback to deal with so go ahead and just create the connection and call
executereader. Justmake sure you close that connection ie using
(SqlConnection cn = new SqlConnection()) so you know it gets disposed of.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Mortar" <a@b.com> wrote in message
news:41***************@nntp.wlfdle.phub.net.cable. rogers.com...
yes i am creating a connection and a command, then binding the
datagrid to the command's executeReader.

you suggest that creating another connection is little cost. Is this
because I can't use the existing connection? If I can use it, please
explain how. Thanks.
On Wed, 13 Oct 2004 19:36:44 -0400, "W.G. Ryan eMVP"
<Wi*********@NoSpam.gmail.com> wrote:
You aren't binding to a Command or a SqlConnection per se. I'm guessing youare using cmd.ExecuteReader witht he ASP.NET Web Grid? The main thing to
remember is close the connection as soon as you're done - because of poolingyou can create another connection at very little cost

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Mortar" <a@b.com> wrote in message
news:41***************@nntp.wlfdle.phub.net.cable .rogers.com...
i am binding a datagrid do a sqlcommand, which uses a sql connection.

fine.

if i want to run another query and bind another datagrid, do i have to
create new connections and commands? because i can't seem to get it to
work using just one connection


Nov 16 '05 #4

i know you are advocating creating a new one. What I am wondering is
why? Personal preference or because the same connection cannot
actually be used.

also, there is no postback, i am trying to do this all on the same
page load. I have a connection object, a command object, and 2
datagrids. I simply want to open a connection, run 2 queries to fill 2
grids, then close the connection. The error I get is that when it
tries to fill the second grid, it says there is a dataReader already
open associated with the connection, and it has to be closed first.
Thing is, i'm not using a datareader anywhere.

here is what i would like to do :

SqlConnection con = new SqlConnection([mydatabasedetails]);
SqlCommand cmd = new SqlCommand(SQL, con);
con.Open();

dgResults.DataSource = cmd.ExecuteReader();
dgResults.DataBind();

'now fill 2nd grid (dgResultsTwo):
'not sure how to do this on same connection and command object

con.Close();


On Thu, 14 Oct 2004 00:44:01 -0400, "W.G. Ryan eMVP"
<Wi*********@NoSpam.gmail.com> wrote:
What I'm actually advocating is not reusing the same one. You have the
postback to deal with so go ahead and just create the connection and call
executereader. Justmake sure you close that connection ie using
(SqlConnection cn = new SqlConnection()) so you know it gets disposed of.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Mortar" <a@b.com> wrote in message
news:41***************@nntp.wlfdle.phub.net.cable .rogers.com...
yes i am creating a connection and a command, then binding the
datagrid to the command's executeReader.

you suggest that creating another connection is little cost. Is this
because I can't use the existing connection? If I can use it, please
explain how. Thanks.
On Wed, 13 Oct 2004 19:36:44 -0400, "W.G. Ryan eMVP"
<Wi*********@NoSpam.gmail.com> wrote:
>You aren't binding to a Command or a SqlConnection per se. I'm guessingyou >are using cmd.ExecuteReader witht he ASP.NET Web Grid? The main thing to
>remember is close the connection as soon as you're done - because ofpooling >you can create another connection at very little cost
>
>--
>W.G. Ryan MVP (Windows Embedded)
>
>TiBA Solutions
>www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
>"Mortar" <a@b.com> wrote in message
>news:41***************@nntp.wlfdle.phub.net.cable .rogers.com...
>> i am binding a datagrid do a sqlcommand, which uses a sql connection.
>>
>> fine.
>>
>> if i want to run another query and bind another datagrid, do i have to
>> create new connections and commands? because i can't seem to get it to
>> work using just one connection
>
>



Nov 16 '05 #5
Hi Mortar,

The line of code:
dgResults.DataSource = cmd.ExecuteReader();

*is* opening a SqlDataReader. You need to close the SqlDataReader
before you can do anything else with the SqlCommand.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 14 Oct 2004 15:58:29 GMT, a@b.com (Mortar) wrote:

i know you are advocating creating a new one. What I am wondering is
why? Personal preference or because the same connection cannot
actually be used.

also, there is no postback, i am trying to do this all on the same
page load. I have a connection object, a command object, and 2
datagrids. I simply want to open a connection, run 2 queries to fill 2
grids, then close the connection. The error I get is that when it
tries to fill the second grid, it says there is a dataReader already
open associated with the connection, and it has to be closed first.
Thing is, i'm not using a datareader anywhere.

here is what i would like to do :

SqlConnection con = new SqlConnection([mydatabasedetails]);
SqlCommand cmd = new SqlCommand(SQL, con);
con.Open();

dgResults.DataSource = cmd.ExecuteReader();
dgResults.DataBind();

'now fill 2nd grid (dgResultsTwo):
'not sure how to do this on same connection and command object

con.Close();


On Thu, 14 Oct 2004 00:44:01 -0400, "W.G. Ryan eMVP"
<Wi*********@NoSpam.gmail.com> wrote:
What I'm actually advocating is not reusing the same one. You have the
postback to deal with so go ahead and just create the connection and call
executereader. Justmake sure you close that connection ie using
(SqlConnection cn = new SqlConnection()) so you know it gets disposed of.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Mortar" <a@b.com> wrote in message
news:41***************@nntp.wlfdle.phub.net.cabl e.rogers.com...
yes i am creating a connection and a command, then binding the
datagrid to the command's executeReader.

you suggest that creating another connection is little cost. Is this
because I can't use the existing connection? If I can use it, please
explain how. Thanks.
On Wed, 13 Oct 2004 19:36:44 -0400, "W.G. Ryan eMVP"
<Wi*********@NoSpam.gmail.com> wrote:

>You aren't binding to a Command or a SqlConnection per se. I'm guessing

you
>are using cmd.ExecuteReader witht he ASP.NET Web Grid? The main thing to
>remember is close the connection as soon as you're done - because of

pooling
>you can create another connection at very little cost
>
>--
>W.G. Ryan MVP (Windows Embedded)
>
>TiBA Solutions
>www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
>"Mortar" <a@b.com> wrote in message
>news:41***************@nntp.wlfdle.phub.net.cable .rogers.com...
>> i am binding a datagrid do a sqlcommand, which uses a sql connection.
>>
>> fine.
>>
>> if i want to run another query and bind another datagrid, do i have to
>> create new connections and commands? because i can't seem to get it to
>> work using just one connection
>
>



Nov 16 '05 #6

how do I close it when it is created in the way I did (no object
variable)? The only way I could get it to work was using the cmd
object to close the connection (cmd.Connection.Close), then setting
the cmd to the new query, then open the connection again. If there is
a better way of doing it, please let me know.

On Thu, 14 Oct 2004 14:08:27 -0400, Scott Allen
<bitmask@[nospam].fred.net> wrote:
Hi Mortar,

The line of code:
dgResults.DataSource = cmd.ExecuteReader();

*is* opening a SqlDataReader. You need to close the SqlDataReader
before you can do anything else with the SqlCommand.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 14 Oct 2004 15:58:29 GMT, a@b.com (Mortar) wrote:

i know you are advocating creating a new one. What I am wondering is
why? Personal preference or because the same connection cannot
actually be used.

also, there is no postback, i am trying to do this all on the same
page load. I have a connection object, a command object, and 2
datagrids. I simply want to open a connection, run 2 queries to fill 2
grids, then close the connection. The error I get is that when it
tries to fill the second grid, it says there is a dataReader already
open associated with the connection, and it has to be closed first.
Thing is, i'm not using a datareader anywhere.

here is what i would like to do :

SqlConnection con = new SqlConnection([mydatabasedetails]);
SqlCommand cmd = new SqlCommand(SQL, con);
con.Open();

dgResults.DataSource = cmd.ExecuteReader();
dgResults.DataBind();

'now fill 2nd grid (dgResultsTwo):
'not sure how to do this on same connection and command object

con.Close();


On Thu, 14 Oct 2004 00:44:01 -0400, "W.G. Ryan eMVP"
<Wi*********@NoSpam.gmail.com> wrote:
What I'm actually advocating is not reusing the same one. You have the
postback to deal with so go ahead and just create the connection and call
executereader. Justmake sure you close that connection ie using
(SqlConnection cn = new SqlConnection()) so you know it gets disposed of.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Mortar" <a@b.com> wrote in message
news:41***************@nntp.wlfdle.phub.net.cab le.rogers.com...
yes i am creating a connection and a command, then binding the
datagrid to the command's executeReader.

you suggest that creating another connection is little cost. Is this
because I can't use the existing connection? If I can use it, please
explain how. Thanks.
On Wed, 13 Oct 2004 19:36:44 -0400, "W.G. Ryan eMVP"
<Wi*********@NoSpam.gmail.com> wrote:

>You aren't binding to a Command or a SqlConnection per se. I'm guessing
you
>are using cmd.ExecuteReader witht he ASP.NET Web Grid? The main thing to
>remember is close the connection as soon as you're done - because of
pooling
>you can create another connection at very little cost
>
>--
>W.G. Ryan MVP (Windows Embedded)
>
>TiBA Solutions
>www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
>"Mortar" <a@b.com> wrote in message
>news:41***************@nntp.wlfdle.phub.net.cable .rogers.com...
>> i am binding a datagrid do a sqlcommand, which uses a sql connection.
>>
>> fine.
>>
>> if i want to run another query and bind another datagrid, do i have to
>> create new connections and commands? because i can't seem to get it to
>> work using just one connection
>
>


Nov 16 '05 #7
Hi Mortar:

Here is one of many possible ways you could do it, I think Bill was
alluding to this earlier:

using(SqlConnection connection = new SqlConnection(<connstring>))
{
connection.Open();

SqlCommand command = new SqlCommand();
command.Connection = connection;

command.CommandText = <query1>;
using(SqlDataReader reader = command.ExecuteReader())
{

DataGrid1.DataSource = reader;
DataGrid1.DataBind();
}

command.CommandText = <query2>;
using(SqlDataReader reader = command.ExecuteReader())
{

DataGrid2.DataSource = reader;
DataGrid2.DataBind();
}
}

The using clause ensures the SqlDataReader is properly closed before
it leaves scope.

--
Scott
http://www.OdeToCode.com/

On Thu, 14 Oct 2004 18:41:06 GMT, a@b.com (Mortar) wrote:

how do I close it when it is created in the way I did (no object
variable)? The only way I could get it to work was using the cmd
object to close the connection (cmd.Connection.Close), then setting
the cmd to the new query, then open the connection again. If there is
a better way of doing it, please let me know.


Nov 16 '05 #8

excellent. Thanks.
On Thu, 14 Oct 2004 15:28:27 -0400, Scott Allen
<bitmask@[nospam].fred.net> wrote:
Hi Mortar:

Here is one of many possible ways you could do it, I think Bill was
alluding to this earlier:

using(SqlConnection connection = new SqlConnection(<connstring>))
{
connection.Open();

SqlCommand command = new SqlCommand();
command.Connection = connection;

command.CommandText = <query1>;
using(SqlDataReader reader = command.ExecuteReader())
{

DataGrid1.DataSource = reader;
DataGrid1.DataBind();
}

command.CommandText = <query2>;
using(SqlDataReader reader = command.ExecuteReader())
{

DataGrid2.DataSource = reader;
DataGrid2.DataBind();
}
}

The using clause ensures the SqlDataReader is properly closed before
it leaves scope.

--
Scott
http://www.OdeToCode.com/

On Thu, 14 Oct 2004 18:41:06 GMT, a@b.com (Mortar) wrote:

how do I close it when it is created in the way I did (no object
variable)? The only way I could get it to work was using the cmd
object to close the connection (cmd.Connection.Close), then setting
the cmd to the new query, then open the connection again. If there is
a better way of doing it, please let me know.


Nov 16 '05 #9

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

Similar topics

2
by: Matthew Clubb | last post by:
Please help, a bit new to this! I've got two databases and I'm trying to look with values from one table in one database and then look up a name from the other database, the two tables are...
16
by: noah | last post by:
Does PHP have a feature to associate Cookie sessions with a persistent database connection that will allow a single transaction across multiple HTTP requests? Here is how I imagine my process: I...
6
by: Quiet Man | last post by:
Hi all, I'm designing a fairly simple service that will run on W2K/SP4 and W2K3 servers. It's job is to be a very specialized database server that listens on a given IP address / TCP port and...
6
by: mark | last post by:
I have an asp.net ecommerce web application on a remote web server. I'm using an Access database on the back end. I've notice a few strange things. When I mimic an multiple user environment by...
9
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated...
13
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've decided to break down in to lots of modules...
1
by: Yelena Varshal via AccessMonster.com | last post by:
Hello, What are the pre-requisites / conditions for the ability to create multiple connections to MS ACCESS database and what is the precedence of its application? adModeShareDenyNone in the code,...
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...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.