Wie behandeln null-Wert in byte[]

Ich habe von binären Bildern in der Image Spalte meiner Datenbank-Tabelle, aber es gibt einige null Werte in der Image Spalte. So wird eine Ausnahme geworfen,

byte[] data = (byte[])ds.Tables[0].Zeilen[0][0]` da null.

Wie Sie damit umgehen?

Exception

Unable to cast object of type 'System.DBNull' type 'System.Byte[]'.

Mein code,

using (var sqlConn = new SqlConnection(connstr))
{
    sqlConn.Open();
    ds = new DataSet();

    SqlDataAdapter sqa = new SqlDataAdapter("Select Image from Templates where Shoe='" + selectedShoe + "'", sqlConn);

    sqa.Fill(ds);

    //i got error here
    byte[] data = (byte[])ds.Tables[0].Rows[0][0];

    .....

InformationsquelleAutor user1358072 | 2013-07-06

Schreibe einen Kommentar