C # insert data null

1

You could support me I'm new, I'm migrating data from Oracle to SQL server but the problem is that I'm trying to insert data and everything was fine, until I came up with the following: When inserting the data there are null fields and I do not know how to correct it.

  

** Error managerid there I have the null fields. managerid = reader3.GetInt32 (2);

It tells me there is no data and it causes error. **

//INSERT DEPARTMENTS
            cmd2.CommandText = "SELECT * FROM DEPARTMENTS";
            cmd2.Connection = conectar;
            cmd2.CommandType = CommandType.Text;

            SqlCommand insert3 = conectarsql.CreateCommand();
            insert.CommandType = CommandType.Text;

            int departmentid;
            string departmentname;
            int managerid;
            int locationid;
            OracleDataReader reader3 = cmd2.ExecuteReader();
            reader = cmd2.ExecuteReader();

            while (reader3.Read())
            {
                departmentid = reader3.GetInt32(0);
                departmentname = reader3.GetString(1);
                managerid = reader3.GetInt32(2);
                locationid = reader3.GetInt32(3);

                insert.CommandText = "INSERT INTO DEPARTMENTS VALUES('" + departmentid + "','" + departmentname + "','" + managerid + "'," + locationid + ")";
                insert.ExecuteNonQuery();
                //FIN INSERT DEPARTMENTS
    
asked by Bau Doo 15.04.2018 в 09:46
source

1 answer

0

You are trying to assign an integer a NULL value, and that is not possible. Try it like this:

managerid = reader3.GetInt32(2) ?? 0;

With this, what you do is give it a default value, in this case 0, whenever 'reader3.GetInt32 (2)' has a NULL value.

    
answered by 15.04.2018 / 10:23
source