Solved

Int32.Parse inputstring was not in a correct format

Posted on 2004-09-17
6
494 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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