473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I assign a null value to a SqlDataReader?

Hello,

This my a method to call a stored proce and uses a DataReader to read
the data

in the below method I am trying to assign a null value to my datareader
variable
Dim datareader As SqlDataReader
datareader = null
but it doesn't accept.
**********************************
Public Function GetDealers() As SqlDataReader
Dim strSql As String
Dim datareader As SqlDataReader
Try
Dim conn As IDbConnection = GetConnection()
Try
Dim cmd As IDbCommand = conn.CreateCommand()
strSql = "USP_GetDealers"
cmd.CommandText = strSql
cmd.CommandType = CommandType.StoredProcedure
conn.Open()
datareader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Finally
End Try
Catch ex As Exception
End Try
Return datareader
End Function
*************************************************
Can some one help me with this..
-L

Apr 25 '06 #1
4 2512
That would work in C#, but since you're using VB.NET you should set it to
Nothing instead of Null.
datareader = Nothing

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"Learner" <pr****@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Hello,

This my a method to call a stored proce and uses a DataReader to read
the data

in the below method I am trying to assign a null value to my datareader
variable
Dim datareader As SqlDataReader
datareader = null
but it doesn't accept.
**********************************
Public Function GetDealers() As SqlDataReader
Dim strSql As String
Dim datareader As SqlDataReader
Try
Dim conn As IDbConnection = GetConnection()
Try
Dim cmd As IDbCommand = conn.CreateCommand()
strSql = "USP_GetDealers"
cmd.CommandText = strSql
cmd.CommandType = CommandType.StoredProcedure
conn.Open()
datareader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Finally
End Try
Catch ex As Exception
End Try
Return datareader
End Function
*************************************************
Can some one help me with this..
-L

Apr 25 '06 #2
I don't think you can, DataReader objects have to be instantiated by using an
ExecuteReader method.

"Learner" wrote:
Hello,

This my a method to call a stored proce and uses a DataReader to read
the data

in the below method I am trying to assign a null value to my datareader
variable
Dim datareader As SqlDataReader
datareader = null
but it doesn't accept.
**********************************
Public Function GetDealers() As SqlDataReader
Dim strSql As String
Dim datareader As SqlDataReader
Try
Dim conn As IDbConnection = GetConnection()
Try
Dim cmd As IDbCommand = conn.CreateCommand()
strSql = "USP_GetDealers"
cmd.CommandText = strSql
cmd.CommandType = CommandType.StoredProcedure
conn.Open()
datareader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Finally
End Try
Catch ex As Exception
End Try
Return datareader
End Function
*************************************************
Can some one help me with this..
-L

Apr 25 '06 #3
Hello ,
Thans for the quick responses.

If I don't assign a default value a green line appears underneath the
'End Function' and when I mouse hover on it it says

'Function 'GetDealers' doesn't return a value on all code paths. Anull
reference could occur at run time when the result is used'

So like Steve mentioned I assigned it with Nothing and modified it
slightly

Public Function GetDealers() As SqlDataReader
Dim strSql As String
Dim datareader As SqlDataReader
datareader = Nothing
Try
Dim conn As IDbConnection = GetConnection()
Try
Dim cmd As IDbCommand = conn.CreateCommand()
strSql = "USP_GetDealers"
cmd.CommandText = strSql
cmd.CommandType = CommandType.StoredProcedure
conn.Open()
datareader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Finally
End Try
Catch ex As Exception
End Try
Return datareader
End Function
executed the code and it works fine and also there is no green line
underneath the "End Function" as I have moved "Return datareader" out
of the Try Catch block.

Hope the way I am doing it looks perfectly alright. Please advise if
itsn't and how do I it. Because I want to do it right :) Offcourse all
of us.

Thanks
-L

Apr 25 '06 #4
Yes, that is a perfectly valid design.
I do hope you plan to put something in the catch block though, otherwise you
should probably remove "ex as Exception" to help optimize the performance.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"Learner" <pr****@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Hello ,
Thans for the quick responses.

If I don't assign a default value a green line appears underneath the
'End Function' and when I mouse hover on it it says

'Function 'GetDealers' doesn't return a value on all code paths. Anull
reference could occur at run time when the result is used'

So like Steve mentioned I assigned it with Nothing and modified it
slightly

Public Function GetDealers() As SqlDataReader
Dim strSql As String
Dim datareader As SqlDataReader
datareader = Nothing
Try
Dim conn As IDbConnection = GetConnection()
Try
Dim cmd As IDbCommand = conn.CreateCommand()
strSql = "USP_GetDealers"
cmd.CommandText = strSql
cmd.CommandType = CommandType.StoredProcedure
conn.Open()
datareader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Finally
End Try
Catch ex As Exception
End Try
Return datareader
End Function
executed the code and it works fine and also there is no green line
underneath the "End Function" as I have moved "Return datareader" out
of the Try Catch block.

Hope the way I am doing it looks perfectly alright. Please advise if
itsn't and how do I it. Because I want to do it right :) Offcourse all
of us.

Thanks
-L

Apr 25 '06 #5

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

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
5
by: TomislaW | last post by:
I have integers in my database that can be null. I read them with SqlDataReader and show them to Repeater. Since integer can not be null in c#, I set null integer to -1. My project is 3-tier and it...
2
by: Chris | last post by:
Hi, I have an SQL Query that loads a SQLDataReader object. The returned record has bit datatype columns that I use to provide true/false values. I can't get a definite answer googling on...
2
by: Michael Bohman | last post by:
Hi, i have a small problem with assigning a database value to a RadioButtonList control. On my form i have 3 user admin=1, premium=2 and basic=3, theese values is stored in an access database in a...
2
by: Jim McGivney | last post by:
In asp 2.0 I am trying to insert a row using a detailsview control connected to an accessDataSource. I get the error message below. I am having trouble identifing which data field is causing the...
11
by: MLH | last post by:
I have 2 lines in a procedure that assign MyVariant a value - line #238 and line #491. When line #238 runs, the value is 152. When line #491 runs, the DLookup function returns Null. I would expect...
3
by: Liam Mac | last post by:
Hi All, Can anyone direct me or provide advice on how I can assign a null value to a date variable in vb.net. Basically what I'm doing is that I'm looping through a recordset where I have three...
0
by: joelranck | last post by:
I'm having a problem figuring out how to evaluate a value read from a SqlConnection and then assign a column based on that evaluation into my DataGrid. Basically when the DB is queried I want to...
10
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following three files. 1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to populate a simple <asp:ListBoxcontrol. 2. The UserDetails.cs file creates a Namespace...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.