I am trying to an ImageUrl to an image in a Template Field in GridView but keep getting the error:
Object reference not set to an instance of an object. on this line:
I think you need to check that its a data row and not the header row
Try this
Protected Sub gvImages_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvImages.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim imagePath As String = rowView("image_path")
Dim strImageUrl As String = "~/admin/images/cases/" & Request.QueryString("uid") & "/" & imagePath
Dim imageFile As System.Web.UI.WebControls.Image = CType(e.Row.FindControl("imageFile"), System.Web.UI.WebControls.Image)
imageFile.ImageUrl = strImageUrl
End If
End Sub
try
Dim imagePath As String = e.rowView("image_path").ToString()
If the table binded with the grid has column "image_path", then instead use an easier way....
<asp:Image id="img1" runat="server" imageUrl = '<%#Eval("image_path")%>' />
If you want to build some custome string
imageurl = '<%# String.Format("{0} - {1} - {2} - {3}", Eval("Name1"), Eval("Name2"), Session["abc"],Request["abc"]) %>'