tags:

views:

38

answers:

2
+2  Q: 

VB.NET QueryString

I am using Datagride and I have a I have passed some parameters in it the code is as follow:

<asp:TemplateField HeaderText="Download">
                <ItemTemplate>
               <asp:LinkButton ID="lnkname" runat="server" Text="Download" PostBackUrl='<%#"~/logout.aspx?ID="+Eval("ID")+"&category=mobile"%>'></asp:LinkButton>
               </ItemTemplate>
               </asp:TemplateField>

I have numeric field (ID) which i need to send through URL I have tried to send the String type data and its working fine but while I am sending numeric type (ID) I am facing this error Conversion from string "~/logout.aspx?ID=" to type 'Double' is not valid I know something i need to change in the syntax near Eval("ID"). How should i send numeric data in querystring. Thanks

+1  A: 

The <%# syntax should just be used for evaluating a specific data item. You are trying to build up the entire string in the <%# which is not necessary since most of the data is static.

Just a hunch as to why you are getting the error in the ID specific scenario is because you have the whole item string and double information in your binding <%# it is using the type of the Eval statement to figure out what the whole section should be. In your case it is looking at the value of Eval("ID") and knows it is a double and it is then expecting the binded result to be a double. Problem is your concatenating the string and the double so that wants to return a string which is invalid to based on the expected type from the Eval.

When your data is sent to the querystring it IS a string. When you parse it off using the QueryString object it returns a string as well. Can you post your code that is parsing off the querystring?

If your code is not checking for an empty value or doing a TryParse then you could have errors. Keep in mind anyone can manipulate this data so you should be verifying that it is of the proper type.

Eg:

Dim yourValue As Double = 0
If Double.TryParse(Request.QueryString("id"), yourValue) = False Then
    ShowError("Invalid id supplied.")
    Return
End If
Kelsey
+3  A: 
<asp:TemplateField HeaderText="Download">
                <ItemTemplate>
               <asp:LinkButton ID="lnkname" runat="server" Text="Download" PostBackUrl='<%# String.Format("~/logout.aspx?ID={0}&category=mobile", Eval("ID")) %>'></asp:LinkButton>
               </ItemTemplate>
               </asp:TemplateField>
Curt
Thanks It works fine.....
Talha Bin Shakir
No problem. I think the issue was that the Eval() function doesn't output a string. You were trying to add a string and this value together causing an error. I think an alternative fix may have been to put Eval("ID").toString() but I think String.Format() looks a lot neater :)
Curt