how to get varchar as byte[] data using DataReader in Ado.Net? We have tried the following code.
if (!objDataReader.IsDBNull(i))
{
long len = objDataReader.GetBytes(i, 0, null, 0, 0);
byte[] buffer = new byte[len];
objDataReader.GetBytes(i, 0, buffer, 0, (int)len);
}
But the above code gives the error ("Unable to cast object of type 'System.String' to type 'System.Byte[]'.")
Update:
When value for a column is null and i remove dbnull condition then executing following lines of code gives an error as "Unable to cast object of type 'System.DBNull' to type 'System.Byte[]"
long len = objDataReader.GetBytes(i, 0, null, 0, 0);
byte[] buffer = new byte[len];
objDataReader.GetBytes(i, 0, buffer, 0, (int)len);