Solved

Object cannot be cast from DBNull to other types.

Posted on 2009-05-12
4
330 Views
Last Modified: 2012-06-21
// Insert into tbl_index_pocs
                ItemCreatedResult res1 = new ItemCreatedResult();

                string sql1 = "sprInsertIntoIndexTicketPOCs";
                DbCommand cmd1 = db.GetStoredProcCommand(sql1);
                db.AddInParameter(cmd1, "@TicketID", DbType.Int32, res.ItemID);
                db.AddInParameter(cmd1, "@POCID",    DbType.Int32, Convert.ToInt32(strPOCString));
               
                db.AddInParameter(cmd1, "@DateAdded", DbType.DateTime, ticket.DateCreated);
                db.AddInParameter(cmd1, "@AgentAdded", DbType.String, ticket.AgentCreated);
                db.AddInParameter(cmd1, "@DateCreated", DbType.DateTime, ticket.DateCreated);
           
                db.AddOutParameter(cmd1, "@ticketPOCIndexID", DbType.Int32, 4);
                db.AddOutParameter(cmd1, "@resultCode", DbType.Int32, 4);
                db.AddOutParameter(cmd1, "@resultMessage", DbType.String, 200);
                db.ExecuteNonQuery(cmd1);


                res1.Code = Convert.ToInt32(db.GetParameterValue(cmd1, "resultCode"));===>??
                res1.Message = db.GetParameterValue(cmd1, "resultMessage").ToString();
                res1.ItemID = Convert.ToInt32(db.GetParameterValue(cmd1, "TicketID"));



???problem here.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
ALTER PROCEDURE [dbo].[sprInsertIntoIndexTicketPOCs]
( 
     @TicketID INT = null,
     @POCID INT = null, 
     @DateAdded DATETIME = null,
     @AgentAdded NVARCHAR(50) = null,
     @DateCreated DATETIME,
     @ticketPOCIndexID INT = null OUTPUT,
     @resultCode INT OUTPUT,
     @resultMessage NVARCHAR(200) OUTPUT
)
 AS  
 BEGIN  
         SET NOCOUNT ON
         INSERT INTO [tbl_index_ticket_pocs]
         (
                 [TicketID],
                 [POCID],
                 [DateAdded],
                 [AgentAdded],
                 [DateCreated]
         )
         VALUES
         (
                 @TicketID,
                 @POCID,
                 @DateAdded,
                 @AgentAdded,
                 @DateCreated
         )
         SET @ticketPOCIndexID=SCOPE_IDENTITY()
 END
I change it to now
-----------------------------------------------------------------------------------
-- ===========================================================================
-- Author:		Mathieu Cupryk
-- Create date: 05/06/2009
-- Description:	Insert Into IndexTicketsPOCs table
-- ===========================================================================
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
ALTER PROCEDURE [dbo].[sprInsertIntoIndexTicketPOCs]
( 
     @TicketID INT = null,
     @POCID INT = null, 
     @DateAdded DATETIME = null,
     @AgentAdded NVARCHAR(50) = null,
     @DateCreated DATETIME,
     @ticketPOCIndexID INT = null OUTPUT,
     @resultCode INT OUTPUT,
     @resultMessage NVARCHAR(200) OUTPUT
)
 AS  
 BEGIN  
         SET NOCOUNT ON
         INSERT INTO [tbl_index_ticket_pocs]
         (
                 [TicketID],
                 [POCID],
                 [DateAdded],
                 [AgentAdded],
                 [DateCreated]
         )
         VALUES
         (
                 @TicketID,
                 @POCID,
                 @DateAdded,
                 @AgentAdded,
                 @DateCreated
         )
         SET @ticketPOCIndexID=SCOPE_IDENTITY()
         
	 SET @resultCode = 0
         SET @resultMessage = 'ticketPOCIndexID ' + CAST(@ticketPOCIndexID  AS NVARCHAR(200)) + ': ' + @ticketPOCIndexID  + ' was created.'
 END     
GO

Open in new window

0
Comment
Question by:mathieu_cupryk
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24369608
What exactly is the logic behind @resultCode and    @resultMessage  in the stored procedure ?
0
 

Author Comment

by:mathieu_cupryk
ID: 24369628
Conversion failed when converting the nvarchar value 'ticketPOCIndexID 5085: ' to data type int.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24369658


SET @resultMessage = 'ticketPOCIndexID ' + CAST(@ticketPOCIndexID  AS NVARCHAR(200)) + ': ' +  CAST(  @ticketPOCIndexID as nvarchar(100) )   + ' was created.'
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24369882

SET @resultMessage = 'ticketPOCIndexID ' + CONVERT(NVARCHAR(200), @ticketPOCIndexID) + ': ' + CONVERT(NVARCHAR(200), @ticketPOCIndexID)  + ' was created.'

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question