Bikram
Bikram

Reputation: 197

Converting issue Byte to Byte

i never work with Bytes before i have a getting error here in may code please have a look

    SqlDataReader reader = cmd.ExecuteReader();
    reader.Read();
    SqlDataReader dr = ExecuteReader(Globals.con, CommandType.Text,
    "select FileName,MediaData,Extension from media where Id=" + ID);
    string FileName="";
    Byte[] MediaData= null;
    string Extension = "";
    while (dr.Read())
    {
        FileName = dr["FileName"].ToString();
        MediaData = Convert.ToByte(dr["MediaData"].ToString()); error is here
        Extension = dr["Extension"].ToString();              
    }
    dr.Close();
    string filename = (String)FileName;
    byte[] fileToDownload = (byte[])MediaData;
    String fileExtension = (String)Extension;

    in gridview i use this code below it working i need manual date
     not like code below
    string filename = (String)reader.GetValue(1);
    byte[] fileToDownload = (byte[])reader.GetValue(2);
    String fileExtension = (String)reader.GetValue(3);

please help me out in it

Upvotes: 1

Views: 121

Answers (1)

Oded
Oded

Reputation: 499092

Convert.ToByte returns a single byte, not an array.

You are also using ToString which can completely convert the binary data into a representation that you can't use:

MediaData = Convert.ToByte(dr["MediaData"].ToString())

Should be:

MediaData = (byte[])dr.Items["MediaData"];

Upvotes: 1

Related Questions