0

I created a web server to retrieve data from my sql database. For this case I wanted to retrieve all "Reds" from my table where there are Reds, Greens and yellows.

Here is an example of my PRODUCTS table:

enter image description here

So from this table I may have a query to get all the REDS from PRODUCT2, and my sql query would be:

SELECT PRODUCT2 from PRODUCTS where Type = 'Stop';

Here is my stack trace:

System.InvalidOperationException: ExecuteReader: Connection property has not been initialized.
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()

And this query works but when I create my web service and datahelper I get the error that my input string was incorrect and that my value is a string instead of of an int (which it should be)

Here is my web service code:

 [WebMethod]
   public int GetRed(int Type)
   {
      return DataHelper.GetRed(Type);
   }

Datahelper Code:

public static int GetRed(int Type)
{
   int PRODUCT1 = 0;
   var con = new SqlConnection(@"Data Source=TestComp\SQLEXPRESS; initial catalog=Database; integrated security=true;");

var cmd = new SqlCommand("Select PRODUCT1 from PRODUCTS where Type = 'Stop'");

con.Open();

SqlDataReader dr = cmd.ExecuteReader();

while(dr.Read())
{
   PRODUCT1 = (int)dr["PRODUCT1"];
}

dr.Close();
con.Close();

return PRODUCT1;

}

I tested the query in SQL Server and it works fine so not sure what I am doing wrong here when trying to retrieve those values. Any help is appreciated.

Thanks

4
  • Where are you getting this exception? While converting the db value to int? or somewhere else?
    – Nilesh
    Nov 8, 2013 at 14:37
  • Your sample query queries for PRODUCT2 but in your code you're querying for PRODUCT1. Are their datatypes different?
    – Cᴏʀʏ
    Nov 8, 2013 at 14:38
  • 1
    In the code above, you fail to tell the SqlCommand which SqlConnection it should use. This would lead to an exception with the message "Connection property has not been initialized." If your exception is coming from GetRed, I doubt it's coming from the version that you've posted above. If what's above is what you're executing, I don't think the error is coming from GetRed. Could you post a stack trace? Nov 8, 2013 at 14:58
  • @AlexHumphrey, I attached my stack trace. Nov 8, 2013 at 15:39

1 Answer 1

0

A lot of time wasted, with your stacktrace, I know the issue: You're not giving your command a connection.

You need this:

cmd.Connection = con;

Before

SqlDataReader dr = cmd.ExecuteReader();
2
  • I changed the line where I parse PRODUCT1 and I still get the same error. Really confused as to why I am getting an error. Not really sure what value I need to put in to test the operation. if I put in PRODUCT1 I get the error so maybe I am wrong there. Nov 8, 2013 at 15:59
  • @CSharpDev4Evr I have edited my answer, there's your problem.
    – Tobberoth
    Nov 8, 2013 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.