Showing posts with label insert. Show all posts
Showing posts with label insert. Show all posts

Thursday, March 22, 2012

Object reference not set to an instance of an object error

In an asp.net insert data script, I'm getting an Object reference not
set to an instance of an object error on the line that creates the
parameter for @dotnet.itags.org.sport. Not sure why, since I am inserting the field
name correctly in the sql insert statement:
''
Dim InsertCmd As String = "insert into tblArticles
(sport,articleheader) values (@dotnet.itags.org.sport,@dotnet.itags.org.articleheader)"
MyCommand.Parameters.Add(New SqlParameter("@dotnet.itags.org.Sport",
SqlDbType.VarChar, 50))
MyCommand.Parameters("@dotnet.itags.org.Sport").Value =
Server.HtmlEncode(sport.Value)
''''
Here is how I am tagging the input box:
<input type="text" id="sport" value="" runat="server">
'
Thanks in advance
ChumleyYou may want to make sure that an instance of your control was created in th
e code behind. It is usually collapsed at the top of your code behind it sh
ould looks something like
protected withevents sport as system.web.ui.webcontrols.textbox
Todd Meister
"Chumley the Walrus" wrote:

> In an asp.net insert data script, I'm getting an Object reference not
> set to an instance of an object error on the line that creates the
> parameter for @.sport. Not sure why, since I am inserting the field
> name correctly in the sql insert statement:
> ''
> Dim InsertCmd As String = "insert into tblArticles
> (sport,articleheader) values (@.sport,@.articleheader)"
> MyCommand.Parameters.Add(New SqlParameter("@.Sport",
> SqlDbType.VarChar, 50))
> MyCommand.Parameters("@.Sport").Value =
> Server.HtmlEncode(sport.Value)
> ''''
> Here is how I am tagging the input box:
> <input type="text" id="sport" value="" runat="server">
>
> '
> Thanks in advance
> Chumley
>

Object reference not set to an instance of an object error

In an asp.net insert data script, I'm getting an Object reference not
set to an instance of an object error on the line that creates the
parameter for @dotnet.itags.org.sport. Not sure why, since I am inserting the field
name correctly in the sql insert statement:

''
Dim InsertCmd As String = "insert into tblArticles
(sport,articleheader) values (@dotnet.itags.org.sport,@dotnet.itags.org.articleheader)"

MyCommand.Parameters.Add(New SqlParameter("@dotnet.itags.org.Sport",
SqlDbType.VarChar, 50))
MyCommand.Parameters("@dotnet.itags.org.Sport").Value =
Server.HtmlEncode(sport.Value)
''''

Here is how I am tagging the input box:

<input type="text" id="sport" value="" runat="server"
??
Thanks in advance
ChumleyDid you declare the control sport ?

The declaration of the control in the aspx should look like:

<asp:TextBox id="sport" runat="server"></asp:TextBox
and in the code behind


protected System.Web.UI.WebControls.TextBox sport;

yeh, i tried declaring the control for the text box and sport, but i got a "value isnt a member of the textbox" error; when i tried using .text with those parameter codings, i got a ".text isnt a member of the parameters" error.

I'm not using code behind (i don't have vs.net) nor c#, so :

protected System.Web.UI.WebControls.TextBox sport;

doesnt make sense
what event takes place when the code is executed.

In the page load add if(!IsPostBack)

maybe that will help
maybe add Name="sport" to your html input textbox and see if that works.

And why aren't you using an <asp:textbox> control?
I don't think value is a valid property... at least for that control... did you try this?

<INPUT id="Text1" type="text" name="Text1" runat="server" value="AA"

Object reference not set to an instance of an object.

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!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!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.

"TN Bella" <tina.pardi@.nav-international.com> wrote in message
news:#4nZ5KPeEHA.3944@.tk2msftngp13.phx.gbl...
> 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!
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
Sure:
If CType(Me.FindControl("txtAcctNum" & i),
TextBox).Text ="" Then

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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.

"TN Bella" <tina.pardi@.nav-international.com> wrote in message
news:e$QIx6VeEHA.2352@.TK2MSFTNGP09.phx.gbl...
> Sure:
> If CType(Me.FindControl("txtAcctNum" & i),
> TextBox).Text ="" Then
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!

Friday, March 16, 2012

Object reference not set to an instance of an object.

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_connec
tion=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!
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!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.
"TN Bella" <tina.pardi@.nav-international.com> wrote in message
news:#4nZ5KPeEHA.3944@.tk2msftngp13.phx.gbl...
> 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_connec
tion=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!
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!
Sure:
If CType(Me.FindControl("txtAcctNum" & i),
TextBox).Text ="" Then
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!
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.
"TN Bella" <tina.pardi@.nav-international.com> wrote in message
news:e$QIx6VeEHA.2352@.TK2MSFTNGP09.phx.gbl...
> Sure:
> If CType(Me.FindControl("txtAcctNum" & i),
> TextBox).Text ="" Then
>
>
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!