9 Replies - 9027 Views - Last Post: 17 July 2012 - 07:45 AM Rate Topic: -----

#1 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 339
  • Joined: 10-November 09

ASP.NET C# input string was not in a correct format (query string)

Posted 11 July 2012 - 12:58 PM

Hey folks,
I'm in a bit of a bind.

I have a page that is to do a function on page load that requires an ID number (integer) passed by a querystring - but I get the error that says "Input string was not in a correct format".

My code to get the the value of the querystring and do the function is simple and straight forward.

int custID = Convert.ToInt32(Request.QueryString["CustomID"]);
RemoveItem(custID);



I've tried the following ways as well.

//1)
int custID = int.Parse(Request.QueryString["CustomID"]);

//2)
string custID = Request.QueryString["CustomID"];
int intCustID = Convert.ToInt32(custID);
RemoveItem(intCustID);

//3
int custID = 0;
int.TryParse(Request.QueryString["CustomID"], out custID);
RemoveItem(custID);  //this one only returned the value of 0.



I'm kind of at a loss. How do I convert the "CustomID" from the querystring into an integer?

This post has been edited by mb2000inc: 11 July 2012 - 12:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ASP.NET C# input string was not in a correct format (query string)

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 11 July 2012 - 06:59 PM

does your url look something like http:/domain/index.aspx?CustomID=123? If not can you post what your url looks like?
Was This Post Helpful? 1
  • +
  • -

#3 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 339
  • Joined: 10-November 09

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 12 July 2012 - 06:20 AM

View PostNakor, on 11 July 2012 - 09:59 PM, said:

does your url look something like http:/domain/index.aspx?CustomID=123? If not can you post what your url looks like?


http://www.domainname.com/page.aspx?CustomID=[insertIDhere]


However, If it's important, I'm getting the ID from a Grid Button (link button).
(in a separate function with GridItemEventArgs)

btnEdit.PostBackUrl = "~/directory/page.aspx?CustomID=" + (e.Item as GridDataItem)["CustomID"].Text;


Although, I don't see where that would be an issue since it's within a string - just need to convert that string to INT.
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 12 July 2012 - 07:41 PM

Are you sure the url that is getting created is what you are expecting? It does have a value after the CustomId= part?

I'm not sure which type of control you're using, but you might could try using a HyperLinkField instead of a link button. It might look something like:

<GridView ID='blah' runat='server'>
<Columns>
    <asp:HyperLinkField DataNavigateUrlFields="CustomID" DataNavigateUrlFormatString="~/page.aspx?CustomID={0}" DataTextField="CustomID" DataTextFormatString="View {0}" />



This would work on most asp.net databound controls that implement columns, such as the gridview or listview.

If you're using the Telerik RadGrid you might try using the GridHyperLinkColumn which looks like it would work about the same.

This post has been edited by Nakor: 12 July 2012 - 07:44 PM

Was This Post Helpful? 1
  • +
  • -

#5 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 339
  • Joined: 10-November 09

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 13 July 2012 - 02:45 PM

Hi... sorry it took me so long to get back to you.... been busy.

So, yes....it does give me a value.

Actually, I am using a Telerik RadGrid instead of a regular Gridview - I probably should have mentioned that before - not that it has any affect on the query string... it still spits out a value for the sring. I'm currently using the "GridButtonColumn".

I guess I can try to use the HyperlinkColumn, but again, I'm not sure it would change the outcome of the querystring.

So, how would one convert the string value to an int? (not repeating my previous tries. :wink: )
Was This Post Helpful? 0
  • +
  • -

#6 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 13 July 2012 - 09:04 PM

The ways you have it are what I would normally use, with the int.TryParse being my preferred choice. If the value in the querystring is just a number I'm not sure why it wouldn't be working. The errors seems to indicate that Request.QueryString["CustomID"] is not in the correct format and can't be converted into an integer.

You might put the querystring into a variable before attempting to parse it. Then debug it and check the value of the variable.

string temp = Request.QueryString["CustomID"];
int custID = 0;

if (int.TryParse(temp, out custID))
{
    // Do Something
}



Debugging that should give you an easy way to see what is being stored in the QueryString["CustomID"] value without having to dig down into the QueryString object. Make sure there's no leading or trailing spaces, although those should be appearing as %20 in your url if they are present.

This post has been edited by Nakor: 13 July 2012 - 09:07 PM

Was This Post Helpful? 0
  • +
  • -

#7 hemantwithu  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 31
  • Joined: 18-October 11

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 16 July 2012 - 04:24 AM

Hi ,


Try to modify your Code as given below
if(!String.IsNullOrEmpty(Request.QueryString["CustomID"]))
{
//1)
int custID = int.Parse(Request.QueryString["CustomID"]);

//2)
string custID = Request.QueryString["CustomID"];
int intCustID = Convert.ToInt32(custID);
RemoveItem(intCustID);

//3
int custID = 0;
int.TryParse(Request.QueryString["CustomID"], out custID);
RemoveItem(custID);  //this one only returned the value of 0.
} 

Was This Post Helpful? 0
  • +
  • -

#8 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 339
  • Joined: 10-November 09

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 16 July 2012 - 04:41 AM

First off... sorry about the "-1" that was purely accidental... I was looking at one thing and clicking another. (apologies)

Second... I will give that a shot.
Was This Post Helpful? 0
  • +
  • -

#9 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 16 July 2012 - 11:54 AM

Fixed the -1
Was This Post Helpful? 1
  • +
  • -

#10 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 339
  • Joined: 10-November 09

Re: ASP.NET C# input string was not in a correct format (query string)

Posted 17 July 2012 - 07:45 AM

View PostNakor, on 16 July 2012 - 02:54 PM, said:

Fixed the -1


Thanks, bud. I think we should have a message box that asks us if we are sure we want to give a -1 and confirm yes/no.
But that's just me. :wink:

Anywho, The suggestion that was given by "hemantwithu" worked.

Thanks for all your input, folks. I appreciate it greatly.

Except, I can't mark their response as the "Accepted Answer" - probably because of the -1 accident.

This post has been edited by mb2000inc: 17 July 2012 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1