Hi, I'm working on a program and keep getting the error "conversion from string to integer is not valid" for one variable and "conversion from string to type double is not valid." I'm fairly new at vb.net and I thought if you converted it you'd be okay. However every time I hit my "Save" button I get these errors. Can anyone help please? Thanks! The errors occur on the last 2 items in the declarations area at about line 11 or 12.

Public Class CollectionForm
    
    Private Sub btnSaveData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveData.Click

        'Declare variables to hold the input data, get the input data
        Dim strRand As String = lblHomeID.Text
        Dim strDate As String = CStr((CDate((Me.datDate.Text))))
        Dim strCountystate As String = Me.cboCountyState.Text
        Dim strRace As String = Me.cboRace.Text
        Dim intNumberInHousehold As Integer = CInt(Me.txtNumHousehold.Text)
        Dim dblYearlyIncome As Double = CDbl(Me.txtYearlyIncome.Text)


        'Save the data to a txt file
        Dim SurveyFile As System.IO.StreamWriter
        Dim strFileName As String = "employees2.txt"


        'Open the output file as append
        SurveyFile = System.IO.File.AppendText(strFileName)


        'Format the data with commas to separate fields
        SurveyFile.Write(strRand)
        SurveyFile.Write(",")
        SurveyFile.Write(strDate)
        SurveyFile.Write(",")
        SurveyFile.Write(strCountyState)
        SurveyFile.Write(",")
        SurveyFile.Write(strRace)
        SurveyFile.Write(",")
        SurveyFile.Write(intNumberInHousehold)
        SurveyFile.Write(",")
        SurveyFile.WriteLine(dblYearlyIncome)

        
        'Close the file
        SurveyFile.Close()


        'Validate the inputsyou
        'Declare variables
        Dim intHousehold As Integer
        Dim dblIncome As Double
        Dim blnValidated As Boolean


        'Call the Validate_Inputs procedure
        Validate_Inputs(intHousehold, dblIncome, blnValidated)
        

    End Sub

Recommended Answers

All 3 Replies

Try Integer.Parse() or Integer.TryParse()

u declare first variable Dim strRand As String = lblHomeID.Text , i think the lblHomeID.text is giving the integer value and datatype of variable name strRand is string .
Regards
M.Waqas Aslam

If you are writing them out again as text strings why not just declare them as string on lines 10 & 11.

If you want to validate then try using the IsNumeric function, ie

if not isnumeric(dblYearlyIncome.text.tostring) then 
    msgbox "some error"
    exit sub
end if
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.