Solved

Int32.Parse inputstring was not in a correct format

Posted on 2004-09-17
6
490 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquey and footrable 2 33
Authentication of Web Services 3 46
Entity Framework 7 27
Javascript alert is skipped before a Response.Redirect is executed in vb.net asp.net 9 23
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

7 Experts available now in Live!

Get 1:1 Help Now