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 TouchToneTommy 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 ...

Status
Not open for further replies.

yeltom

Programmer
Sep 17, 2001
62
US
Hi, can someone look this over and help me find out why I keep getting the Object reference error. I have looked at the HTML and the names match exactly. Basically what I am trying to do is insert multiple records into the database using a data entry form. My form contains a datagrid with text boxes for the variables. Any input would be appreciated.


Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim _MID, _Desc, _Actual, _Target, _Thresh, _PED As TextBox
Dim aMID, aDesc, aActual, aTarget, aThresh, aPED As String
Dim MyRow As DataGridItem
Dim StringSQL As String
Dim counter As Integer


For counter = 0 To dgTrans.Items.Count - 1
MyRow = dgTrans.Items(counter)

_Desc = CType(FindControl("tbAddDesc"), TextBox)
_Actual = CType(FindControl("tbAddActual"), TextBox)
_Target = CType(FindControl("tbAddTarget"), TextBox)
_Thresh = CType(FindControl("tbAddThresh"), TextBox)
_MID = CType(FindControl("tbAddMID"), TextBox)
_PED = CType(FindControl("tbDate"), TextBox)

If Not _MID.Text = "" Then
aMID = CType(_MID.Text, String)
Else
aMID = "0"
End If
If Not _Desc.Text = "" Then
aDesc = CType(_Desc.Text, String)
Else
aDesc = "0"
End If
If Not _Actual.Text = "" Then
aActual = CType(_Actual.Text, String)
Else
aActual = "0"
End If
If Not _Target.Text = "" Then
aTarget = CType(_Target.Text, String)
Else
aTarget = "0"
End If
If Not _Thresh.Text = "" Then
aThresh = CType(_Thresh.Text, String)
Else
aThresh = "0"
End If
If Not _PED.Text = "" Then
aPED = CType(_PED.Text, String)
Else
aPED = "0"
End If




StringSQL = "INSERT INTO Transcription1 (MeasureId, Description, Acutal, Target, Threshold, PED) VALUES ("
StringSQL = StringSQL & "'" & aMID & "',"
StringSQL = StringSQL & "'" & aDesc & "',"
StringSQL = StringSQL & "'" & aActual & "',"
StringSQL = StringSQL & "'" & aTarget & "',"
StringSQL = StringSQL & "'" & aThresh & "',"
StringSQL = StringSQL & "'" & aPED & "')"
ExecuteStatement(StringSQL.ToString)
Next counter
 
Your calls to FindControl - you are calling the FindControl method of the page there. I believe what you want is for the current row of the datagrid to find the control, so it would be e.Item.FindControl("controlName").

As a general rule, whenever I use FindControl I always check to see if it actually found anything by checking for null (if it's Nothing).
Like
Code:
_PED = CType(e.Item.FindControl("tbDate"), TextBox)

if not _PED is nothing then
    'it found the control so you can do something with it
else
    'control not found by that name - 
end if


[pipe]
 
It would have helped to know which statement "blew up".

But TextBox can have Nothing as a value and "blow up" the
If Not _Desc.Text = "" Then

statement so try
_Desc = String.Empty & CType(FindControl("tbAddDesc"), TextBox)
OR
If not (_Desc Is Nothing OrElse _Desc.Text = "") Then



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Thank you for the responses but I am still not there. Now my question is if I am to use e.Item.findcontrol should the e param type be e as System.Web.UI.WebControls.DataGridItemEventArgs or something else because Item is not a member of system.eventargs.
 
sorry - I missed that. Disregrad that part. Are you cheking for nothing on those controls?

[pipe]
 
Yes I am checking for nothing in the code but removed it for the post.
 
I Figured it out. I had to use
CType(MyRow.FindControl("tbAddDesc"), TextBox) and so on to get it to work.
Thanks guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top