Npgsql parameters with NULL value, vb.net

I start the loop by adding parameters, when I get to NULL, the program drinks. My statement is as follows:

mysql = "Insert into TABLE (field1) VALUES (:Col1)" mycomm = New NpgsqlCommand (mySQL, conn) myComm.Parameters.Add("Col" & cCnt, NpgsqlTypes.NpgsqlDbType.Double).Value = Obj.value myComm.ExecuteNonQuery() 

This works fine if Obj.value is not empty, but if Obj.value is nothing, my Execute statement does not work. I would like to just insert a NULL value in the database. Any ideas? Thanks for any help!

+4
source share
1 answer

To insert a NULL value in the database field, you need to pass DBNull.Value to your parameter

You can use the VB.NET 3D operator to check if Obj is nothing in itself, or if Obj.Value is nothing, and in this case (true) pass DBNull.Value instead of Obj.Value

 myComm.Parameters.Add("Col" & cCnt, NpgsqlTypes.NpgsqlDbType.Double).Value = If(Obj Is Nothing OrElse Obj.Value Is Nothing, DBNull.Value, Obj.value)) 
+8
source

Source: https://habr.com/ru/post/1478878/


All Articles