1,105,344 Community Members

Save Access Db to txt file

Member Avatar
ke3r4
Newbie Poster
5 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello Every one
i'm a newbie in vb.net and i am facing a little problem i hope that someone can help me.
i made a little programme that i need at work witch have 15 textbox and in each textbox we will enter
a data that will be saved in an access db. till now i made it throught and everything workjs fine
the problem is that i want to make a button that will save the saved data in the db to a txt file
i found a little code that will open a dialog to chose the path and the name of the txt file but i can't make it to import the data from the db
PS: i need to save the data of each line in the Db to a single line in the txt file without space between the data.

i'm going to show you the code of the edit buttom to see the db info and the code of the botton witch will save the data to txt file and i hope someone can tell me what do i have to do.

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'check for the selected item in list
        If Me.dgvData.Rows.Count > 0 Then
            If Me.dgvData.SelectedRows.Count > 0 Then
                Dim intStdID As Integer = Me.dgvData.SelectedRows(0).Cells("id").Value
                'get data from database followed by student id
                'open connection
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If
                'get data into datatable
                Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM student " & _
                                                     " WHERE stdid=" & intStdID, cnn)
                Dim dt As New DataTable
                da.Fill(dt)

                Me.txtstdID.Text = intStdID
                Me.txtStdName.Text = dt.Rows(0).Item("stdname")
                Me.cboGender.Text = dt.Rows(0).Item("gender")
                Me.txtPhone.Text = dt.Rows(0).Item("phone")
                Me.txtAddress.Text = dt.Rows(0).Item("address")
                Me.TextBox1.Text = dt.Rows(0).Item("NAS")
                Me.TextBox2.Text = dt.Rows(0).Item("MGF")
                Me.TextBox3.Text = dt.Rows(0).Item("DF")
                Me.TextBox4.Text = dt.Rows(0).Item("DD")
                Me.TextBox5.Text = dt.Rows(0).Item("Dfin")
                Me.TextBox6.Text = dt.Rows(0).Item("DR")
                Me.TextBox7.Text = dt.Rows(0).Item("MHT")
                Me.TextBox8.Text = dt.Rows(0).Item("AF")
                Me.TextBox10.Text = dt.Rows(0).Item("TTVA")
                Me.TextBox9.Text = dt.Rows(0).Item("Timbre")
                '
                'hide the id to be edited in TAG of txtstdid in case id is changed
                Me.txtstdID.Tag = intStdID
                'change button add to update
                Me.btnAdd.Text = "Update"
                'disable button edit
                Me.btnEdit.Enabled = False
                'close connection
                cnn.Close()
            End If
        End If
    End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim filewriter As StreamWriter
        Dim results As DialogResult
        Dim SaveFileDialog1 As New SaveFileDialog()
        Dim a As String
        Dim intStdID As Integer = Me.dgvData.SelectedRows(0).Cells("id").Value
        'get data from database followed by student id
        'open connection
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If
        'get data into datatable
        Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM student " & _
                                             " WHERE stdid=" & intStdID, cnn)
        Dim dt As New DataTable
        da.Fill(dt)


        a = (Me.txtstdID.Text = intStdID & Me.txtStdName.Text = dt.Rows(0).Item("stdname") & Me.cboGender.Text = dt.Rows(0).Item("gender") & Me.txtPhone.Text = dt.Rows(0).Item("phone") & Me.txtAddress.Text = dt.Rows(0).Item("address") & Me.TextBox1.Text = dt.Rows(0).Item("NAS") & Me.TextBox2.Text = dt.Rows(0).Item("MGF") & Me.TextBox3.Text = dt.Rows(0).Item("DF") & Me.TextBox4.Text = dt.Rows(0).Item("DD") & Me.TextBox5.Text = dt.Rows(0).Item("Dfin") & Me.TextBox6.Text = dt.Rows(0).Item("DR") & Me.TextBox7.Text = dt.Rows(0).Item("MHT") & Me.TextBox8.Text = dt.Rows(0).Item("AF") & Me.TextBox10.Text = dt.Rows(0).Item("TTVA") & Me.TextBox9.Text = dt.Rows(0).Item("Timbre"))

        results = SaveFileDialog1.ShowDialog

        If results = DialogResult.OK Then

            filewriter = New StreamWriter(SaveFileDialog1.FileName, False)

            filewriter.Write(a)

            filewriter.Close()

        End If
    End Sub

i know i may made a stupid mistake the thing is that i have no idea on coding with vb.net i just looked for random codes and tryed to modifie them to have what i need.

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

You can use Linq to Dataset:

Sample:

Dim arr() = dt.AsEnumerable().Select(Function(p)
                      Return New String(p("stdname") & "" & p("gender"))
                                    End Function).ToArray()

System.IO.File.WriteAllLines("c:\csnet\p.txt", arr)
Member Avatar
ke3r4
Newbie Poster
5 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i used your sample but i got a few errors

Error   1   Expression expected.    C:\Users\Maselec\Desktop\VB-Access\VB-Access\Form1.vb   202 56  VB-Access
Error   2   'Return' statement in a Sub or a Set cannot return a value. C:\Users\Maselec\Desktop\VB-Access\VB-Access\Form1.vb   203 9   VB-Access
Error   3   'End Sub' expected. C:\Users\Maselec\Desktop\VB-Access\VB-Access\Form1.vb   204 37  VB-Access
Error   4   End of statement expected.  C:\Users\Maselec\Desktop\VB-Access\VB-Access\Form1.vb   204 49  VB-Access
Error   5   Declaration expected.   C:\Users\Maselec\Desktop\VB-Access\VB-Access\Form1.vb   207 1   VB-Access
Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

PS: i need to save the data of each line in the Db to a single line in the txt file without space between the data.

I don't understand why you do not what a delimiter between the data as it will be nearly impossible to parse later. But you can always change your mind.

Perhaps a more old fashioned approach will be easier for you to understand.

  Dim lines As New List(Of String)
  For Each row As DataRow In dt.Rows
     Dim line As String = String.Empty
     For Each o As Object In row.ItemArray
        line &= o.ToString
     Next
     lines.Add(line)
  Next
  System.IO.File.WriteAllLines("c:\csnet\p.txt", lines.ToArray)
Member Avatar
ke3r4
Newbie Poster
5 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thnks for the help guys but i found out a half solution
instead of recording from the db and then saving into the txt file
now i'm trying to paste the contenent of the datagridview into a txt file
i made it so far but the pb am facing now is that after clicking the saving buttong the
txt file contain only the table's titles.
i'll show you the code i wrote and what i get in the txt file.

Dim numCols As Integer = dgvData.ColumnCount
        Dim numRows As Integer = dgvData.RowCount - 1

        Dim tw As StreamWriter
        Dim results As DialogResult
        Dim SaveFileDialog1 As New SaveFileDialog()
        'writing the header
        results = SaveFileDialog1.ShowDialog
        If results = DialogResult.OK Then
            tw = New StreamWriter(SaveFileDialog1.FileName, False)
            For count As Integer = 0 To numCols - 1
                tw.Write(dgvData.Columns(count).HeaderText)
                If (count <> numCols - 1) Then
                    tw.Write(", ")
                End If

            Next
        tw.WriteLine()
        For count As Integer = 0 To numRows - 1
            For count2 As Integer = 0 To numCols - 1
                tw.Write(dgvData.Rows(count).Cells(count2).Value)
                If (count2 <> numCols) Then
                    tw.Write(", ")
                End If
            Next
            tw.WriteLine()
        Next
            tw.Close()
        End If
    End Sub

after executing the program i only get

ID, Name, Gender, Phone, Address

but i don't get the contenent of each column
and i'm wandering how can i remove the space between the data saved in the txt file

thank you very much

Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
0
 

The space part is easy, remove if from tw.Write(", ").
As for the actual data, have you stepped through your code to see that the loop handling it works as planned?
A couple things I see wrong are:
Dim numRows As Integer = dgvData.RowCount - 1
For count As Integer = 0 To numRows - 1
you are deducting 1 twice when handling rows. If this is a datagridview with 1 row, then it won't fire.
Also
For count2 As Integer = 0 To numCols - 1
If (count2 <> numCols) Then
The if will never return false, as by definition count2 will never equal to number of columns (since you are limiting this in the for statement) .

Member Avatar
ke3r4
Newbie Poster
5 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

and the solution is ?

Member Avatar
ke3r4
Newbie Poster
5 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you very much :D i figured out
it was the second -1 now everything works fine

Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
0
 

Please mark this thread as solved then.
Thanks.

Question Answered as of 1 Year Ago by adam_k, TnTinMN and __avd
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article