Solved

Question on output parameter in VB.NET program.

Posted on 2006-07-20
3
206 Views
Last Modified: 2008-02-26
I have a stored proc:
CREATE proc usp_Test1    
@wo_num varchar(6),    
@emp_cd varchar(6) out,    
@status_message varchar(20) out    
as    
set @status_message = 'Print something'    
select @status_message, @emp_cd  
return    

When I run this stored proc: exec usp_Test1    '100000', 'aaa', 'bbb'
It has no error and will print: 'Print something'   'aaa'

But when I used in my vb.net program:
emp_cd = objCmd.Parameters("@emp_cd").Value
it will have an exception that shows:
"Cast from type 'DBNull' to type 'String' is not valid."

It's obviously because that output parameter was not assisgned a value.
1. But my question is, why it was null instead of getting the value from the parameter list in exec usp_Test1    '100000', 'aaa', 'bbb', i.e. got a value of 'aaa'?
2. My solution to the above problem was added one more initilization line:
set @emp_cd = ''  
Is there any better way or standard way of doing this? Thanks a lot.
0
Comment
Question by:heyday2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Expert Comment

by:Justin_W
ID: 17150761
I assume you have declared emp_cd as a String. If there is any possiblility that it can return NULL, you need to declare it as type Object, and only cast it to string if (DBNull.Value.Equals(emp_cd)) is false.
0
 
LVL 24

Assisted Solution

by:Justin_W
Justin_W earned 100 total points
ID: 17150764
FYI: This is because DBNull and Nothing/null are not the same thing in .NET.
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 400 total points
ID: 17151541
For some reason, Microsoft has created an object called System.DBNull which represents an empty field. What's really baffling is that they made this object not cast to anything else (i.e. not string, integer, double)

The easiest way to deal with this is to rewrite your stored procedure to return something other than a NULL:
select @status_message, ISNULL(@emp_cd  ,'')
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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