0

so this is my problem.

this function:

    public int Reader(string query, string value, int id, int reader)
    {
        CloseConnection();
        int i = 0;
        con.Open();
        MySqlCommand cmd;
        cmd = new MySqlCommand(query, con);
        cmd.Parameters.AddWithValue(value, id);
        cmd.ExecuteNonQuery();
        MySqlDataReader sdr = cmd.ExecuteReader();
        if (sdr.Read())
        {
            i = sdr.GetInt16(reader);
            con.Close();
        }
        return i;
    }

is supposed to data read mysql command and it works, excpet for this line:

 string str = Convert.ToString(mysqlquery.Reader("SELECT * FROM items WHERE id=@id", "@id", i, 2));

the other lines look exact the same, and only this one doesn't work.

This is how my database looks.

EDIT

replacing the 2 in the en of the line is oke

example:

  string str = Convert.ToString(mysqlquery.Reader("SELECT * FROM items WHERE id=@id", "@id", i, 1));
  • 1
    You are selecting 3 columns. how do you expect Convert to know to fetch 3 pieces if data from the reader? Read each data element first – Ňɏssa Pøngjǣrdenlarp Nov 07 '18 at 15:03
  • Well, column 2 (in 0-based column numbering) is the name - you're trying to fetch the name value with `GetInt16`, which is not going to work. – Jon Skeet Nov 07 '18 at 15:14
  • @JonSkeet could you please tell me how to fix it? –  Nov 07 '18 at 15:32
  • Well we don't know whether you're *trying* to read the name column, or whether you just have an off-by-one error. It's an odd method to start with though, as it can only read 16-bit integers... – Jon Skeet Nov 07 '18 at 15:34
  • @JonSkeet oh sorry I was being stupid the items in my database are string and I am asking my program to get a int i should change it to sdr.GetString(reader) –  Nov 07 '18 at 15:44

1 Answers1

0

Its a complex object, probably an array of sorts. Try accessing the index of the array, then doing the conversion to string.