i am trying to get the picture string from the database and when it is a null it gives me this error.
Unable to cast object of type 'System.DBNull' to type 'System.String
This is my code
public List<Item> GetAllItemFromMenuID(List<int> ListOfItemIDs)
{
conn.Open();
List<Item> listOfItems = new List<Item>();
try
{
foreach (int id in ListOfItemIDs)
{
SqlCommand comm = new SqlCommand("Select * FROM tblItem WHERE ID = '" + id + "'", conn);
SqlDataReader r = comm.ExecuteReader();
while (r.Read())
{
string name = (string)r[1];
int itemType = (int)r[2];
decimal price = (decimal)r[3];
string picture = (string)r[4];
if (picture == null)
{
picture = "";
}
Item i = new Item(name, itemType, price, picture);
//Item i = new Item((string)r[1], (int)r[2], (decimal)r[3], picture);//(string) r[4]);
listOfItems.Add(i);
}
r.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return listOfItems;
}
finally
{
conn.Close();
}
return listOfItems;
}
any solutions pls?