简体   繁体   中英

How to save textbox data to file using FolderBrowsedialog

Am new to VB.NET. In the Below code when i compile it, Am getting an error when i check the Select Radiobutton and browse the folder and clikc the GENERATE HL7 Message am getting an error as " Error: Expression is a value and therefore cannot be the target of an assignment. " when i check the Default Radiobutton it works like a charm. But when i check the Select Radiobutton am getting the error. I don't know whats wrong in my code. you can find my design in the following URL: [URL= http://s1065.photobucket.com/user/Izaz_Ahmed/media/Capture_zpst4jjgvxb.jpg.html]

Private Sub HL_Click(sender As Object, e As EventArgs) Handles HL.Click

        If vld(TxtProcode) = False Then
            Exit Sub
        End If

        Dim file As System.IO.StreamWriter
        Dim folderBrowser As New FolderBrowserDialog
        Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd") & DateTime.Now.ToString("HHmmss") & ".HL7"
        Dim ts As String = DateTime.Now.ToString("yyyyMMdd") & DateTime.Now.ToString("HHmmss")
        'file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)
        folderBrowser.ShowNewFolderButton = True
        If RadioBtndefault.Checked Then
            TxtDob.Format = DateTimePickerFormat.Custom
            TxtDob.CustomFormat = "yyyyMMdd"
            TxtExamtime.Format = DateTimePickerFormat.Custom
            TxtExamtime.CustomFormat = "hhMMss"
            TxtExamdate.Format = DateTimePickerFormat.Custom
            TxtExamdate.CustomFormat = "yyyyMMdd"
            file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)
            file.WriteLine("MSH|^~\&|||||" & TxtExamdate.Text & "" & TxtExamtime.Text & "||ORM^O01||P|2.3.1")
            file.WriteLine("PID|||" & TxtId.Text & "||" & TxtFamilyname.Text & "^" & TxtGivenname.Text & "||" & TxtDob.Text & "||" & TxtGender.Text & "|||" & TxtStreet.Text & " " & TxtHouse.Text & "^^" & TxtCity.Text & "^^" & TxtPostcode.Text)
            file.WriteLine("PV1||O|||||||||||||||||" & TxtId.Text & "|||||||||||||||||||||||||" & ts)
            file.WriteLine("ORC|NW|" & ts & "|||||^^^S||" & TxtExamdate.Text)
            file.WriteLine("OBR||" & ts & "^" & ts & "||" & TxtProcode.Text & "|||" & TxtExamdate.Text & "" & TxtExamtime.Text & "|" & TxtExamdate.Text & "" & TxtExamtime.Text)
            file.WriteLine()
            file.Close()
        End If

        If RadioBtnselect.Checked Then
            If folderBrowser.ShowDialog() = DialogResult.OK Then
                file.WriteLine = folderBrowser.SelectedPath
                file.WriteLine("MSH|^~\&|||||" & TxtExamdate.Text & "" & TxtExamtime.Text & "||ORM^O01||P|2.3.1")
                file.WriteLine("PID|||" & TxtId.Text & "||" & TxtFamilyname.Text & "^" & TxtGivenname.Text & "||" & TxtDob.Text & "||" & TxtGender.Text & "|||" & TxtStreet.Text & " " & TxtHouse.Text & "^^" & TxtCity.Text & "^^" & TxtPostcode.Text)
                file.WriteLine("PV1||O|||||||||||||||||" & TxtId.Text & "|||||||||||||||||||||||||" & ts)
                file.WriteLine("ORC|NW|" & ts & "|||||^^^S||" & TxtExamdate.Text)
                file.WriteLine("OBR||" & ts & "^" & ts & "||" & TxtProcode.Text & "|||" & TxtExamdate.Text & "" & TxtExamtime.Text & "|" & TxtExamdate.Text & "" & TxtExamtime.Text)
                file.WriteLine()
                file.Close()
                Dim root As Environment.SpecialFolder = folderBrowser.RootFolder
            End If
        End If
End Class

Look at your second condition, RadioBtnselect statement. You never tell the StreamWriter the path, hence your error. You do give it a path in your first if but not in the second one.

For example this:

file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)

The exact error message is caused by this line

 file.WriteLine = folderBrowser.SelectedPath

WriteLine is a method not a property. The syntax shoud be WriteLine(....).

In any case your code will fail because the StreamWriter used in the Select case is not initalized correctly as you do in the first case.
You need something like this that combine the SelectedPath with your intended file name

If RadioBtnselect.Checked Then
    If folderBrowser.ShowDialog() = DialogResult.OK Then
        Dim destFile = Path.Combine(folderBrowser.SelectedPath,fileDateTime)
        file = My.Computer.FileSystem.OpenTextFileWriter(destFile,True)      
        file.WriteLine(.....)
        .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM