cannot implicitly convert type string to char

user2822113 picture user2822113 · Jan 16, 2014 · Viewed 41.1k times · Source

As I know char is different from string. I give datatype char to a column status in a table. I am stuck here because i am using char first time. here is my code

 cmd.CommandType = CommandType.StoredProcedure;
        if (con.State == ConnectionState.Closed)
            con.Open();
        MySqlDataReader dr = cmd.ExecuteReader();
        List<clssessionprp> obj = new List<clssessionprp>();
        while (dr.Read())
        {
            clssessionprp k = new clssessionprp();
            k.p_sescod = Convert.ToInt32(dr[0]);
            k.p_session = dr[1].ToString();
            k.p_status = dr[2].ToString();
        }

here status column is of char datatype. I google this but can't found any desired result. any help will be appriciated

my BLL code is

public void save_rec(clsclsprp p)
    {
        MySqlCommand cmd = new MySqlCommand("ins_cls", con);
        cmd.CommandType = CommandType.StoredProcedure;
        if (con.State == ConnectionState.Closed)
            con.Open();
        cmd.Parameters.Add("_clsnam", MySqlDbType.VarChar, 50).Value = p.p_clsnam;
        cmd.Parameters.Add("_clsdes", MySqlDbType.VarChar, 200).Value = p.p_clsdes;
        cmd.Parameters.Add("_clssec", MySqlDbType.Char,1).Value = p.p_clssec;
        cmd.ExecuteNonQuery();
        con.Close();
        cmd.Dispose();
    }

Answer

Adil picture Adil · Jan 16, 2014

You can use Convert.ToChar(Object) to directly converting object to character instead of converting it to string using ToString().

k.p_status = Convert.ToChar(dr[2]);