Object cannot be cast from DBNull to other typeshttp://forums.asp.net/t/1613587.aspx/1?Object+cannot+be+cast+from+DBNull+to+other+typesFri, 22 Oct 2010 02:12:27 -040016135874127857http://forums.asp.net/p/1613587/4127857.aspx/1?Object+cannot+be+cast+from+DBNull+to+other+typesObject cannot be cast from DBNull to other types <p>Im receving a runtime error for this, and I've checked other posts for similar errors, but this error is different.&nbsp; Does anybody have a clue?</p> <p>Under Locals/ arparams/ for the first parameter &quot;0&quot; the more detail error reports: </p> <p>&quot;Data is Null, the method or property cannot cannot be called on null values&quot;</p> <p>Im rewriting this program in the 4.0 framework from 2.0, utilizing VisualStudio2010.</p> <p>This&nbsp;ran fine previously without any erros in .NET 2.0.&nbsp;</p> <p>Here is the C# which collects the parameters and calls the stored proceedure.&nbsp;</p> <p>Here the first parameter strProjectID will be &quot;&quot; because this is a new project:</p> <pre class="prettyprint">public static int SaveProject(string strProjectID, string strProjectName, string strProjectDesc, string strProjectNotes, string strProjectDate, string strProjectType) { // Update Project within hip_Project int newprojectid; SqlParameter[] arParms = new SqlParameter[6]; arParms[0] = new SqlParameter(&quot;@pProjId&quot;, SqlDbType.Int, 1); if (strProjectID == &quot;&quot;) { arParms[0].Value = DBNull.Value; } else { arParms[0].Value = strProjectID; } arParms[0].Direction = ParameterDirection.Input; arParms[1] = new SqlParameter(&quot;@pProjName&quot;, SqlDbType.VarChar, 255); arParms[1].Value = strProjectName; arParms[1].Direction = ParameterDirection.Input; arParms[2] = new SqlParameter(&quot;@pProjDesc&quot;, SqlDbType.VarChar, 1000); arParms[2].Value = strProjectDesc; arParms[2].Direction = ParameterDirection.Input; arParms[3] = new SqlParameter(&quot;@pProjNotes&quot;, SqlDbType.VarChar, 8000); arParms[3].Value = strProjectNotes; arParms[3].Direction = ParameterDirection.Input; arParms[4] = new SqlParameter(&quot;@pProjDate&quot;, SqlDbType.DateTime, 8); arParms[4].Value = strProjectDate; arParms[4].Direction = ParameterDirection.Input; arParms[5] = new SqlParameter(&quot;@pProjType&quot;, SqlDbType.Int, 1); if (strProjectType == &quot;True&quot;) { arParms[5].Value = 1; } else { arParms[5].Value = 0; } arParms[5].Direction = ParameterDirection.Input; try { //SqlHelper.ExecuteNonQuery(ConfigurationManager.ConnectionStrings[&quot;connectionString&quot;].ConnectionString, CommandType.StoredProcedure, &quot;hip_InsUpdProj&quot;, arParms); newprojectid = Convert.ToInt32(SqlHelper.ExecuteScalar(ConfigurationManager.ConnectionStrings[&quot;connectionString&quot;].ConnectionString, CommandType.StoredProcedure, &quot;hip_InsUpdProj&quot;, arParms)); return newprojectid; } catch (Exception exCallInsertOrgSP) { throw exCallInsertOrgSP; } }</pre> <P><BR>Here is the original stored proceedure using SQL Server 2000.&nbsp; Now Im using SQL Server 2008:</P><pre class="prettyprint">CREATE proc dbo.hip_InsUpdProj ( @pProjId int, @pProjName varchar(255), @pProjDesc varchar(1000), @pProjNotes varchar(8000), @pProjDate datetime, @pProjType int ) as declare @RC int, @ROWS int begin if @pProjId = null begin insert into hip_Project (ProjectName, ProjectDesc, ProjectNotes, dtCreated) values (@pProjName, @pProjDesc, @pProjNotes, @pProjDate) end else begin update hip_Project set ProjectName = @pProjName, ProjectDesc = @pProjDesc, ProjectNotes = @pProjNotes, dtCreated = @pProjDate, deleted = @pProjType where Project_Id = @pProjId end if @pProjId = null SELECT SCOPE_IDENTITY() as newProjectID else SELECT @pProjId as newProjectID return 0 GO </pre> <p><br> I did find a workaround just passing in 0 instead of using DBNulls, but I would still like to find out why this problem exists?</p> <p>Please let me know if you need further analysis, thank you in advance for your help!</p> <p>&nbsp;</p> 2010-10-16T20:41:49-04:004127878http://forums.asp.net/p/1613587/4127878.aspx/1?Re+Object+cannot+be+cast+from+DBNull+to+other+typesRe: Object cannot be cast from DBNull to other types <p>Are you sure that your stored procedure returned a value? For instance, if table has no identity defined then SCOPE_IDENTITY() will return NULL which may not be converted to int32. Call the code with DBNull as it is listed here and see in SQL Management Studio if Project_Id has got an ID.&nbsp;&nbsp;</p> 2010-10-16T21:37:12-04:004127940http://forums.asp.net/p/1613587/4127940.aspx/1?Re+Object+cannot+be+cast+from+DBNull+to+other+typesRe: Object cannot be cast from DBNull to other types <p>OK I executed this within Management Studio: </p> <p>pProjID - Allow Nulls (checked)<br> pProjName - Project6Name<br> pProjDesc - Project6Desc<br> pProjNotes- Project6Notes<br> pProjDate- 12/16/2010<br> pProjType- 0 <br> <br> The NewProjectID = Null (this should contain a new projectID)<br> The ReturnValue = 0 </p> <p>the Project_ID column isIdentity with its seed and increment&nbsp;set.</p> <p>&nbsp;</p> 2010-10-17T00:29:30-04:004128053http://forums.asp.net/p/1613587/4128053.aspx/1?Re+Object+cannot+be+cast+from+DBNull+to+other+typesRe: Object cannot be cast from DBNull to other types <p></p> <blockquote><span class="icon-blockquote"></span> <h4>NinjaSk8ter</h4> <p></p> <p>pProjID - Allow Nulls (checked)<br> </p> <p></p> </blockquote> <p></p> <p>Huh? That's the problem. It should not allow nulls, it should be an automatically incremented ID.</p> <p></p> 2010-10-17T08:00:13-04:004128553http://forums.asp.net/p/1613587/4128553.aspx/1?Re+Object+cannot+be+cast+from+DBNull+to+other+typesRe: Object cannot be cast from DBNull to other types <p>Yes but&nbsp;isnt&nbsp;Null is whats coming in from the application (DBNull)?&nbsp; </p> <p>This is and always was an auto incremented ID, I changed nothing.</p> <p>Like I mentioned this worked perfectly before with .NET 2.0.</p> <p>Is there something in the&nbsp;Stored Proceedure I should change?&nbsp;</p> 2010-10-18T02:51:15-04:004128755http://forums.asp.net/p/1613587/4128755.aspx/1?Re+Object+cannot+be+cast+from+DBNull+to+other+typesRe: Object cannot be cast from DBNull to other types <p></p> <blockquote><span class="icon-blockquote"></span> <h4>NinjaSk8ter</h4> <p></p> <p>...isnt&nbsp;Null is whats coming in from the application (DBNull)?&nbsp;</p> <p></p> </blockquote> <p></p> <p>No. You sent NULL to the stored procedure (as @pProjId). In the SP you have&nbsp;</p> <p></p> <pre class="prettyprint">if @pProjId = null begin insert into hip_Project (ProjectName, ProjectDesc, ProjectNotes, dtCreated) values (@pProjName, @pProjDesc, @pProjNotes, @pProjDate) end else ...</pre><p>which means that if @pProjId is NULL then SP will insert new entry into the hip_Project table.</p><p>As we can see, there is no value specified for the Project_Id column, which means that&nbsp;</p><ol><li>either&nbsp;this column should accept NULL&nbsp;</li><li>or it should be an identity column that is made up of values generated by the database.</li></ol><p>Now, see the code below</p><p></p><pre class="prettyprint">if @pProjId = null SELECT SCOPE_IDENTITY() as newProjectID else ...</pre> <p>This tells me that it is expected to get a value from the SCOPE_IDENTITY function which returns the last identity value inserted into an identity column in the same scope. As there is only one INSERT statement - it means that the the Project_Id column should be an identity column and not NULL.<br> </p> <p></p> <p></p> <p></p> 2010-10-18T06:41:31-04:004129700http://forums.asp.net/p/1613587/4129700.aspx/1?Re+Object+cannot+be+cast+from+DBNull+to+other+typesRe: Object cannot be cast from DBNull to other types <p>The Project_ID column should be, and is, an identity column which will create a new Project_ID if @pProjID = null.&nbsp; </p> <p>The code snippet that you had listed&nbsp;IS part of my SP.&nbsp; I want to re-print this here:&nbsp;</p> <p>If this SP worked perfectly in SQL Server 2000, why does this not return the ID value in SQL Server 2008?</p> <pre class="prettyprint">CREATE proc dbo.hip_InsUpdProj ( @pProjId int, @pProjName varchar(255), @pProjDesc varchar(1000), @pProjNotes varchar(8000), @pProjDate datetime, @pProjType int ) as declare @RC int, @ROWS int begin if @pProjId = null begin insert into hip_Project (ProjectName, ProjectDesc, ProjectNotes, dtCreated) values (@pProjName, @pProjDesc, @pProjNotes, @pProjDate) end else begin update hip_Project set ProjectName = @pProjName, ProjectDesc = @pProjDesc, ProjectNotes = @pProjNotes, dtCreated = @pProjDate, deleted = @pProjType where Project_Id = @pProjId end select @RC = @@error if @RC &lt;&gt; 0 return @RC end --print SCOPE_IDENTITY() if @pProjId = null SELECT SCOPE_IDENTITY() as newProjectID else SELECT @pProjId as newProjectID return 0 GO</pre> <p>&nbsp;</p> <p>Is there something new in&nbsp;SQLServer 2008 that I need to account for in this SP?<br> &nbsp;</p> 2010-10-18T17:51:28-04:004129913http://forums.asp.net/p/1613587/4129913.aspx/1?Re+Object+cannot+be+cast+from+DBNull+to+other+typesRe: Object cannot be cast from DBNull to other types <p>Ok, it's my fault: I asked you about table structure and you sent me</p> <p><i>pProjID - Allow Nulls (checked)</i></p> <p>I thought it is a name of your column, that's why I said it's not correct.</p> <p>Well, can you send me a script to create the table? So, I can test it myself.</p> <p>Thanks,</p> 2010-10-18T21:01:52-04:004129947http://forums.asp.net/p/1613587/4129947.aspx/1?Re+Object+cannot+be+cast+from+DBNull+to+other+typesRe: Object cannot be cast from DBNull to other types <p>Here is the structure for that table:</p> <p>&nbsp;</p> <table class="mceItemTable"> <tbody> <tr> <th>Column Name</th> <th>Data Type</th> <th>Allow Nulls</th> </tr> <tr> <td>Project_Id</td> <td>int</td> <td>No Allow Nulls (IsIdentity w/ identityseed=200 w/ increment=1)</td> </tr> <tr> <td>ProjectName</td> <td>nvarchar(50)</td> <td>No Allow Nulls</td> </tr> <tr> <td>ProjectDesc</td> <td>nvarchar(50)</td> <td>Allow Nulls</td> </tr> <tr> <td>deleted</td> <td>int</td> <td>No Allow Nulls</td> </tr> <tr> <td>ProjectNotes</td> <td>nvarchar(50)</td> <td>Allow Nulls</td> </tr> <tr> <td>dtCreated&gt;</td> <td>datetime</td> <td>Allow Nulls</td> </tr> </tbody> </table> <p>&nbsp;</p> 2010-10-18T21:27:25-04:004131741http://forums.asp.net/p/1613587/4131741.aspx/1?Here+is+the+script+to+create+the+tableHere is the script to create the table <pre class="prettyprint">SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[hip_Project]( [Project_Id] [int] IDENTITY(200,1) NOT NULL, [ProjectName] [nvarchar](50) NOT NULL, [ProjectDesc] [nvarchar](50) NULL, [deleted] [int] NOT NULL, [ProjectNotes] [nvarchar](50) NULL, [dtCreated] [datetime] NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[hip_Project] ADD CONSTRAINT [DF_hip_Project_dtCreated] DEFAULT (getdate()) FOR [dtCreated] GO</pre> <p><br> &nbsp;</p> 2010-10-19T19:27:27-04:004135853http://forums.asp.net/p/1613587/4135853.aspx/1?Re+Here+is+the+script+to+create+the+tableRe: Here is the script to create the table <p>I discovered that there was a bug in the StoredProceedure:</p> <li>if&nbsp;@pProjId&nbsp;=&nbsp;null&nbsp;&nbsp; <p>this is correct:</p> <p>if @pProjID is null</p> </li>2010-10-22T02:12:27-04:00