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);

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: