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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object

Status
Not open for further replies.

olypost

Programmer
Apr 15, 2005
43
US
Hi, I'm very new to ASP.net. I'm attempting to write a simple web page that connects to sql server to insert a new row after getting a value entered in a textbox field. The problem is, I keep getting the error: "Object reference not set to an instance of an object.", and it points to a parameter variable in my code-behind file. If I manually set the value for the parameter (txtMemberNumber.Text) in the code-behind instead of trying to get it dynamically from the page, it works... I can't reference the value I type in on the page. What am I missing??? Thanks.

Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents txtMemberNumber As System.Web.UI.WebControls.TextBox

<snipped out>

Private Sub bInsert_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles bInsert.ServerClick
Dim str As String
str = "Insert into dbo.members values (@m1)"
Dim inscom As New SqlCommand(str, SqlConnection1)
SqlDataAdapter1.InsertCommand = inscom
SqlDataAdapter1.InsertCommand.Parameters.Add(New _
SqlParameter("@m1", SqlDbType.Int, 10))
SqlDataAdapter1.InsertCommand.Parameters("@m1").Value = _
txtMemberNumber.Text
SqlConnection1.Open()
SqlDataAdapter1.InsertCommand.ExecuteNonQuery()
SqlConnection1.Close()
End Sub
End Class
 
>>If I manually set the value for the parameter (txtMemberNumber.Text) in the code-behind instead of trying to get it dynamically from the page

confusing...

Known is handfull, Unknown is worldfull
 
What I meant was, in this code-behind, if I have

SqlDataAdapter1.InsertCommand.Parameters("@m1").Value =100

instead of typing in 100 in a browser on the opened .aspx
file, it works. txtMemberNumber.Text is a field on my page that I want to simply reference in the code-behind so that it inserts to the server.

 
It look like what you are doing should work. Are you sure that is the line it is referncing in the error?
 
Thanks. Yes the error window always points back to exactly where I'm assigning txtMemberNumber.Text to the parameter. For some reason, it just can't get the value from the page. Shouldn't these 2 lines:

Inherits System.Web.UI.Page
Protected WithEvents txtMemberNumber As System.Web.UI.WebControls.TextBox


enable the code-behind to know that txtMemberNumber.Text is coming from my web page??
 
do u by any chance set the textbox's value to empty in page_load???

Known is handfull, Unknown is worldfull
 
Here is a thought, I noticed you declard the parameter as an int, and trying to assign text to it. Try converting the text with:

Code:
SqlDataAdapter1.InsertCommand.Parameters("@m1").Value = _
         [b]CInt[/b](txtMemberNumber.Text)

Although I don't know why that would cause the error you received. But give it a shot any way and see what happens.
 
Thanks. Right now I'm not on a machine where I can see the code. When I do, I'll check the page_load. I think the only code there is what visual studio defaults there. I know I didn't customize page_load on my own. If something is indeed emptying the variable, would that override the inheritence from the page?

Also, I'll try the CINT. Thanks again.
 
I'm pretty knew to ASP/Web programming as well, but I've done something similar to what you are trying. The difference, is that inside the sub, I declared an instance of the object type that held my data, for example text box. I then got a reference to the text box on the form inside my sub variable. I was then able to get the text value out.

Hope that helps.
 
There's nothing at all in the page_load (should there be?). Also CINT didn't make a difference. Could you provide a working example of declaring an instance of the object type inside the sub? Thanks.

Here is the error:

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.

Source Error:


Line 98: SqlDataAdapter1.InsertCommand.Parameters("@m1").Value = _
Line 99: txtMemberNumber.Text
Line 100: SqlDataAdapter1.InsertCommand.Parameters("@m2").Value = "aaa"


Source File: c:\inetpub\ Line: 98
 
try debugging it, give:

response.write(txtMemberNumber.Text)
SqlDataAdapter1.InsertCommand.Parameters("@m1").Value = 100


Known is handfull, Unknown is worldfull
 
Thanks. Setting the value to 100 works, but even the response.write(txtMemberNumber.Text) throws the same error.

txtMemberNumber.Text = 500 also gives the same error.
 
I am totally stumped on how you can get that error if you dropped the textbox on your form and it generates the correct code.
At this point, I would delete that textbox from your from and then drop a new textbox on the form and see if that solves the problem.

Jim
 
Finally solved! The textboxes on my .aspx were <INPUT>, not System.Web.UI.WebControls.TextBox

In the designer, those textboxes didn't have the small green arrow icon at the top left. Dropped those textboxes, dragged new textboxes and now it works...

Don't know how I created those textboxes like that in the first place.

Thanks all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top