try catch finally
Posted
by
gligom
on Stack Overflow
See other posts from Stack Overflow
or by gligom
Published on 2012-12-16T17:02:05Z
Indexed on
2012/12/16
17:02 UTC
Read the original article
Hit count: 303
Maby this is simple for you, but for me is not. I have this code:
Private int InsertData()
{
int rezultat = 0;
try
{
if (sqlconn.State != ConnectionState.Open)
{
sqlconn.Open();
}
rezultat = (int)cmd.ExecuteScalar();
}
catch (Exception ex)
{
lblMesaje.Text = "Eroare: " + ex.Message.ToString();
}
finally
{
if (sqlconn.State != ConnectionState.Closed)
{
sqlconn.Close();
}
}
return rezultat;
}
Is just for inserting a new record in a table. Even if this throw an error "Specified cast is not valid." "rezultat=(int)cmd.ExecuteScalar();" - the code is executed and the row is inserted in the database, and the execution continues.
Why it continues? Maby i don't understand the try catch finally yet Smile | :)
Thank you!
© Stack Overflow or respective owner