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();
6
  • 3
    Does it really have any relevant records in the database ? maybe you can also think of passing the name as a parameter Commented Mar 20, 2012 at 12:53
  • i did , still no return appear in the label , so i think something wrong with the Reader Part . Commented Mar 20, 2012 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. Additionally, you should utilize the IDispoable interface via using(). Commented Mar 20, 2012 at 12:58
  • From your code I can't see if the command is connected to the connection. Is it connected right? Commented Mar 20, 2012 at 13:04
  • haha ok i'll get rid of that :D , Thanks for the help Bobby Commented Mar 20, 2012 at 13:07

4 Answers 4

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

i changed it from GetString() to GetInt32() ,still no return value , and the column type is INT
Have you made sure that there actually is a record that matches x?
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.
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 :-)
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().

Comments

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

2 Comments

Nope, your code wouldn't work, as you're not using reader, but Reader to actually read the value.
yes. thanks. of course you have to write reader.GetString(0); instead of Reader.GetString(0);
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.