-4
if (!IsPostBack)
{
    string EnquiryID = (Session["Enq_ID"].ToString());

    if (EnquiryID != null)
    {
        int Enquiry = Convert.ToInt32(EnquiryID);

        hotelOBJ.FillbyQueryEnquiry1(Enquiry, txtClientph, txtClientAddress );
    }
}

there is my code my session is not convert into integer the error is

"Input string was not in a correct format. "

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Shiv
  • 77
  • 2
  • 15
  • 1
    Did you use Int32.TryParse. – cheedep May 30 '13 at 04:37
  • 2
    You usually see that error if there are characters in the string that can't be converted to int. – JaySilk84 May 30 '13 at 04:39
  • another try this Convert.Int32... – j.s.banger May 30 '13 at 04:44
  • http://stackoverflow.com/questions/9372210/int-parse-input-string-was-not-in-a-correct-format http://stackoverflow.com/questions/8321514/input-string-was-not-in-a-correct-format http://stackoverflow.com/questions/4833555/c-sharp-input-string-was-not-in-a-correct-format – Anthony Pegram May 30 '13 at 04:59
  • It is a duplicate of those, but with the added twist that he'll get NullReferenceException if the query string parameter is missing because he's checking if it's null at the wrong spot. – mason May 30 '13 at 05:05

3 Answers3

3

The error says that there might be some characters that can't be converted to Integer in any case like 1234ab contains characters ab which can't be converted to Integer.

What you can do is:

bool result = Int32.TryParse(Session["Enq_ID"].ToString(), out number);

if (result)
{
    hotelOBJ.FillbyQueryEnquiry1(number, txtClientph, txtClientAddress );
}
else
{
    Console.WriteLine("Attempted conversion of '{0}' failed.",
                       Session["Enq_ID"].ToString());
}
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0

Use Int32.Parse(). Beware FormatException when parsing, its good to use TryParse first or wrap TryParse in an extension method. Also change your if statement. The way you have it now can result in NullReferenceException if your query string parameter is missing.

if (!IsPostBack)
{
 if (Session["Enq_ID"] != null)
 {
 string EnquiryID = Session["Enq_ID"].ToString();
 if (EnquiryID.IsValidInt32())
   {
   int Enquiry =Int32.Parse(EnquiryID);
   hotelOBJ.FillbyQueryEnquiry1(Enquiry, txtClientph, txtClientAddress );
   }
 }
}

Extension method...

 public static bool IsValidInt32(this string value)
 {
     int result;
     return int.TryParse(value, out result);
 }
mason
  • 31,774
  • 10
  • 77
  • 121
  • `int.TryParse` returns a bool not an int, you need to instead return the `result`. – Shaun Wilde May 30 '13 at 05:16
  • Fixed. I copied the extension method off the net and assumed it was like the one in my private utilities library. Similar, but slightly different. – mason May 30 '13 at 05:19
0

I think the string that you try to convert to int is empty or it contains characters that is not digits (basically your string does not represent an integer value in a string form). That's why you get that error message.

So at least you have to replace your

if (EnquiryID != null) 

with

if(!string.IsNullOrWhiteSpace(EnquiryID))

That's how you will know what if you try to convert variable it at least have something to convert.

And or use Int32.TryParse() function to test (and convert to integer) if the string that you try to convert is an integer.

Georgii Gonchar
  • 436
  • 2
  • 4
  • This code still doesn't catch non-digit characters. – mason May 30 '13 at 05:01
  • @msm8bball so you think Int32.TryParse() won't help you to catch the fact that string contains non digit characters? – Georgii Gonchar May 30 '13 at 05:04
  • No, I edited the comment to say your code wouldn't catch it. Clearly Int32.TryParse() will catch it. – mason May 30 '13 at 05:07
  • Well, if you have a look at last sentence in my answer i clearly stated that you have to use Int32.TryParse() to test if string convertable into integer. :) – Georgii Gonchar May 30 '13 at 05:14
  • Yes, I read that. That's not what I was referring to. The point I'm trying to make is that calling IsNullOrWhitespace alone will not catch everything, therefore you must use TryParse(). And since you must use TryParse() there's little point in using IsNullOrWhitespace(). – mason May 30 '13 at 05:24
  • Well, the reason i mentioned string.IsNullOrWhitespace() function is that i prefer to know that i have nothing to convert before i go any further and call more "expensive" function. As usually in the context of the question you will probably have missed values more often that non integer values. But yes, you have to test the values to be integer in any way. – Georgii Gonchar May 30 '13 at 05:31