4 Replies - 512 Views - Last Post: 23 May 2016 - 08:43 AM Rate Topic: -----

#1 waseemak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-December 14

Input string was not in a correct format.

Posted 23 May 2016 - 08:15 AM

I'm working on a Microsoft TailspinspyWorks project. I'm writing code step by step provided by the document. But now i'm stuck in some error i don't know what to do. I applied every possible solutions but no positive result. I've uploaded the image of the code and error. And i know the error what it is but i've followed the steps given in document, even then i got this error. I also watch the video from microsoft sites of this error.. they use the same functionality i'm using but i can't go further.

Is This A Good Question/Topic? 0
  • +

Replies To: Input string was not in a correct format.

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13965
  • View blog
  • Posts: 55,749
  • Joined: 12-June 08

Re: Input string was not in a correct format.

Posted 23 May 2016 - 08:18 AM

Quote

I've uploaded the image of the code and error.

No you didn't.

Just copy/paste the error here and explain: what it is doing, what it should be doing, etc.
Was This Post Helpful? 0
  • +
  • -

#3 waseemak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-December 14

Re: Input string was not in a correct format.

Posted 23 May 2016 - 08:24 AM

This is the image of the code. Sorry got disconnected while uploading image.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13965
  • View blog
  • Posts: 55,749
  • Joined: 12-June 08

Re: Input string was not in a correct format.

Posted 23 May 2016 - 08:42 AM

You need to either convert your int to a string to compare against a string or convert your string to an int to compare against an int.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Input string was not in a correct format.

Posted 23 May 2016 - 08:43 AM

Seriously though, don't upload code screenshots, please do as Modi asked: paste your code here between code tags:

:code:

By the way, the error you're showing here is not the same error in your title. Which error are you having problems with, "Cannot implicitly convert type 'string' to 'int?'", or "Input string was not in a correct format".

Please be specific. We're not mind readers, and we're not being paid for this. Don't make us drag information out of you.

The error you're getting indicates that cartadd.CartId is an int? (which is a shortcut for the type Nullable<Int32>. The parameter you pass in cartId is a string. Strings and integers are not the same thing. 1 is not "1", nor vice versa. If you have a string and you need to make an integer out of it, you must Parse/TryParse.

Now, since you're dealing with a nullable int, here's a handy function you can use:

public static int? ParseNInt(string value)
{
	int result;
	if(!int.TryParse(value, out result))
		return null;
	return result;
}


This will return null if the incoming string fails to parse, otherwise it will return the parsed number.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1