Link to home
Start Free TrialLog in
Avatar of kdeutsch
kdeutschFlag for United States of America

asked on

Item bata bound event causing an error

I am trying to create a databound event for my hyperlink when it is blank from the database.
but when I impliment it it causes errors on the datagrid, but when I put same code in db it works, it also works when I comment out the ItemDatabound event, but what happens is some items are not links on the datagrid but just blank.


Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemDataBound
        Dim Fixpage As String = e.Item.Cells(4).Text
        Dim hl As HyperLink = e.Item.Cells(4).FindControl("hlFix")

        If Fixpage = " " Or Fixpage = "" Then
            hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
        End If
    End Sub


<asp:TemplateColumn HeaderText="Fix Page">
											<ItemTemplate>
												<asp:Hyperlink id="hlFix" runat="server" Text="Fix Page" NavigateUrl='<%# DataBinder.Eval (Container.DataItem,"strFilterPage") %>' Target="_blank"></asp:Hyperlink>  
											</ItemTemplate>
										</asp:TemplateColumn>

Open in new window

Avatar of guru_sami
guru_sami
Flag of United States of America image

Whats the error ?

May  be because NavigateUrl='<%# DataBinder.Eval (Container.DataItem,"strFilterPage") %>' is throwing error due to null value in database.

Can you try:
.aspx
         NavigateUrl='<%# GetUrl(Eval("strFilterPage"))%>'

.aspx.vb

Public Function GetUrl(ByVal strFilterPage As Object) As String
    Dim url As String = DirectCast(strFilterPage, String)
    If strFilterPage Is Nothing or strFilterPage = String.EmptyThen
        url = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
    End If
    Return url
End Function

Note: There could be some other way....
Avatar of kdeutsch

ASKER

Sorry this is the error,

When I comment out these lines it works good,
If Fixpage = "&nbsp;" Or Fixpage = "" Then
            hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
        End If

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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   MobTrackerDotNet.StandardReports.myDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e) in R:\Inetpub\wwwroot\MobTrackerDotNet\Reports\StandardReports.aspx.vb:568
   System.Web.UI.WebControls.DataGrid.OnItemDataBound(DataGridItemEventArgs e) +110
   System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource) +181
   System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource) +1485
   System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +49
   System.Web.UI.WebControls.BaseDataList.DataBind() +23
   MobTrackerDotNet.StandardReports.FillDataGrid() in R:\Inetpub\wwwroot\MobTrackerDotNet\Reports\StandardReports.aspx.vb:165
   MobTrackerDotNet.StandardReports.ddlDeployment_SelectedIndexChanged(Object sender, EventArgs e) in R:\Inetpub\wwwroot\MobTrackerDotNet\Reports\StandardReports.aspx.vb:93
   System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +108
   System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +26
   System.Web.UI.Page.RaiseChangedEvents() +115
   System.Web.UI.Page.ProcessRequestMain() +1099

 
ok add a check before that...
if hl IsNot nothing then
SOLUTION
Avatar of codingbeaver
codingbeaver
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
HL isNot Nothing gives me Blue errors, still working in 1.1.
Tried this but gives me errors as well
If hl = "" Then
            If Fixpage = "&nbsp;" Or Fixpage = "" Then
                hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
            End If
        End If
Oh,
the erro for that is Operator is not defined for types system.web.uic.webcontrols.hyperlink & string
All,
the realization just hit me that checking agianst the text in the cell will never work because it will never be blank, it will always have Fix page in it, but what is blank is the data behind the link.  How do I access this data????
In your ItemDataBound event handler, you will first need to check if it is a Header row or a Item row, because the event handler loops through all rows in the DataGrid, starting from the Header row.
Ok, I am really getting confused witht this now, it does not give the error above when I put in the alternating item it seemed to disapppear, but some elements in cell 4 of the datagrid do not appear with a blue link becuase they have no database element attached to them.  I put in the check for header footer but I don't see how thats solving the problem of checking for data behind the link.

 If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
            Dim Fixpage As String = e.Item.Cells(4).Text
            Dim hl As HyperLink = e.Item.Cells(4).FindControl("hlFix")
            If (e.Item.ItemType = ListItemType.Header Or e.Item.ItemType = ListItemType.Footer) Then
                If e.Item.Cells(4).Text = "&nbsp;" Then
                    hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
                End If
            End If
        End If
Are you trying to change the url only for header and footer? If not, then remove the checking for Header and Footer.
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
	Dim Fixpage As String = e.Item.Cells(4).Text
	Dim hl As HyperLink = e.Item.Cells(4).FindControl("hlFix")
	If (Not hl Is Nothing) Then
		If e.Item.Cells(4).Text = "&nbsp;" Then
			hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
		End If
	End If
End If

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it to work by going directly to the datarow.  Thanks for everyones help.  I had to spread points on this because I would be remiss if I did not acknowledge help from others.