3

How can i read the return value from "Select Where" statement , every time i run no return value appear in the label, and no syntax error.

command.CommandText = "select product_price from product where product_name='"+x+"';";
            connection.Open();
            Reader = command.ExecuteReader();
            while(Reader.Read()){


            Price_label.Content = "" + Reader.GetString(0);

            }
            connection.Close();
Robaticus
  • 22,857
  • 5
  • 54
  • 63
A.R
  • 173
  • 1
  • 7
  • 14
  • 3
    Does it really have any relevant records in the database ? maybe you can also think of passing the name as a parameter – V4Vendetta Mar 20 '12 at 12:53
  • i did , still no return appear in the label , so i think something wrong with the Reader Part . – A.R Mar 20 '12 at 12:56
  • 2
    You're from a VB6 background, are you? First off, C# is a static language, so get rid of that `"" + ` habit for typecasting. Secondly, you want to look [at parameterized queries](http://bobby-tables.com/). Additionally, you should utilize the `IDispoable` interface via `using()`. – Bobby Mar 20 '12 at 12:58
  • From your code I can't see if the command is connected to the connection. Is it connected right? – Steve Mar 20 '12 at 13:04
  • haha ok i'll get rid of that :D , Thanks for the help Bobby – A.R Mar 20 '12 at 13:07
  • Yes Steve , it is connected , all my problem is in reading the return value :S .. the label doesn't show any values when i run – A.R Mar 20 '12 at 13:08

4 Answers4

4

If the product_price column is not of type TEXT in MySQL, the Reader.GetString(0) will (depending on how the reader was implemented by Oracle) throw an Exception or return an empty string. I would think the latter is happening.

Retrieving the value through a DataReader requires you to know the data type. You can not simply read a string for every type of field. For example, if the field in the database is an Integer, you need to use GetInt32(...). If it is a DateTime use GetDateTime(...). Using GetString on a DateTime field won't work.

EDIT
This is how I'd write this query:

using (MySqlConnection connection = new MySqlConnection(...))
{
    connection.Open();
    using (MySqlCommand cmd = new MySqlCommand("select product_price from product where product_name='@pname';", connection))
    {
        cmd.Parameters.AddWithValue("@pname", x);
        using (MySqlDataReader reader = cmd.ExecuteReader())
        {
            StringBuilder sb = new StringBuilder();
            while (reader.Read())
                sb.Append(reader.GetInt32(0).ToString());

            Price_label.Content = sb.ToString();
        }
    }
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • i changed it from GetString() to GetInt32() ,still no return value , and the column type is INT – A.R Mar 20 '12 at 13:10
  • Have you made sure that there actually is a record that matches `x`? – Thorsten Dittmar Mar 20 '12 at 13:11
  • yea i tested it on Mysql Command and it worked , even in the code replaced X with a know product name , still no return value. – A.R Mar 20 '12 at 13:34
  • Just noticed that there was an error in the code - when creating the command, the second parameter must be `connection`, not `conn` as `conn` doesn't exist :-) – Thorsten Dittmar Mar 20 '12 at 13:44
3

To append to my comment, your approach has three problems which are not part of your problem:

So, a more correct version for your code would look like this:

// using utilizes the IDisposable-Interface, whcih exists to limit the lifetime
// of certain objects, especially those which use native resources which
// otherwise might be floating around.
using(YourConnectionType connection = new YourConnectionType("connectionstring"))
{
    connection.Open(); // You might want to have this in a try{}catch()-block.

    using(YourCommandType command = connection.CreateCommand())
    {
        command.CommandText = "select product_price from product where product_name=@NAME;";
        command.Parameters.Add("NAME", YourTypes.VarChar);
        command.Parameters[0].Value = x; // For your own sanity sake, rename that variable!

        using(YourReaderType reader = command.ExecuteReader())
        {
            while(reader.Read()) // If you're expecting only one line, change this to if(reader.Read()).
            {
                Price_label.Content = reader.GetString(0);
            }
        }
    }
} // No need to close the conenction explicit, at this point connection.Dispose()
  // will be called, which is the same as connection.Close().
Bobby
  • 11,419
  • 5
  • 44
  • 69
0

You should write @pname without '' otherwise it won't work.

instead of:

select product_price from product where product_name='@pname'

you should write like this:

select product_price from product where product_name=@pname

Tugrul
  • 76
  • 5
0

you have to create a variable of your reader

command.CommandText = "select product_price from product where product_name='"+x+"';";
try {
connection.Open();
SqlReader reader = command.ExecuteReader();
while(reader.Read()){


    Price_label.Content = "" + Reader.GetString(0);

}
} catch (Exception) {}
finally {
connection.Close();
}
Safari
  • 3,302
  • 9
  • 45
  • 64