Object reference not set to an instance of an object.

  • Thread starter Thread starter TN Bella
  • Start date Start date
T

TN Bella

I can't figure this out and I need some help...please!
Object reference not set to an instance of an object.

'Insert data into the account table
Sub doInsert2(Source as Object, E as EventArgs)
Dim strConn as string =
"server=abcdef;database=a_;trusted_connection=true"
Dim Mysql1 as string
Dim MyConn1 As New SqlConnection(strConn)
MySql1 = "INSERT INTO
a_p.dbo.tbl_ap_web_ce_inv_account (acct_num, cst_cntr, ref_num,
acct_amt, splr_cd, inv_numb, inv_date, user_id) VALUES ('" &
txtacctnum.Text & "','" & txtCostCntr.text & "','" & txtRefNum.text &
"','" & txtAcctAmt.text & "','" & txtSupCD.text & "','" & txtInvNum.text
& "','" & txtInvDate.text & "','" & strUser & "')"
Dim Cmd1 As New SqlCommand(MySql1, MyConn1)
MyConn1.Open()
Cmd1.ExecuteNonQuery()
MyConn1.Close()

Dim i As Int32
For i = 1 To 14
HERE: Dim MySql2 As String
MySql2 = "INSERT INTO
acct_payable.dbo.tbl_ap_web_ce_inv_account (acct_num, cst_cntr, ref_num,
acct_amt, splr_cd, inv_numb, inv_date, user_id) VALUES ("
If CType(Me.FindControl("txtAcctNum" & i),
TextBox).Text ="" Then
i +=1
GoTo HERE:
Else
MySql2 &= "'" &
CType(Me.FindControl("txtAcctNum" & i), TextBox).Text & "',"
End If
If CType(Me.FindControl("txtCostCntr" & i),
TextBox).Text ="" Then
i +=1
GoTo HERE:
Else
MySql2 &= "'" &
CType(Me.FindControl("txtCostCntr" & i), TextBox).Text & "',"
End If
If CType(Me.FindControl("txtRefNum" & i),
TextBox).Text ="" Then
i +=1
GoTo HERE:
Else
MySql2 &= "'" &
CType(Me.FindControl("txtRefNum" & i), TextBox).Text & "',"
End If
If CType(Me.FindControl("txtAcctAmt" & i),
TextBox).Text ="" Then
i +=1
GoTo HERE:
Else
MySql2 &= "'" &
CType(Me.FindControl("txtAcctAmt" & i), TextBox).Text & "',"
End If
MySql2 &= "'" & txtSupCD.text & "',"
MySql2 &= "'" & txtInvNum.text & "',"
MySql2 &= "'" & txtInvDate.text & "',"
MySql2 &= "'" & strUser & "')"
Cmd1= New SqlCommand(MySql2, MyConn1)
MyConn1.Open()
Cmd1.ExecuteNonQuery()
MyConn1.Close()
Next
finalpage(source, e)

End Sub

THANKS!
 
How about narrowing it down for us to the line number mentioned in the
errror message?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Okay, Null REference Exception means that you're attempting to reference an
object that does not exist. So, let's analyze your line of code:
If CType(Me.FindControl("txtAcctNum" & i),
TextBox).Text ="" Then

There is only one reference to an object in this statement. It is the object
referenced by the rerturn value of the FindControl method. Since you're
getting a Null Reference exception, that object must be null or Nothing.

Why? Most likely, it's the reference to "Me". "Me" is the Page class. The
FindControl() Method of a Control searches the immediate children of a
Control, not the Children of its Children. I would guess that your textbox
is inside a WebForm, which would mean that it is a Child Control of the
Form, not the Page.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top