Solved

Object cannot be cast from DBNull to other types.

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

23 Experts available now in Live!

Get 1:1 Help Now