Jeff-Lewis / codesmith

Automatically exported from code.google.com/p/codesmith
0 stars 0 forks source link

Cannot implicitly convert type 'System.DBNull' to 'System.Data.SqlTypes.SqlInt32 #140

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Create a table with a nullable int column
2.Create CRUD stored procedures from template
3.Create a CommandWrappers from template
4.In vs.net I set stp parameters, call execute() on object of a wrapper for
the generated usp_Insert....... 

What is the expected output? What do you see instead?
When I set a nullable value as parameter it shows 

Error: Cannot implicitly convert type 'System.DBNull' to
'System.Data.SqlTypes.SqlInt32'

Please use labels and text to provide additional information.

Insertsurowce insSurowce = new Insertsurowce(connectionString);
insSurowce.gru_id = iidGrupy;
insSurowce.sur_nazwa = snazwaSurowca;
insSurowce.sur_cena = dcenaZakupu;
insSurowce.dos_id = //--- set this int column parameter as null --- 
insSurowce.Execute();

Original issue reported on code.google.com by blak...@gmail.com on 15 Sep 2009 at 8:10

GoogleCodeExporter commented 9 years ago

Original comment by shannon....@gmail.com on 18 Sep 2009 at 4:42

GoogleCodeExporter commented 9 years ago

Original comment by shannon....@gmail.com on 6 Oct 2009 at 3:36

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago

Original comment by shannon....@gmail.com on 7 Oct 2009 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by bniemyjski on 8 Oct 2009 at 7:20

GoogleCodeExporter commented 9 years ago
Hello,

You need to add in a null check. If you call the Empty constructor of 
SqlInt32() it
will pass in null.

int? nullInt = null;
var updatenullableIntTable = new InsertUpdatenullableIntTable("Data 
Source=.;Initial
Catalog=wrappers;Integrated Security=True");

updatenullableIntTable.id = "test";
updatenullableIntTable.nullableIntColumn = nullInt.HasValue ? new
SqlInt32(nullInt.Value) : new SqlInt32();
updatenullableIntTable.Execute();

By taking a look at SQL Profiler we can see this is the expected result:

exec [dbo].[usp_InsertUpdatenullableIntTable] 
@nullableIntColumn=NULL,@id=N'test      '
go

Thanks
-Blake Niemyjski

Original comment by bniemyjski on 12 Oct 2009 at 12:01