Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-23
7
Medium Priority
?
249 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
Industry Leaders: 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!

 
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 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 44

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

730 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