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

Initialize Connection error

RP
When I execute following code, this error is displayed :

ExecuteReader: Connection property has not been initialized, on line
4.

------------------------------------------------------------------------------------------
1 OpenConnection();
//Get Allowance Records
2 string qAllow = "Select ADCode from ADMaster where
ADType='A'";

3 SqlCommand AllCmd = new SqlCommand(qAllow, CN);

4 SqlDataReader dr = AllCmd.ExecuteReader();
-----------------------------------------------------------------------------------------

Aug 28 '07 #1
3 1371
On Aug 28, 1:55 pm, RP <rpk.gene...@gmail.comwrote:
When I execute following code, this error is displayed :

ExecuteReader: Connection property has not been initialized, on line
4.

---------------------------------------------------------------------------*---------------
1 OpenConnection();
//Get Allowance Records
2 string qAllow = "Select ADCode from ADMaster where
ADType='A'";

3 SqlCommand AllCmd = new SqlCommand(qAllow, CN);

4 SqlDataReader dr = AllCmd.ExecuteReader();
---------------------------------------------------------------------------*--------------

What does the OpenConnection() sub do?

Aug 28 '07 #2
RP
That problem got resolved by:

Public SQLConnection CN = new SQLConnection();

Previously I was just doing:

Public SQLConnection CN;

Now the next problem is here:

=======================================
//Get Allowance Records
string qAllow = "Select ADCode from ADMaster where
ADType='A'";

SqlCommand AllCmd = new SqlCommand(qAllow, CN);
SqlDataReader dr;
dr = AllCmd.ExecuteReader();
string qNew = "";

while (dr.Read()== true)
{
qNew = "ALTER TABLE NewT ADD " + dr[0].ToString() + "
varchar(12)";
SqlCommand Ins2 = new SqlCommand(qNew,CN);
Ins2.ExecuteNonQuery();
Ins2.Dispose();
}
dr.Close();
==================================

At line Ins2.ExecuteNonQuery.

Error: There is already an open DataReader associated with this
Command which must be closed first.
Aug 28 '07 #3
On Aug 28, 2:21 pm, RP <rpk.gene...@gmail.comwrote:
That problem got resolved by:

Public SQLConnection CN = new SQLConnection();

Previously I was just doing:

Public SQLConnection CN;

Now the next problem is here:

=======================================
//Get Allowance Records
string qAllow = "Select ADCode from ADMaster where
ADType='A'";

SqlCommand AllCmd = new SqlCommand(qAllow, CN);
SqlDataReader dr;
dr = AllCmd.ExecuteReader();
string qNew = "";

while (dr.Read()== true)
{
qNew = "ALTER TABLE NewT ADD " + dr[0].ToString() + "
varchar(12)";
SqlCommand Ins2 = new SqlCommand(qNew,CN);
Ins2.ExecuteNonQuery();
Ins2.Dispose();
}
dr.Close();
==================================

At line Ins2.ExecuteNonQuery.

Error: There is already an open DataReader associated with this
Command which must be closed first.
You cannot execute a nonquery on a connection that has a dataread
still open, just as the exception message says. If you want to execute
a nonquery while reading a datareader, you will need a second
connection. That's just how it works.

Aug 28 '07 #4

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

Similar topics

74
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
1
by: Dmitry Davletbaev | last post by:
Hi! I'm working on program that acts as MySQL client. After successful connection program send queries for a long time. When I stop server mysql_query return error (no surprise). But when I...
0
by: realjacky | last post by:
i cannot initialize a object of ADODB ADODB::Connection *con = new ADODB::Connection; i have add the reference in the reference list but it also not work Could anyone guide me a bit thx
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
0
by: sbartz | last post by:
Greetings, My ldap application relies on user input for a ldap server uri, so I am looking for a way to use the ldap api to determine whether the ldap server is up or not. All the docs have to...
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
1
by: =?Utf-8?B?SmFzb24=?= | last post by:
Hi all, I have set up ODBC connection through data source configuration wizard and I can run my application normally. But without any changes of my application & ODBC connection, the error is...
0
by: Robert Avery | last post by:
In VBA/VB6, I had a class (incomplete sample below) that watched and displayed for the user all connection events, so that I could easily see what SQL was taking a long time, and when it freezes, I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.