Home All Groups Group Topic Archive Search About

Null in Visual Basic .Net???

Author
8 Jan 2006 9:16 PM
Familjen Karlsson
How will the code under look like in Visual Basic .Net, since you can't use
the keyword null anymoore in Visual Basic .Net. What can I use instead.

SqlConnection connection = null; SqlCommand command = null; SqlDataReader
reader = null;

String connectionString = "integrated security=SSPI;"; connectionString +=
"data source=(local);"; connectionString += "initial catalog=Northwind";

String sqlCommandString = "SELECT CustomerID, CompanyName FROM Customers";

try {

connection = new SqlConnection(connectionString);
command = new SqlCommand(sqlCommandString, connection); connection.Open();
reader = command.ExecuteReader();
if(reader != null)
{
    while(reader.Read())
    {
        Console.Write(reader["CustomerID"]+ "\t");
Console.WriteLine(reader["CompanyName"]); ...

    }

}

}catch( ){....}
finally
{
    if(reader != null)
        reader.Close();
    if(connection.State == ConnectionState.Open)
connection.Close();
}

mvh

Fia

Author
8 Jan 2006 9:24 PM
Ken Tucker [MVP]
Hi,

        If not reader is nothing then

Ken
-----------------------
Show quoteHide quote
"Familjen Karlsson" <fiao***@telia.com> wrote in message
news:yifwf.153681$dP1.511961@newsc.telia.net...
> How will the code under look like in Visual Basic .Net, since you can't
> use
> the keyword null anymoore in Visual Basic .Net. What can I use instead.
>
> SqlConnection connection = null; SqlCommand command = null; SqlDataReader
> reader = null;
>
> String connectionString = "integrated security=SSPI;"; connectionString +=
> "data source=(local);"; connectionString += "initial catalog=Northwind";
>
> String sqlCommandString = "SELECT CustomerID, CompanyName FROM Customers";
>
> try {
>
> connection = new SqlConnection(connectionString);
> command = new SqlCommand(sqlCommandString, connection); connection.Open();
> reader = command.ExecuteReader();
> if(reader != null)
> {
>    while(reader.Read())
>    {
>        Console.Write(reader["CustomerID"]+ "\t");
> Console.WriteLine(reader["CompanyName"]); ...
>
>    }
>
> }
>
> }catch( ){....}
> finally
> {
>    if(reader != null)
>        reader.Close();
>    if(connection.State == ConnectionState.Open)
> connection.Close();
> }
>
> mvh
>
> Fia
>
>
Author
8 Jan 2006 10:57 PM
Herfried K. Wagner [MVP]
"Familjen Karlsson" <fiao***@telia.com> schrieb:
> How will the code under look like in Visual Basic .Net, since you can't
> use
> the keyword null anymoore in Visual Basic .Net. What can I use instead.

> SqlConnection connection = null; SqlCommand command = null; SqlDataReader
> reader = null;
>[...]
> reader = command.ExecuteReader();
> if(reader != null)

Visual Basic .NET initializes local variables to 'Nothing' ("null
reference") automatically.  Thus you do not need to assign 'Nothing' to
these variables explicitly.  The line above can be written in VB.NET as
shown below:

\\\
If Not Reader Is Nothing Then
    ...
End If
///

VB 2005 supports the 'IsNot' operator, which can be used to shorten the code
to 'If Reader IsNot Nothing Then...'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
9 Jan 2006 1:34 AM
Cyril Gupta
In VB.Net Null is replaced by DBNull in databases. I had some trouble with
it too a few days ago, so when I solved my problem I wrote an article on it.
Here is the link

http://cyrilgupta.com/blog/?p=40

Cheers
Cyril Gupta
Author
9 Jan 2006 8:44 AM
Cor Ligthert [MVP]
Familjen,

You have to distinct a database null value which is a "dbnull.value" and an
object which has nothing as address (not instanced) and therefore not
referancable, which is than "IS Nothing",

Because that address was classical a null value in hardware, is in older
types of programming direct that hardware word "null" used for that.

I hope this helps,

Cor