Monday, November 09, 2009

Cannot implicitly convert type 'System.DBNull' to 'int'

There will be situations where you have to set null values to an Integer field. So when you tried to assign a DBNull.Value to an integer varialbe you will prompt for the error : "Cannot implicitly convert type 'System.DBNull' to 'int' ".

to resolve this matter, let it be as its in the interface level, but at the time of passing the parameter to the Database just make it DBNull.Value.

'With your command object

1: myCmd.Parameters.Add("@myParameter",SQLDBType.INT).Value = IIf(myTextBox.Text <> "", myTextBox.Text, DBNull.Value)

another code snippet :
  1. if (Employee.EmployeeManagerID == 0) { AddParameter("@EmployeeManagerID", DBNull.Value, DbType.Int32, null, ParameterDirection.Input); } else AddParameter("@Manager",Employee.EmployeeManagerID, DbType.Int32, null, ParameterDirection.Input);

0 comments: