We help IT Professionals succeed at work.

Input string not in correct format....error in the code

Last Modified: 2010-05-18
In the folowing piece of code, I'm getting the following error,"Input String not in correct format"
Can anyone help me.I'm new to VB.NET AND UNABLE TO FIGURE OUT.
i've a tight deadline to meet.
Please help me.
Public Sub ShowFirstPage(ByVal s As System.Object, ByVal e As System.EventArgs)
    lblCurrentIndex.Text = "0"
  End Sub

  Public Sub ShowPreviousPage(ByVal s As System.Object, ByVal e As System.EventArgs)
    lblCurrentIndex.Text = Cstr(Cint(lblCurrentIndex.Text) - CInt(lblPageSize.Text))
    If CInt(lblCurrentIndex.Text) < 0 Then
      lblCurrentIndex.Text = "0"
    End If
  End Sub

  Public Sub ShowNextPage(ByVal s As System.Object, ByVal e As System.EventArgs)
    If CInt(CInt(lblCurrentIndex.Text) + CInt(lblPageSize.Text)) < CInt(lblRecordCount.Text) Then
      lblCurrentIndex.Text = CStr(CInt(lblCurrentIndex.Text) + CInt(lblPageSize.Text))
    End If
  End Sub

  Public Sub ShowLastPage(ByVal s As System.Object, ByVal e As System.EventArgs)
    Dim intMod as Integer
    intMod = CInt(lblRecordCount.Text) Mod CInt(lblPageSize.Text)
    If intMod > 0 Then
      lblCurrentIndex.Text = Cstr(CInt(lblRecordCount.Text) - intMod)
      lblCurrentIndex.Text = Cstr(CInt(lblRecordCount.Text) - CInt(lblPageSize.Text))
    End If
  End Sub

  Private Sub ShowCounts()
    lblCounts.Text = "|Total Rows: <b>" & lblRecordCount.Text
    lblCounts.Text += "</b> | Page:<b> "
    lblCounts.Text += CStr(CInt(CInt(lblCurrentIndex.Text) / CInt(lblPageSize.Text)+1))
    lblCounts.Text += "</b> of <b>"
    If (CInt(lblRecordCount.Text) Mod CInt(lblPageSize.Text)) > 0 Then
      lblCounts.Text += CStr(Fix(CInt(lblRecordCount.Text) / CInt(lblPageSize.Text)+1))
      lblCounts.Text += CStr(Fix(lblRecordCount.Text) / CInt(lblPageSize.Text))
    End If
    lblCounts.Text += "</b> |"
  End Sub
Watch Question

can you please provide a stack trace. most likely this is a data coversion error.


[FormatException: Input string was not in a correct format.]
   Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat)
   Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value)
   Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value)

[InvalidCastException: Cast from string "" to type 'Integer' is not valid.]
   Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value)
   Blast2.LossPromoSummaryReport.ShowNextPage(Object s, EventArgs e) in C:\vsProjects\Phase2\Blast2\LossPromoSummaryReport.aspx.vb:162
   System.Web.UI.HtmlControls.HtmlAnchor.OnServerClick(EventArgs e)
   System.Web.UI.HtmlControls.HtmlAnchor.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
see this from the stack trace Cast from string "" to type 'Integer'

you are trying to convert a blank "" string to an integer which is not valid and hence it raises an exception.

make sure whereever you are using CInt() that your code is not passing a blank value to that CInt function.

hope this helps.
Chief Technology Officer
Most Valuable Expert 2011
This one is on us!
(Get your first solution completely free - no credit card required)
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Since you are using this in multiple places, I would create a function to do this to save you having to do this all over the place or do alternatives of IF...THEN or TRY...CATCH statements to ensure that string is valid.  Doing it this way, you can also add more functionality like having a parameter "Optional ByVal defaultValue As Integer = 0" that you can use to determine what the integer value should be if want more flexibility than TryParse function's default to zero.  In that case you would use the Integer.Parse function with a Try...Catch block with intValue = defaultValue in the catch statement.  Otherwise, code below should work fine.
Public Function ToInteger(ByVal str As String) As Integer
     Dim intValue As Integer
     Integer.TryParse(str, intValue)
     Return intValue
End Function

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.