Problem with QueryString

I have built a report using a datagrid, and one of the columns is a template column in which I place a hyperlink control. While viewing the report, a user may click on a link to bring that record up in a form where they can make edits/updates.

The hypelink column is tied to the street address, and additionally, I am sending some other values in the string. This works very well except for those cases where the street address contains an ampersand "&". Our buildings location table has a number of entrries that use this as opposed to a typical street address, and it plays havoc with my query strings. Tuncating them after the ampersand, so rather than sending "Smith & Vine", I would get "Smith", for which no match would be found, leaving me with the dreaded "there is no row at position 0."  exception.

Does anyone know a way around this? The code I'm using currently is listed below.

Thanks,
Jeff

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem
                Dim link As HyperLink = DirectCast(e.Item.FindControl("HyperLink1"), HyperLink)
                link.NavigateUrl = String.Format("http://dcsdesktopprojects.verizon.com/pcRefresh/editLocationInfo.aspx?Street={0}", DataBinder.Eval(e.Item.DataItem, "StreetAddress"))
                link.NavigateUrl += "&City=" & e.Item.Cells(1).Text & "&State=" & e.Item.Cells(2).Text
                link.Text = DataBinder.Eval(e.Item.DataItem, "StreetAddress").ToString
        End Select
 End Sub
jpetterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
the way around it would be to replace the & in the original text (using 'amp;' ?) and then unscrmabling it on the other page... like this

link.NavigateUrl = String.Format("http://dcsdesktopprojects.verizon.com/pcRefresh/editLocationInfo.aspx?Street={0}", Replace(DataBinder.Eval(e.Item.DataItem, "StreetAddress")),"&","ampersand")

On the other page...

address = Replace(Request.QueryString("Street"),"ampersand","&")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeff CertainCommented:
you could also replace it with the HTML equivalent (%26)
jpetterAuthor Commented:
Chaosian,

Wow, that sounds cool. Let me try it quick and I'll get right back.

Thanks,
Jeff
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Fred GoodwinVP of Software DevelopmentCommented:
Try using server.urlencode and server.urldecode or you can use %26 in place of the &
NowaYCommented:
I think this should solve your problem (note the server.htmlEncode):

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem
                Dim link As HyperLink = DirectCast(e.Item.FindControl("HyperLink1"), HyperLink)
                link.NavigateUrl = String.Format("http://dcsdesktopprojects.verizon.com/pcRefresh/editLocationInfo.aspx?Street={0}", DataBinder.Eval(e.Item.DataItem, "StreetAddress"))
                link.NavigateUrl += "&City=" & e.Item.Cells(1).Text & "&State=" & e.Item.Cells(2).Text
                link.NavigateUrl = server.HtmlEncode(link.NavigateUrl )
                link.Text = DataBinder.Eval(e.Item.DataItem, "StreetAddress").ToString
        End Select
 End Sub
Jeff CertainCommented:
Noway,

Won'tl that encode the other  &  required to delimit the state and city portions of the querystring????

You might be better off trying:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem
                Dim link As HyperLink = DirectCast(e.Item.FindControl("HyperLink1"), HyperLink)
                link.NavigateUrl = String.Format("http://dcsdesktopprojects.verizon.com/pcRefresh/editLocationInfo.aspx?Street={0}", server.HtmlEncode(DataBinder.Eval(e.Item.DataItem, "StreetAddress")))
                link.NavigateUrl += "&City=" & e.Item.Cells(1).Text & "&State=" & e.Item.Cells(2).Text
                link.Text = DataBinder.Eval(e.Item.DataItem, "StreetAddress").ToString
        End Select
 End Sub
NowaYCommented:
Indeed it would. Your last post is the correct method. Good catch!
jpetterAuthor Commented:
Chaosian,

Your first suggestion worked like a dream. Thanks.

Thanks to all for the help.

Jeff
Jeff CertainCommented:
Jeff,

Noway's solution is probecbly the correct (and more elegant) solution -- mine's just a twisted work-around. You might want to try it... remembering , of course, that HTMLDecode will be required on the other end to clean things up. This approach will avoid problems with other prohibited URL characters...

Jeff
jpetterAuthor Commented:
Chaosian,
I appreciate your candor. I'll give Noway's solution a try when I get some breathing room, and if it works okay will try to find a way to tack on some points for him.

Thank,
Jeff
NowaYCommented:
I use quick and dirty sometimes. If it works... it work!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.