Solved

Int32.Parse inputstring was not in a correct format

Posted on 2004-09-17
6
498 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
insert value of checklistbox checked 4 39
Finding the IIS version 5 38
Header Font Size in Grid View 6 26
ASP.net Moving Visual Studio 2015 project to 2017 2 38
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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