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

System.NullReferenceException in a datagrid..

Status
Not open for further replies.

Bob2

Programmer
Jul 3, 2000
228
0
0
SE
Hi


I have a datagrid wich consits of 6 items, 5 of them are visible but the 6'th value isn't visible and it can also be null. When I loop through the datagrid I get this error "System.NullReferenceException: Object reference not set to an instance of an object"

That error appears when it somes to the "If TextFileName.Text Is Nothing Then" line.

Am I doing this the wrong way or..? I don't know and could really use some help.

Here's the code I use....


***********************************

Dim TextFileName As TextBox

Dim FsObj As FileStream



For Each dgi In MyDataGrid.Items

Select Case dgi.ItemType

Case ListItemType.Item, ListItemType.AlternatingItem

'Use CartID to identify what item to delete or update

CartID = MyDataGrid.DataKeys(dgi.ItemIndex)

TextBoxQuantity = CType(dgi.FindControl("intQty"), TextBox)

TextFileName = CType(dgi.FindControl("FileName"), TextBox)





If CType(dgi.FindControl("DeleteFromCartCheckBox"), CheckBox).Checked = True Or (TextBoxQuantity.Text) = 0 Then

'Remove from Cart Code Here

If TextFileName.Text Is Nothing Then

Else

If File.Exists(TextFileName.Text.ToString) Then

File.Delete(TextFileName.Text.ToString)

'Response.Write ("Finns")

End If

End If

******************************************************


And a bit of the datagrid ....




<asp:TemplateColumn ItemStyle-Width=&quot;50&quot; ItemStyle-BorderWidth=&quot;0&quot; ItemStyle-HorizontalAlign=&quot;Center&quot; HeaderStyle-HorizontalAlign=&quot;Center&quot; Visible=&quot;False&quot; runat=&quot;server&quot;>

<ItemTemplate>

<asp:Label ID=&quot;lblFileName&quot; runat=&quot;server&quot; Text='<%# DataBinder.Eval(Container.DataItem, &quot;FileName&quot;) %>'/>

</ItemTemplate>

</asp:TemplateColumn>




Best Regards

/M



 
Could you have set the value of the Item or one the field you are testing to null?

I got the same error under c# and y resorted to Convert.DbNull

tellme if it works for you
 
Hi FluxAnomaly


Yes the value can be a null value, but how do you mean I can use &quot;Convert.DbNull&quot;? Can you give me an example?


/M
 
Did you try this?
If TextFileName Is Nothing Then
....

else if TextFileName.Text Is Nothing Then

else
...

Let me know if it works.
 
Either
If IsNothing(TextFileName.Text) = True Then
OR
If TextFileName.Text = &quot;&quot; Then

is the correct format.

Craig
 
Hi rk1962


It worked so far as I don't get any errors any more. But it Doesnt' delete the file that it should....

 
Hmmm


I found out that the reason it doesn't delete any files is beacuse it doesnt pass any value on...

What might be wrong in my code then.. The Datagrid part look like this...

<asp:Datagrid
AutogenerateColumns=&quot;false&quot; BackColor=&quot;#F5F6F5&quot; BorderWidth=&quot;1&quot;
cellpadding=&quot;0&quot;
cellspacing=&quot;0&quot;
DataKeyField=&quot;CartID&quot;
Font-Name=&quot;Verdana&quot;
Font-Size=&quot;8&quot;
Headerstyle-BackColor=&quot;#F5F6F5&quot;
Headerstyle-Font-Name=&quot;Arial&quot;
Headerstyle-Font-Size=&quot;8&quot; HorizontalAlign=&quot;Center&quot;
Id=&quot;MyDataGrid&quot; runat=&quot;server&quot;
ShowHeader=&quot;false&quot; Width=&quot;440&quot;
OnItemDataBound=&quot;DataGrid1_ItemDataBound&quot;
gridline=&quot;Horizontal&quot; >
<Columns>

<asp:TemplateColumn ItemStyle-Width=&quot;50&quot; ItemStyle-BorderWidth=&quot;0&quot; ItemStyle-HorizontalAlign=&quot;Center&quot; HeaderStyle-HorizontalAlign=&quot;Center&quot; Visible=&quot;False&quot; runat=&quot;server&quot;>
<ItemTemplate>
<asp:Label ID=&quot;lblFileName&quot; runat=&quot;server&quot; Text='<%# DataBinder.Eval(Container.DataItem, &quot;FileName&quot;) %>'/>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn ItemStyle-Width=&quot;50&quot; ItemStyle-BorderWidth=&quot;0&quot; ItemStyle-HorizontalAlign=&quot;Center&quot; HeaderStyle-HorizontalAlign=&quot;Center&quot; runat=&quot;server&quot;>
<ItemTemplate>
<asp:Label ID=&quot;lblFirst&quot; runat=&quot;server&quot; Text='<%# DataBinder.Eval(Container.DataItem, &quot;ArtNr&quot;) %>'/>
</ItemTemplate>
</asp:TemplateColumn>



Regards


M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top