?
Solved

Item bata bound event causing an error

Posted on 2010-01-12
12
Medium Priority
?
329 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:kdeutsch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 26297097
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....
0
 

Author Comment

by:kdeutsch
ID: 26297295
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

 
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 26297386
ok add a check before that...
if hl IsNot nothing then
0
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 
LVL 10

Assisted Solution

by:codingbeaver
codingbeaver earned 500 total points
ID: 26297398
Change it to this:
If hl IsNot Nothing 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

Open in new window

0
 

Author Comment

by:kdeutsch
ID: 26297657
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
0
 

Author Comment

by:kdeutsch
ID: 26297674
Oh,
the erro for that is Operator is not defined for types system.web.uic.webcontrols.hyperlink & string
0
 

Author Comment

by:kdeutsch
ID: 26297994
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????
0
 
LVL 10

Expert Comment

by:codingbeaver
ID: 26298137
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.
0
 

Author Comment

by:kdeutsch
ID: 26298194
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
0
 
LVL 10

Expert Comment

by:codingbeaver
ID: 26298257
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

0
 
LVL 41

Accepted Solution

by:
guru_sami earned 1100 total points
ID: 26298958
not sure if you want to try this as well to get the value directly from datarow:

Dim drv As System.Data.DataRowView = DirectCast((e.Item.DataItem), System.Data.DataRowView)
Dim filterPage As String = drv.Row("strFilterPage").ToString()

filterPage is the value fetched from Database so compare it and do the work as per your requirement.
0
 

Author Closing Comment

by:kdeutsch
ID: 31676312
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.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question