Link to home
Start Free TrialLog in
Avatar of dionisio_79
dionisio_79

asked on

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

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();

               
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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

Hello
Try this instead:

SqlCommand sqlcmd = conn.CreateCommand();

:-)
Avatar of dionisio_79
dionisio_79

ASKER

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());
BAD.

Have a function that performs this code.
Call the function from the Load event and from the button event.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've changed the code as you suggested but I've got the same result
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial