Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How can I return SqlDataReader in Module
Message
 
 
To
20/05/2011 05:18:30
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows XP
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01511205
Message ID:
01511219
Views:
43
>Hi all,
> i want to wirte a fucntion to return SqlDataReader in Module, but appear error, NullReferenceException was unhandled by user code.
>And Troubleshooting tips: Use the "new" keyword to create an object instance.
>I use vb.net , my table more data not nul,GetConnection is no problem , Anyone can give some idea to me , thank you .
>
>
>
>Imports System.Data.SqlClient
>Module CommonModule
>            Public dr As SqlDataReader
>           Public Function Selectdr(ByVal tablename As String, ByVal field As String, ByVal value As String, Optional ByVal tablename2 As String = "", Optional ByVal joinfield As String = "", Optional ByVal wherefield As String = "") As SqlDataReader
>        Dim conn As SqlConnection = GetConnection()
>    
>        Try
>
>       
>                Dim sqlcmd As New SqlCommand("select * from syslib", conn)
>            End If
>
>            conn.Open()
>
>            Return sqlcmd.ExecuteReader             < 'in this the compile display error
>
>        Finally
>            conn.Close()
>            conn.Dispose()
>        End Try
>    End Function
>End Module
>
>
>
>
>
Looks to me like sqlcmd was not successfully instantiated. Some things to "try" --

1. Before the Try block, trace through the "Dim conn" statement to make sure the database connection is valid.
2. Put a Catch in the Try block.
3. Check sqlcmd for Null before using it.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform