c# reading integer fields from database, returning empty string when reading integer type field

Posted by arnoldino on Stack Overflow See other posts from Stack Overflow or by arnoldino
Published on 2010-05-16T13:29:27Z Indexed on 2010/05/16 15:40 UTC
Read the original article Hit count: 420

Filed under:
|
|
|
|

what is wrong with this code?

field="id";
table="MInvMonth";
condition="machine_id=37";
 public static String getConditionedField(String field, String table, String condition)
    try
    {
        if (cmd == null) getConnection();
        cmd.CommandText = "Select " + field + " from " + table + " where " + condition;
        SQLiteDataReader reader = cmd.ExecuteReader();
        if (reader.HasRows==true)
        {
            reader.Read();
            string s = reader[0].ToString(); // return first element
            reader.Close();
            return s;
        }
        reader.Close();
        return null;
    }
    catch (Exception e)
    {
        MessageBox.Show("Caught exception: " + e.Message+"|"+cmd.CommandText);
        return null;
    }

I checked the sql statement, it turns the right value. why can't I read it? the returnvalue is "".

© Stack Overflow or respective owner

Related posts about c#

Related posts about database