Solved

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

Posted on 2011-09-23
7
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 44

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 44

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 44

Accepted Solution

by:
AndyAinscow earned 500 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 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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