Solved

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

Posted on 2011-09-23
7
241 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 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now