I have the following code, but i got a problem. Within my While, a have to test if r[“Varandas”] is not null. In a specific imovel_id, the r[“Varandas”] is null, but still passing through my code and then I got an exception error because can’t parse to int the r[“Varandas”] because is null, but why he pass through my first IF inside my While loop ?
string s = "SELECT * "
+ "FROM San_Imovel_Caracteristica "
+ "WHERE Imovel_Id = " + imovel_id + " ";
SqlConnection c = new SqlConnection(conn.Con);
SqlCommand cmd = new SqlCommand(s, c);
c.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
if (r["Varandas"] != null)
{
if (Convert.ToInt32(r["Varandas"].ToString()) > 0)
{
XmlElement itemImovel1 = doc.CreateElement("itemImovel");
caracteristicasImovel.AppendChild(itemImovel1);
itemImovel1.InnerText = "varanda";
}
}
}
null value from database has its own special type:
To make it more elegant you can write some simple function or even extension for the DataRow class.