Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Object reference not set to an instance of an object.

Status
Not open for further replies.

chicdog

Programmer
Feb 28, 2002
84
US
I'm having a problem with my code. My users occasionaly get Object reference not set to an instance of an object.
It occurs when the I try to get the results back for a stored procedure. I'm at a loss as to what to change.
FYI MyReader & MyCmd are declared in a global modual
Here's the stack trace:
Code:
2: Error Description:Object reference not set to an instance of an object.
2: Source:System.Data
2: Stack Trace:   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
2:    at System.Data.SqlClient.SqlCommand.ExecuteReader()
2:    at WEBMIS.AdhocQuery.CheckDownload(Decimal GroupID) in F:\app-development\WebMIS\AdhocQuery.aspx.vb:line 229
2:    at WEBMIS.AdhocQuery.Page_Load(Object sender, EventArgs e) in F:\app-development\WebMIS\AdhocQuery.aspx.vb:line 106
2:    at System.Web.UI.Control.OnLoad(EventArgs e)
2:    at System.Web.UI.Control.LoadRecursive()
2:    at System.Web.UI.Page.ProcessRequestMain()
2: Target Site:System.Data.SqlClient.SqlDataReader ExecuteReader(System.Data.CommandBehavior, System.Data.SqlClient.RunBehavior, Boolean)
Code:
227   If cnMIS.State = ConnectionState.Closed Then cnMIS.Open()
228   MyCmd = New SqlCommand("usp_HasDownloaded", cnMIS)
229   With MyCmd
230     .CommandTimeout = 8000
231     .CommandType = CommandType.StoredProcedure
232     .Parameters.Add(New SqlParameter("@GroupID", SqlDbType.Decimal, 9)).Value = GroupID
233     .Parameters.Add(New SqlParameter("@MyDate", SqlDbType.DateTime, 9)).Value = FormatDateTime(Date.Now, DateFormat.ShortDate)
234      MyReader = .ExecuteReader()
235   End With


 
Don't know if this matters but I would move the IF statement closer to the execute reader (open late, close early when it comes to connections). Also maybe take the MyReader = .ExecuteReader() out of the With block. Also are you disposing of the connection anywhere else in the code?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top