473,545 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DBNULL Error - SQL Server2000/ASP.NET

Dear Group

I'm having a very weird problem. Any hints are greatly appreciated.

I'm returning two values from a MS SQL Server 2000 stored procedure to my
ASP.NET Webapplication and store them in sessions.
Like This:

prm4 = cmd1.CreatePara meter
With prm4
..ParameterName = "@Sec_ProgUser_ Gen"
..SqlDbType = SqlDbType.VarCh ar
..Size = 10
..Direction = ParameterDirect ion.Output
End With

prm5 = cmd1.CreatePara meter
With prm5
..ParameterName = "@Sec_ProgUser_ Key"
..SqlDbType = SqlDbType.VarCh ar
..Size = 10
..Direction = ParameterDirect ion.Output
End With
....
cmd1.ExecuteNon Query()
....
Session("Sec_Pr ogUser_Gen") = prm4.Value
Session("Sec_Pr ogUser_Key") = prm5.Value

Both output parameters are declared as varchar(10) within the stored
procedure. If I run the stored procedure in SQL Analyzer, I'm getting a
string value for each of them. E.g. @Sec_ProgUser_G en is "1110011",
@Sec_ProgUser_K ey = "1100".

Now the strange thing happens if I try to run the following code:

Sub MyTest()
Dim MyString1 As String
Dim MyString2 As String
MyString1 = CStr(Session("S ec_ProgUser_Key "))
....
MyString2 = CStr(Session("S ec_ProgUser_Gen "))
End Sub

It fails in line 'MyString2 = CStr(Session("S ec_ProgUser_Gen "))' with Cast
from type 'DBNull' to type 'String' is not valid.

I don't understand this. They are both the same, the only difference is the
length of the string. Help!

Additional Information:
The values for @Sec_ProgUser_X XX are created in the stored procedure with a
statement like this:
SET @Sec_ProgUser_K ey = (SELECT Convert(varchar (1),Key_CanCrea teKey) +
Convert(varchar (1),Key_CanCrea teTransaction) +
Convert(varchar (1),Key_CanView ) + Convert(varchar (1),Key_CanDele te) FROM
i2b_proguser_se curityprofile WHERE SecurityProfile ID = @SecurityProfil eID)

The datatype of the source columns used to be bit then changed them to
Integer as I thought this might cause the problem. (Although it shouldn't as
the values get converted to varchar without a problem in the stored
procedure. No fields contain NULL values, only 1 or 0.

Jul 20 '05 #1
1 5074
Hi Everyone

Found the problem. Strange that I haven't seen it earlier.
Dim str1 As String = "EXEC sp_ValidatePerm issions @ProgClientID,
@ProgUserID, @Sec_ProgClient _Mod OUTPUT, @Sec_ProgUser_G en OUTPUT,
@Sec_ProgUser_K ey OUTPUT"

Forgot OUTPUT for @Sec_ProgUser_G en

"Martin Feuersteiner" <th************ @hotmail.com> wrote in message
news:bu******** **@titan.btinte rnet.com...
Dear Group

I'm having a very weird problem. Any hints are greatly appreciated.

I'm returning two values from a MS SQL Server 2000 stored procedure to my
ASP.NET Webapplication and store them in sessions.
Like This:

prm4 = cmd1.CreatePara meter
With prm4
.ParameterName = "@Sec_ProgUser_ Gen"
.SqlDbType = SqlDbType.VarCh ar
.Size = 10
.Direction = ParameterDirect ion.Output
End With

prm5 = cmd1.CreatePara meter
With prm5
.ParameterName = "@Sec_ProgUser_ Key"
.SqlDbType = SqlDbType.VarCh ar
.Size = 10
.Direction = ParameterDirect ion.Output
End With
...
cmd1.ExecuteNon Query()
...
Session("Sec_Pr ogUser_Gen") = prm4.Value
Session("Sec_Pr ogUser_Key") = prm5.Value

Both output parameters are declared as varchar(10) within the stored
procedure. If I run the stored procedure in SQL Analyzer, I'm getting a
string value for each of them. E.g. @Sec_ProgUser_G en is "1110011",
@Sec_ProgUser_K ey = "1100".

Now the strange thing happens if I try to run the following code:

Sub MyTest()
Dim MyString1 As String
Dim MyString2 As String
MyString1 = CStr(Session("S ec_ProgUser_Key "))
...
MyString2 = CStr(Session("S ec_ProgUser_Gen "))
End Sub

It fails in line 'MyString2 = CStr(Session("S ec_ProgUser_Gen "))' with Cast
from type 'DBNull' to type 'String' is not valid.

I don't understand this. They are both the same, the only difference is the length of the string. Help!

Additional Information:
The values for @Sec_ProgUser_X XX are created in the stored procedure with a statement like this:
SET @Sec_ProgUser_K ey = (SELECT Convert(varchar (1),Key_CanCrea teKey) +
Convert(varchar (1),Key_CanCrea teTransaction) +
Convert(varchar (1),Key_CanView ) + Convert(varchar (1),Key_CanDele te) FROM
i2b_proguser_se curityprofile WHERE SecurityProfile ID = @SecurityProfil eID)

The datatype of the source columns used to be bit then changed them to
Integer as I thought this might cause the problem. (Although it shouldn't as the values get converted to varchar without a problem in the stored
procedure. No fields contain NULL values, only 1 or 0.

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
3578
by: jim | last post by:
This line of code returns error 13, cast from 'DBNull' to type 'String' is not valid If IsDBNull(Clinics.Clinics.Item(A).Workphone) The <other code End I Clinics.Clinics is a dataset that was loaded using a left join from two MS SQL database tables. The workphone element is null because there was no corresponding record from the right...
5
7447
by: DraguVaso | last post by:
Hi, Something I don't understand about a Typed DataSet: When a value in the DataSet is DBNull, it throws this error: "Cannot get value because it is DBNull". But aren't Typed DataSets invented to make life easier, to be able to get to tge Tables and Values with less code, in less time? But with this thing you need to add a Try-Catch...
2
1487
by: Filipe Cristóvão | last post by:
Hi, I know that this is a newbie's question, but I need to know how i do to know if a field in a table is DBNull. If I try to write (with response.write) the field, it gives me an error: "Cast from type 'DBNull' to type 'String' is not valid". Thanks for any help. -- Filipe Cristóvão
11
4896
by: Patrick.O.Ige | last post by:
When i try and use this (Where Unit is a column in my Table):- If Unit Is DBNull.Value Then Return "1" Else Return "2" End If I always have 2 returned! Even when Unit is NULL! I want a case if a column is NULL the it xhould return '1 and if not '2' How does DBNull.Value work?
4
1721
by: Tina | last post by:
I have instantiated an insertRow for a dataset. I now want to make the GLCode DBNull. I have tried: insertRow.GLCode = Convert.dbnull insertRow.GLCode = Convert.dbnull(insertRow.GLCode) and several other things. How is it done?
8
17503
by: MattB | last post by:
Hello. I have a vb.net (asp.net) application that uses ado.net datasets. At one point, I need to check a text field in a DataTable to see if there's any text in it before performing text operations on that record. I thought I could just test using something like this: If Not IsNothing(e.Item.DataItem("guest_name")) Then .... End If But...
6
4607
by: tshad | last post by:
I have a value coming from my Object (or it could also be from a SqlDbReader) where I need to test for DBNull and 0. I tried to do it in one call: if (not (newPosition.ReportsTo is DBNull.Value)) andalso (newPosition.ReportsTo <> 0) then The first time I did it I used "and" and got the following error. Operator is not valid for type...
19
19298
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
6
13090
by: scott ocamb | last post by:
Hello, I have a function that expects the following parms. How can I handle null values. For example, if FirstName comes through as null, how can i propogate that null value to the stored procedure. public long InsertPerson(string FirstName, string LastName, string MiddleName, string EMail, long PersonTypeId, string HomePhone, string...
0
7487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.