Solved

Object cannot be cast from DBNull to other types.

Posted on 2009-05-12
4
327 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now