Solved

Int32.Parse inputstring was not in a correct format

Posted on 2004-09-17
6
487 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
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Expert Comment

by:shovavnik
Comment Utility
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
Comment Utility
Thank you very much.
Its all make sence now
0
 

Author Comment

by:dnagahawatte
Comment Utility
THANK U VERY MUCH BOTH.
BOTH ARE BEING REALLY HELPFUL
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now