Am I blind?

R

Roy

Sorry for the sea of code. For no apparent reason that I can tell, this
section of code began producing an "Object reference not set to an
instance of an object." error. The error occurs on the line I indicate
below. The code below is nothing in-depth, some variables and a darn
editable datagrid... I just don't get it.




FVDConnection = New
SqlConnection("server=localhost;uid=itv;pwd=;database=test")
Dim strSQLQuery As String

'Assign variables to columns
Dim strBook As String = CType(e.Item.Cells(1).Controls(0),
TextBox).Text
Dim strVan As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim strContNum as String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
Dim strVDN as String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim strPoE as String = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Dim strShipName as String = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
Dim strPod as String = CType(e.Item.Cells(8).Controls(0), TextBox).Text
Dim strCarrier As String = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
Dim strPCFN as String = CType(e.Item.Cells(10).Controls(0),
TextBox).Text
Dim strTCN as String = CType(e.Item.Cells(11).Controls(0),
TextBox).Text
Dim strLastEvent As String = CType(e.Item.Cells(12).Controls(0),
TextBox).Text
Dim strLELoc as String = CType(e.Item.Cells(13).Controls(0),
TextBox).Text
Dim strLEShipName as String = CType(e.Item.Cells(15).Controls(0),
TextBox).Text
Dim strLESCAC as String = CType(e.Item.Cells(16).Controls(0),
TextBox).Text
Dim strCCity as String = CType(e.Item.Cells(17).Controls(0),
TextBox).Text
Dim strRecon as String = CType(e.Item.Cells(18).Controls(0),
TextBox).Text


********ERROR OCCURS ON FIRST LINE OF THIS strSQLQuery statement***

strSQLQuery = "UPDATE [firstvd] " & _
"SET carrier_booking_nr = N'" & Replace(strBook.Trim, "'", "''") & "',
" & _
"van_owner = '" & Replace(strVan.Trim, "'", "''") & "', " & _
"tcon_container_num = N'" & Replace(strContNum.Trim, "'", "''") & "', "
& _
"voydoc = '" & Replace(strVDN.Trim, "'", "''") & "', " & _
"poe = '" & Replace(strPoE.Trim, "'", "''") & "', " & _
"Ship_Name = '" & Replace(strShipName.Trim, "'", "''") & "', " & _
"pod = '" & Replace(strPoD.Trim, "'", "''") & "', " & _
"ocean_carrier_cd = N'" & Replace(strCarrier.Trim, "'", "''") & "', " &
_
"PCFN = '" & Replace(strPCFN.Trim, "'", "''") & "', " & _
"tcn = '" & Replace(strTCN.Trim, "'", "''") & "', " & _
"lastevent = '" & Replace(strLastEvent.Trim, "'", "''") & "', " & _
"lasteventloc = '" & Replace(strLELoc.Trim, "'", "''") & "', " & _
"Lastevent_shipname = N'" & Replace(strLEShipName.Trim, "'", "''") &
"', " & _
"lastevent_scac = N'" & Replace(strLEScac.Trim, "'", "''") & "', " & _
"consigneecity = N'" & Replace(strCCity.Trim, "'", "''") & "', " & _
"Recon_Status = '" & Replace(strRecon.Trim, "'", "''") & "' " & _
"WHERE carrier_booking_nr = '"& strPreBook.Trim & "' AND van_owner =
'"& strPreVan.Trim &"' AND tcon_container_num = '"& strPreCNUM.Trim
&"'"

FVDConnection.Open()
Dim objCommand As SqlCommand = New SqlCommand(strSQLQuery,
FVDConnection)
objCommand.ExecuteNonQuery()
FVDConnection.Close()
 
B

BG

How about something like this?

Sub DataGrid1_Update(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
Dim strBook As String =
Convert.ToString(CType(e.Item.FindControl("textBoxName"), TextBox).Text)
TextBox).Text

End Sub



Roy said:
Sorry for the sea of code. For no apparent reason that I can tell, this
section of code began producing an "Object reference not set to an
instance of an object." error. The error occurs on the line I indicate
below. The code below is nothing in-depth, some variables and a darn
editable datagrid... I just don't get it.




FVDConnection = New
SqlConnection("server=localhost;uid=itv;pwd=;database=test")
Dim strSQLQuery As String

'Assign variables to columns
Dim strBook As String = CType(e.Item.Cells(1).Controls(0),
TextBox).Text
Dim strVan As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim strContNum as String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
Dim strVDN as String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim strPoE as String = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Dim strShipName as String = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
Dim strPod as String = CType(e.Item.Cells(8).Controls(0), TextBox).Text
Dim strCarrier As String = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
Dim strPCFN as String = CType(e.Item.Cells(10).Controls(0),
TextBox).Text
Dim strTCN as String = CType(e.Item.Cells(11).Controls(0),
TextBox).Text
Dim strLastEvent As String = CType(e.Item.Cells(12).Controls(0),
TextBox).Text
Dim strLELoc as String = CType(e.Item.Cells(13).Controls(0),
TextBox).Text
Dim strLEShipName as String = CType(e.Item.Cells(15).Controls(0),
TextBox).Text
Dim strLESCAC as String = CType(e.Item.Cells(16).Controls(0),
TextBox).Text
Dim strCCity as String = CType(e.Item.Cells(17).Controls(0),
TextBox).Text
Dim strRecon as String = CType(e.Item.Cells(18).Controls(0),
TextBox).Text


********ERROR OCCURS ON FIRST LINE OF THIS strSQLQuery statement***

strSQLQuery = "UPDATE [firstvd] " & _
"SET carrier_booking_nr = N'" & Replace(strBook.Trim, "'", "''") & "',
" & _
"van_owner = '" & Replace(strVan.Trim, "'", "''") & "', " & _
"tcon_container_num = N'" & Replace(strContNum.Trim, "'", "''") & "', "
& _
"voydoc = '" & Replace(strVDN.Trim, "'", "''") & "', " & _
"poe = '" & Replace(strPoE.Trim, "'", "''") & "', " & _
"Ship_Name = '" & Replace(strShipName.Trim, "'", "''") & "', " & _
"pod = '" & Replace(strPoD.Trim, "'", "''") & "', " & _
"ocean_carrier_cd = N'" & Replace(strCarrier.Trim, "'", "''") & "', " &
_
"PCFN = '" & Replace(strPCFN.Trim, "'", "''") & "', " & _
"tcn = '" & Replace(strTCN.Trim, "'", "''") & "', " & _
"lastevent = '" & Replace(strLastEvent.Trim, "'", "''") & "', " & _
"lasteventloc = '" & Replace(strLELoc.Trim, "'", "''") & "', " & _
"Lastevent_shipname = N'" & Replace(strLEShipName.Trim, "'", "''") &
"', " & _
"lastevent_scac = N'" & Replace(strLEScac.Trim, "'", "''") & "', " & _
"consigneecity = N'" & Replace(strCCity.Trim, "'", "''") & "', " & _
"Recon_Status = '" & Replace(strRecon.Trim, "'", "''") & "' " & _
"WHERE carrier_booking_nr = '"& strPreBook.Trim & "' AND van_owner =
'"& strPreVan.Trim &"' AND tcon_container_num = '"& strPreCNUM.Trim
&"'"

FVDConnection.Open()
Dim objCommand As SqlCommand = New SqlCommand(strSQLQuery,
FVDConnection)
objCommand.ExecuteNonQuery()
FVDConnection.Close()
 
C

Craig Deelsnyder

Sorry for the sea of code. For no apparent reason that I can tell, this
section of code began producing an "Object reference not set to an
instance of an object." error. The error occurs on the line I indicate
below. The code below is nothing in-depth, some variables and a darn
editable datagrid... I just don't get it.




FVDConnection = New
SqlConnection("server=localhost;uid=itv;pwd=;database=test")
Dim strSQLQuery As String

'Assign variables to columns
Dim strBook As String = CType(e.Item.Cells(1).Controls(0),
TextBox).Text
Dim strVan As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim strContNum as String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
Dim strVDN as String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim strPoE as String = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Dim strShipName as String = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
Dim strPod as String = CType(e.Item.Cells(8).Controls(0), TextBox).Text
Dim strCarrier As String = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
Dim strPCFN as String = CType(e.Item.Cells(10).Controls(0),
TextBox).Text
Dim strTCN as String = CType(e.Item.Cells(11).Controls(0),
TextBox).Text
Dim strLastEvent As String = CType(e.Item.Cells(12).Controls(0),
TextBox).Text
Dim strLELoc as String = CType(e.Item.Cells(13).Controls(0),
TextBox).Text
Dim strLEShipName as String = CType(e.Item.Cells(15).Controls(0),
TextBox).Text
Dim strLESCAC as String = CType(e.Item.Cells(16).Controls(0),
TextBox).Text
Dim strCCity as String = CType(e.Item.Cells(17).Controls(0),
TextBox).Text
Dim strRecon as String = CType(e.Item.Cells(18).Controls(0),
TextBox).Text


********ERROR OCCURS ON FIRST LINE OF THIS strSQLQuery statement***

strSQLQuery = "UPDATE [firstvd] " & _
"SET carrier_booking_nr = N'" & Replace(strBook.Trim, "'", "''") & "',
" & _
"van_owner = '" & Replace(strVan.Trim, "'", "''") & "', " & _
"tcon_container_num = N'" & Replace(strContNum.Trim, "'", "''") & "', "
& _
"voydoc = '" & Replace(strVDN.Trim, "'", "''") & "', " & _
"poe = '" & Replace(strPoE.Trim, "'", "''") & "', " & _
"Ship_Name = '" & Replace(strShipName.Trim, "'", "''") & "', " & _
"pod = '" & Replace(strPoD.Trim, "'", "''") & "', " & _
"ocean_carrier_cd = N'" & Replace(strCarrier.Trim, "'", "''") & "', " &
_
"PCFN = '" & Replace(strPCFN.Trim, "'", "''") & "', " & _
"tcn = '" & Replace(strTCN.Trim, "'", "''") & "', " & _
"lastevent = '" & Replace(strLastEvent.Trim, "'", "''") & "', " & _
"lasteventloc = '" & Replace(strLELoc.Trim, "'", "''") & "', " & _
"Lastevent_shipname = N'" & Replace(strLEShipName.Trim, "'", "''") &
"', " & _
"lastevent_scac = N'" & Replace(strLEScac.Trim, "'", "''") & "', " & _
"consigneecity = N'" & Replace(strCCity.Trim, "'", "''") & "', " & _
"Recon_Status = '" & Replace(strRecon.Trim, "'", "''") & "' " & _
"WHERE carrier_booking_nr = '"& strPreBook.Trim & "' AND van_owner =
'"& strPreVan.Trim &"' AND tcon_container_num = '"& strPreCNUM.Trim
&"'"

FVDConnection.Open()
Dim objCommand As SqlCommand = New SqlCommand(strSQLQuery,
FVDConnection)
objCommand.ExecuteNonQuery()
FVDConnection.Close()

well, I don't have time to cross-check them all, but I believe I see 15
variable declarations, but you have 19 variables concatenated into your
SQL string...make sure all variables are declared and initialized
properly, as one of your .Trim statements is failing due to a null
variable...
 
R

Roy

Hrmm. As you two correctly guessed (forgot to mention it), the code
above is within the update command of my datagrid. BG, I'll give it a
try and get back to ya. Craig, the missing declarations are globals and
are set = fields in the edit command of my datagrid. I have to pre-set
them before the user edits the table so the query updates the right
record. I've done a response write on the globals and they're all
pulling the right data when the edit button is clicked...

Here's another tip, btw, the error doesn't kick off until the Update
button is clicked and the update command is activated. Besides that,
the page runs fine. Talk about frustrating.
 
R

Roy

Interesting...

I've figured out *how* the error is being caused, I'm just clueless as
to *why.* You were right about the .Trim statements Craig. Here's the
scoop:

The error occurs in the last line of the strSQLQuery: "WHERE
carrier_booking_nr = '"& strPreBook.Trim & "' AND van­_owner = '"&
strPreVan.Trim &"' AND tcon_container_num = '"& strPreCN­UM.Trim &"'"

The system isn't seeing the values of these 3 global variables values.
I declare and initialize them as demonstrated simplisticly here:

--Class
----Declare Global Var's

----Sub edit_command
-------set GV's = to the correct grid cells

----Sub update_command
-------kick off query which fails to work because it sees the GV's as
being null

Now, using a response.write I can tell the GV's are being set within
the edit_command, by why isn't their value carrying over to the
update_command? I have enableviewstate = "true," what else can I do
short of setting them equal to session variables, which I'd rather not
do...

Any ideas, theories, etc...?
 
R

Roy

Server is a workstation running windows server 2003 which one other
developer is using as well as his test station. Could he have
inadvertantly changed a setting in sql server or IIS that generates
such errors?

I'm clueless at this point, just grasping at straws...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top