Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I've got the following exception when I try to run the code below:

Posted on 2011-09-23
7
Medium Priority
?
251 Views
Last Modified: 2012-05-12
Hi Experts,

I've got the following exception when I try to run the code below:
ExecuteReader: CommandText property has not been initialized
The first time it runs without errors, the exception occours when I recall it.

 conn = new SqlConnection(Constants.ConnectionDbSupporto);
                string queryString = "SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='" + nomeTab + "'";
                SqlCommand sqlcmd = new SqlCommand(queryString, conn);


                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                sqlcmd.CommandText = queryString;

                sqlDr = sqlcmd.ExecuteReader();
                while (sqlDr.Read())
                {
                    ...                }

                sqlDr.Close();

               
0
Comment
Question by:dionisio_79
  • 4
  • 2
7 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36587186
You mean like the following?

SomeFn()
{
conn = new SqlConnection(Constants.ConnectionDbSupporto);
                string queryString = "SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='" + nomeTab + "'";
                SqlCommand sqlcmd = new SqlCommand(queryString, conn);


                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                sqlcmd.CommandText = queryString;

                sqlDr = sqlcmd.ExecuteReader();
                while (sqlDr.Read())
                {
                    ...                }

                sqlDr.Close();
}

SomeOtherFn()
{
  SomeFn();  //OK
  SomeFn();  //Now fails with error
}

Open in new window

0
 
LVL 4

Expert Comment

by:theHollow
ID: 36587487
Hello
Try this instead:

SqlCommand sqlcmd = conn.CreateCommand();

:-)
0
 

Author Comment

by:dionisio_79
ID: 36587590
I've tryed with the createcommand method but the result is still the same

The method is called in the ExportForm_load method which is the event handler for the form load event.
this.Load += new EventHandler(ExportForm_Load);

the second time, when I press a button, I recall the load method:

  this.ExportForm_Load(this, new EventArgs());
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36587673
BAD.

Have a function that performs this code.
Call the function from the Load event and from the button event.
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 36587688
eg.  (not real code)

SomeFn()
{
.... //your database code goes here
}

FormLoad()
{
SomeFn();
}

ButtonClick()
{
SomeFn();
}
0
 

Author Comment

by:dionisio_79
ID: 36588154
I've changed the code as you suggested but I've got the same result
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 2000 total points
ID: 36588673
Please post your now actual code - there is nothing obviously wrong with the code you have posted.  In other words it should work, unless you have edited the problem line out that is.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question