Thursday, March 22, 2012

Object reference not set to an instance of an object.

I keep receiving "Object reference not set to an instance of an object" error when trying to run this code:

Private Sub cmdok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdok.Click
Dim rs1 As SqlClient.SqlDataAdapter
Dim ds As DataSet
Dim xnav As String
Dim xfreq As String
Dim xsn As String
Dim xfw As String
Dim xdte As Date
Dim xrecvd As String
Dim xrels As String
Dim xremarks As String
Dim xcmpy As String
Dim xrver As String
Dim xplate As String
Dim xdr As String
Dim xassign As Date
Dim msg As String

If TXTsn.Text <> "" Then

Dim conPubs As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
rs1 = New SqlClient.SqlDataAdapter("SELECT * FROM tbl_Inbox WHERE imd_sn = '" & TXTsn.Text & "'", conPubs)

ds = New DataSet
rs1.Fill(ds, "imd_nav")

If ds.Tables(0).Rows.Count <> 0 Then
xrels = ds.Tables(0).Rows(0)("imd_rels").ToString()
xrecvd = ds.Tables(0).Rows(0)("imd_recvd").ToString()

Functions.InsertImdAssign(TXTnav.Text, TXTfreq.Text, TXTsn.Text, TXTfw.Text, TXTdate.Text, xrecvd, xrels, TXTremarks.Text, TXTcmpy.Text, TXTrver.Text, TXTplate.Text, TXTdr.Text, TXTassign.Text)

conPubs.Close()
End If

End If
End Sub

**************

Public Function InsertImdAssign(ByVal xnav As String, ByVal xfreq As String, ByVal xsn As String, ByVal xfw As String, ByVal xdte As Date, ByVal xrecvd As String, ByVal xrels As String, ByVal xremarks As String, ByVal xcmpy As String, ByVal xrver As String, ByVal xplate As String, ByVal xdr As String, ByVal xassign As Date) As SqlDataReader
' Create Instance of Connection and Command Object
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim MyCommand As SqlCommand
Dim strCommand As String

strCommand = "INSERT INTO tbl_Inbox ([imd_nav],[imd_freq],[imd_sn],[imd_fw],[imd_date],[imd_recvd],[imd_rels],[imd_remarks],[imd_cmpy],[imd_rver],[imd_plate],[imd_dr],[imd_drdte])"
strCommand = strCommand + "VALUES ('" & UCase(xnav) & "','" & UCase(xfreq) & "','" & xsn & "','" & xfw & "','" & xdte & "','" & xrecvd & "','" & xrels & "','" & xremarks & "','" & xcmpy & "','" & xrver & "','" & xplate & "','" & xdr & "','" & xassign & "')"
MyCommand = New SqlCommand(strCommand, myConnection)
' Execute the command
myConnection.Open()
Dim result As SqlDataReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)

Return result
result.Close()
myConnection.Close()
End Function

What am I doing wrong?On what line do you get the error?
You never open conPubs.

Dim conPubs As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
rs1 = New SqlClient.SqlDataAdapter("SELECT * FROM tbl_Inbox WHERE imd_sn = '" & TXTsn.Text & "'", conPubs)

conPubs.Open()

ds = New DataSet
rs1.Fill(ds, "imd_nav")
error occurred on this line:

Functions.InsertImdAssign(TXTnav.Text, TXTfreq.Text, TXTsn.Text, TXTfw.Text, TXTdate.Text, xrecvd, xrels, TXTremarks.Text, TXTcmpy.Text, TXTrver.Text, TXTplate.Text, TXTdr.Text, TXTassign.Text)
all,

still having this problem.. error occured at functions..

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

kindly help
Set a breakpoint on this line (click the line, press F9 so a red dot appears beside the line, then press F5 to run the project):
Functions.InsertImdAssign(TXTnav.Text, TXTfreq.Text, TXTsn.Text, TXTfw.Text, TXTdate.Text, _
xrecvd, xrels, TXTremarks.Text, TXTcmpy.Text, TXTrver.Text, TXTplate.Text, TXTdr.Text, TXTassign.Text)
When this line is hit, and turns yellow, hover your mouse over each object - 'Functions', 'TXTfw' etc. One of these should give you an intellisense popup with a value of "nothing". If this is a textbox, make sure yuo've typed the name correctly, if this is another object like the "Functions" one, make sure it is instanciated and ready for use...
Don't you have to use 'Shared' if you want to call a function without instantiating first? I'm not sure, but give it a try.
Copy paste the entire error page for us please, so we can read the stack trace and the few lines of code it is highlighting.

Thanks.
A) It's ASP.Net, break points don't work the same way
B) I'm assuming the function is in the same class as the sub (or atleast an object is created somewhere for it). If it wasn't, they'd receive an error in design time and not be able to build it.
C) Your command is an INSERT, not a SELECT. A sqlDataReader won't be able to do anything with it. You need to use the ExecuteNonQuery member of MyCommand.
I don't see a class 'Functions'. Also, where do you create a new instance of it?
Copy paste the entire error page for us please, so we can read the stack trace and the few lines of code it is highlighting.

Thanks.

FUNCTIONS ERROR!!!

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
CMSnet.imd_outbox.cmdok_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\CMSnet\imd_outbox.aspx.vb:116
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain() +1277

0 comments:

Post a Comment