Solved

Int32.Parse inputstring was not in a correct format

Posted on 2004-09-17
6
501 Views
Last Modified: 2012-05-05
Hi Experts;
I am developing an application using asp.net(VB) and sql database.
Trying to pass the Employee ID and get the Name at the runtime
Using the following function to do that.....
Sub GetName(ByVal Expr1 As Object)

        Dim strSQLDDL As String = "SELECT dbo_Carter_ColleagueDetails.Colleague_Surname, dbo.CarterCourses.Course_ID,CarterCoursesLogged.Logged_ID,CarterCoursesLogged.Colleague_CartNumber,[Colleague_Forename]+' '+[Colleague_Surname] as Coll_Name,CarterCourses.CourseName,CarterCoursesLogged.Course_Done,CarterCourses.ExpiaryDays,[Course_Done]+[ExpiaryDays] AS course_Expiary_Date,dbo_Carter_ColleagueDetails.Colleague_CartNumber  FROM (CarterCoursesLogged LEFT JOIN CarterCourses  ON CarterCoursesLogged.Course_ID = CarterCourses.Course_ID) LEFT JOIN dbo_Carter_ColleagueDetails ON CarterCoursesLogged.Colleague_CartNumber = dbo_Carter_ColleagueDetails.Colleague_CartNumber  WHERE dbo_Carter_ColleagueDetails.Colleague_CartNumber = @colleague_CN "

        Dim SQlCommand As New SQlCommand(strSQLDDL, mySqlConnection)
        SQlCommand.CommandType = CommandType.Text

         Dim Colleague_CN As String = Int32.Parse(Expr1)

        Dim parameterEventType_ID As SqlParameter = _
           New SqlParameter("@Colleague_CN", SqlDbType.NVarChar, 50)
        parameterEventType_ID.Value = Colleague_CN
        SQlCommand.Parameters.Add(parameterEventType_ID)

        Dim myDataAdapter As SqlDataAdapter = New _
        SqlDataAdapter(SQlCommand)
        myDataAdapter.Fill(DivisionDataSet, "ExpiaryDate")
        myDataAdapter.Update(DivisionDataSet, "ExpiaryDate")
        DivisionDataSet.AcceptChanges()
    End Sub
This is fine as far as i am passing a number, but my employee numbers has letters as well.
This is how i pass it at the runtime.............................
If e.Item.Cells(0).Text <> "" Then
            GetName(e.Item.Cells(0).Text.ToString.Trim)
            e.Item.Cells(0).Text = DivisionDataSet.Tables("ExpiaryDate").Rows(0)("Coll_Name")
            DivisionDataSet.Reset()
        End If
.............................
How do i get around this problem.I tried Tostring() and convert.Tostring as well but no joy
Can u please help
Tx
0
Comment
Question by:dnagahawatte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12084954
Hi,

if you have a letter in your GetName input parameter you can not parse with Int32.Parse
may if you post the reason for parsing the EE memebers can help you


Regards,
B..M
0
 

Author Comment

by:dnagahawatte
ID: 12085177
Ok...
I am developing a training matrix which has employee names courses they have done and expiary dates of those courses
I am getting these details from the databse using a PIVOT table with the help of a stored procedure which has only Employee numbers.
(Couldnt get it to work to get Names)
So i need to pass these numbers and get the Employee name at the  runtime using onitemdatabound function.
Well i need to get the relevent expiary dates as well later on...
So i am trying to pass the number which in the datagrid .................................

If e.Item.Cells(0).Text <> "" Then
            GetName(e.Item.Cells(0).Text.ToString.Trim)
            e.Item.Cells(0).Text = DivisionDataSet.Tables("ExpiaryDate").Rows(0)("Coll_Name")
            DivisionDataSet.Reset()
        End If

Here is the Getname function

Sub GetName(ByVal Expr1 As Object)

        Dim strSQLDDL As String = "SELECT dbo_Carter_ColleagueDetails.Colleague_Surname, dbo.CarterCourses.Course_ID,CarterCoursesLogged.Logged_ID,CarterCoursesLogged.Colleague_CartNumber,[Colleague_Forename]+' '+[Colleague_Surname] as Coll_Name,CarterCourses.CourseName,CarterCoursesLogged.Course_Done,CarterCourses.ExpiaryDays,[Course_Done]+[ExpiaryDays] AS course_Expiary_Date,dbo_Carter_ColleagueDetails.Colleague_CartNumber  FROM (CarterCoursesLogged LEFT JOIN CarterCourses  ON CarterCoursesLogged.Course_ID = CarterCourses.Course_ID) LEFT JOIN dbo_Carter_ColleagueDetails ON CarterCoursesLogged.Colleague_CartNumber = dbo_Carter_ColleagueDetails.Colleague_CartNumber  WHERE dbo_Carter_ColleagueDetails.Colleague_CartNumber = @colleague_CN "

        Dim SQlCommand As New SQlCommand(strSQLDDL, mySqlConnection)
        SQlCommand.CommandType = CommandType.Text

         Dim Colleague_CN As String = Int32.Parse(Expr1)

        Dim parameterEventType_ID As SqlParameter = _
           New SqlParameter("@Colleague_CN", SqlDbType.NVarChar, 50)
        parameterEventType_ID.Value = Colleague_CN
        SQlCommand.Parameters.Add(parameterEventType_ID)

        Dim myDataAdapter As SqlDataAdapter = New _
        SqlDataAdapter(SQlCommand)
        myDataAdapter.Fill(DivisionDataSet, "ExpiaryDate")
        myDataAdapter.Update(DivisionDataSet, "ExpiaryDate")
        DivisionDataSet.AcceptChanges()
    End Sub

So how can i pass a NVarchar to another function like i have written above.
How do i get around this problem....
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 12085244
first of all you can not ( except if vb allow this ) to set string to int variable
second
try this :

Dim Colleague_CN As String
if Not Expr1 = Nothing Then
 Colleague_CN= Expr1.ToString()
End If

if thethe Expr1 is null you can not parse it at all

B..M
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 8

Expert Comment

by:shovavnik
ID: 12086540
1. Unrelated, but may be posing other problems for you:
GetName(e.Item.Cells(0).Text.ToString.Trim)
should be
GetName(e.Item.Cells(0).Text.ToString().Trim())
ToString and Trim are methods, not properties, so they need the parentheses.

2. As mmarinov said, you can only use Convert.ToInt32 if the complete string can be converted to an integer.  It doesn't work like javascript, which tries to extract the number between the characters.  This means that your string cannot be null or empty, and that it must contain at least one digit, and that all digits must conform to the specifications for a number, so "1", "1e5", "2.4" are ok, but null, "", "a3", "3a" are not.

3. I think you should re-evaluate your design.  Retrieve the id in a separate column.  It should not be in the same column as the name.  That way, you always have the id, and you don't need to convert or extract the number.

4. If you do want to extract the number anyway, you can try something like:
if not Expr1 is nothing and expr1.ToString().Trim() <> String.Empty then
  dim index as Int32 = 0
  dim numString as String = String.Empty
  while index < Expr1.ToString().Length do
    try
      numString = numString & Convert.ToInt32(Expr1.ToString()(index)).ToString()
    catch
      ' ignore the exception
    end try
    index = index + 1
  wend
  if numString.Length > 0 then
end if
Dim Colleague_CN As String = Convert.ToInt32( numString ).ToString()

(By the way, Convert.ToInt32 and Int32.Parse() are functionally equivalent, though Microsoft recommends using the Convert functions.)
0
 

Author Comment

by:dnagahawatte
ID: 12086711
Thank you very much.
Its all make sence now
0
 

Author Comment

by:dnagahawatte
ID: 12086726
THANK U VERY MUCH BOTH.
BOTH ARE BEING REALLY HELPFUL
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question