1

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

1
  • which error you are getting? Commented Jun 6, 2013 at 11:43

1 Answer 1

1

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

2 Comments

im using this same please check the code one again MediaData = Convert.ToByte(dr["MediaData"].ToString()); error is here
@Vikas - Exactly. I have explained why that line is not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.