Object reference not set to an instance of an object

monfu

Member
Joined
Jul 2, 2004
Messages
8
Location
Malta
Programming Experience
3-5
Dear All

I have this piece of code:-

Dim r As RepeaterItem
Dim bln As Integer
For Each r In rptEvManage.Items
Dim strFileName As String = CType(r.FindControl("uplEventFile"), HtmlInputFile).PostedFile.FileName
Next

And it is giving me an error on Dim strFileName. The error is :-

Object reference not set to an instance of an object

Can you please help?

Thanks

Johann
 
you could try:
VB.NET:
 Dim strFileName As String
 For Each r As RepeaterItem In rptEvManage.Items
  strFileName = CType(r.FindControl("uplEventFile"),HtmlInputFile).PostedFile.FileName
 Next r
 
I beleive the code JuggaloBrotha has posted will do the exact same thing as the original code. Declaring the variable in the for loop was introduced in V1.1 of the .NET framework, but it does essentially the same thing as declaring it in a previous line of code.
The problem may be that the r.FindControl("uplEventFile") call doesn't produce a result. I would suggest breaking the code up and stepping thru the code to see the value of the above call to FindControl.
 
Thanks guys

I solved it like this

Dim ctrl As Control = r.FindControl("uplEventFile")
If Not ctrl Is Nothing Then
Dim file As HtmlInputFile = CType(ctrl, HtmlInputFile)
If Not file.PostedFile Is Nothing Then
Dim strFileName As String = file.PostedFile.FileName
Dim c As String = System.IO.Path.GetFileName(strFileName)
'Dim p as String =
If Not (strFileName = "") Then
CType(r.FindControl("uplEventFile"), HtmlInputFile).PostedFile.SaveAs(ConfigurationSettings.AppSettings.Get("dataPath") + c)
EDITFileExists =
True
Else
EDITFileExists = False
End If
Else
EDITFileExists = False
End If
End If
 
add validation

hi,
add validation to ur loop, i.e)
Dim r As RepeaterItem
Dim bln As Integer
For Each r In rptEvManage.Items
if not isNothing(r.FindControl("uplEventFile")) Then
Dim strFileName As String = CType(r.FindControl("uplEventFile"), HtmlInputFile).PostedFile.FileName
Endif
Next
 
Back
Top