Socarsky
Well-known member
- Joined
- Mar 3, 2014
- Messages
- 59
- Programming Experience
- Beginner
I face with a matter that says "Specified cast is not valid." and then started to dig it to figured out. So, I found the reason but one thing made me curious to learn why does MSSQL Sum(nQty) with int32 datatype by itself? is this normal?
I fount the matter with this code!
and thrown occurs here:
getBalanceQty is a type of int16
I fount the matter with this code!
C#:
for (int i = 0; i < reader.FieldCount; i++)
{
MessageBox.Show(reader.GetName(i) + ":" + reader.GetFieldType(i).FullName);
}
and thrown occurs here:
C#:
getBalanceQty = reader.GetInt16(reader.GetOrdinal("QTY"));
getBalanceQty is a type of int16
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
//reader.Read();
while (reader.Read())
{
if (!reader.IsDBNull(reader.GetOrdinal("QTY")))
{
getBalanceQty = reader.GetInt16(reader.GetOrdinal("QTY"));
}
else
{
getBalanceQty = 0;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return getBalanceQty;
}
Attachments
Last edited: