Read txt File to a DataSet

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ruca

    Read txt File to a DataSet

    Hi,

    Can I read a .TXT File to a DataSet? How can I do that?
    I want to read his lines to a DropDownList. This lines are the names of
    employees that I export from an application that I have.
    I export them to a .txt file and I want to "work" with this employees in my
    aspx page. Can you help me?
    It's a little bit strange, but it's what I need. :)


    --

    Thank's (if you try to help me)
    Hope this help you (if I try to help you)
    ruca


  • Alvin Bruney [MVP]

    #2
    Re: Read txt File to a DataSet

    Can you possibly change the textfile to xml? The data set can suck this up
    pretty easily because it has built in support for this. Otherwise, you will
    need to parse the contents of the file and create new rows adding the
    contents to these rows. It's not going to be pretty this way but these are
    the options that you have.

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...

    "ruca" <ruuca@iol.pt > wrote in message
    news:eFD8r%23H$ DHA.2800@tk2msf tngp13.phx.gbl. ..[color=blue]
    > Hi,
    >
    > Can I read a .TXT File to a DataSet? How can I do that?
    > I want to read his lines to a DropDownList. This lines are the names of
    > employees that I export from an application that I have.
    > I export them to a .txt file and I want to "work" with this employees in[/color]
    my[color=blue]
    > aspx page. Can you help me?
    > It's a little bit strange, but it's what I need. :)
    >
    >
    > --
    >
    > Thank's (if you try to help me)
    > Hope this help you (if I try to help you)
    > ruca
    >
    >[/color]


    Comment

    • Suresh

      #3
      RE: Read txt File to a DataSet

      Yes you can.

      Is the text file comma delimited? If yes, you can use the .net OleDBObject to read the file as you would a database table

      If it isn't a comma delimeted file and even if it is you can use the .NET StreamReader object and manually build a datatable that you can add to a dataset

      Example of StreamReader object


      Reading a text file and generating a dataset out of it involves some overhead. Unless you plan to use the dataset for other things than just to bind to a dropdownlist I think it would be efficient to add the items to your dropdownlist as your reading/parsing the text file

      If you have a need to reuse this dropdownlist you can always cache the control

      HTH
      Suresh

      ----- ruca wrote: ----

      Hi

      Can I read a .TXT File to a DataSet? How can I do that
      I want to read his lines to a DropDownList. This lines are the names o
      employees that I export from an application that I have
      I export them to a .txt file and I want to "work" with this employees in m
      aspx page. Can you help me
      It's a little bit strange, but it's what I need. :


      -

      Thank's (if you try to help me
      Hope this help you (if I try to help you
      ruc



      Comment

      • Scott Mitchell [MVP]

        #4
        Re: Read txt File to a DataSet

        > Can I read a .TXT File to a DataSet? How can I do that?

        You could programmaticall y pick through the text file and add it to the
        DataSet, yes. But there's not a method in the DataSet class like,
        ReadTextFile().
        [color=blue]
        > I want to read his lines to a DropDownList.[/color]

        Perhaps an easier approach would be to just programmaticall y step through
        the lines of code and add it to the DropDownList's Items collection.
        Something like:

        StreamReader sr = File.OpenText(f ilepath);
        while (sr.Peek() >= 0)
        myDDL.Items.Add (new ListItem(sr.Rea dLine());
        sr.Close();


        Something like that ought to do the trick.

        Happy Programming!

        --

        Scott Mitchell
        mitchell@4guysf romrolla.com
        eSports News, Results, upcoming Matches & live Matches. Learn tricks and guides in the esports space. ✅ We cover CS:GO, Dota 2, LOL, Overwatch & PUBG. 




        * When you think ASP, think 4GuysFromRolla. com!




        Comment

        • ruca

          #5
          Re: Read txt File to a DataSet

          Can you give me an example? I don't uderstand quite well. I try to find
          OleDbObject type but I can't. Do you mean to define a variable like this:

          Dim obj as OleDbObject

          If you yes, well... I can't define it. :(


          --

          Thank's (if you try to help me)
          Hope this help you (if I try to help you)
          ruca

          "Suresh" <anonymous@disc ussions.microso ft.com> escreveu na mensagem
          news:AF7DE4D7-8F14-4B03-9F46-E1200D1EFC51@mi crosoft.com...[color=blue]
          > Yes you can.
          >
          > Is the text file comma delimited? If yes, you can use the .net[/color]
          OleDBObject to read the file as you would a database table.[color=blue]
          >
          > If it isn't a comma delimeted file and even if it is you can use the .NET[/color]
          StreamReader object and manually build a datatable that you can add to a
          dataset.[color=blue]
          >
          > Example of StreamReader object:
          > http://samples.gotdotnet.com/quickst.../Anagrams.aspx
          >
          > Reading a text file and generating a dataset out of it involves some[/color]
          overhead. Unless you plan to use the dataset for other things than just to
          bind to a dropdownlist I think it would be efficient to add the items to
          your dropdownlist as your reading/parsing the text file.[color=blue]
          >
          > If you have a need to reuse this dropdownlist you can always cache the[/color]
          control.[color=blue]
          >
          > HTH,
          > Suresh.
          >
          > ----- ruca wrote: -----
          >
          > Hi,
          >
          > Can I read a .TXT File to a DataSet? How can I do that?
          > I want to read his lines to a DropDownList. This lines are the names[/color]
          of[color=blue]
          > employees that I export from an application that I have.
          > I export them to a .txt file and I want to "work" with this employees[/color]
          in my[color=blue]
          > aspx page. Can you help me?
          > It's a little bit strange, but it's what I need. :)
          >
          >
          > --
          >
          > Thank's (if you try to help me)
          > Hope this help you (if I try to help you)
          > ruca
          >
          >
          >[/color]


          Comment

          • Cor

            #6
            Re: Read txt File to a DataSet

            Hi Ruca,

            Without the FMT=Delimited\ it is a row with vbcrlf

            I hope this helps?

            Cor

            Private Sub Form1_Load(ByVa l sender As Object, _
            ByVal e As System.EventArg s) Handles MyBase.Load
            Dim file As String = "Test2.txt"
            Dim path As String = "C:\Test1\"
            Dim ds As New DataSet
            Try
            Dim f As System.IO.File
            If f.Exists(path & file) Then
            Dim ConStr As String = _
            "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & _
            path & ";Extended Properties=""Te xt;HDR=No;FMT=D elimited\"""
            Dim conn As New OleDb.OleDbConn ection(ConStr)
            Dim da As New OleDb.OleDbData Adapter("Select * from " & _
            file, conn)
            da.Fill(ds, "TextFile")
            End If
            Catch ex As Exception
            MessageBox.Show (ex.ToString)
            End Try
            End Sub
            ///


            Comment

            • Paul Clement

              #7
              Re: Read txt File to a DataSet

              On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ruuca@iol.pt > wrote:

              ¤ Hi,
              ¤
              ¤ Can I read a .TXT File to a DataSet? How can I do that?
              ¤ I want to read his lines to a DropDownList. This lines are the names of
              ¤ employees that I export from an application that I have.
              ¤ I export them to a .txt file and I want to "work" with this employees in my
              ¤ aspx page. Can you help me?
              ¤ It's a little bit strange, but it's what I need. :)

              Could you post a couple of lines from your text file so we can see the format?


              Paul ~~~ pclement@amerit ech.net
              Microsoft MVP (Visual Basic)

              Comment

              • ruca

                #8
                Re: Read txt File to a DataSet

                It's quite easy:

                Name, Code, Password
                Name, Code, Password
                Name, Code, Password
                Name, Code, Password
                Name, Code, Password
                Name, Code, Password


                ruca


                "Paul Clement" <UseAdddressAtE ndofMessage@sws pectrum.com> escreveu na
                mensagem news:3ffs30pl9p ogs6s4cihiuv4hk d1pp0uk2r@4ax.c om...[color=blue]
                > On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ruuca@iol.pt > wrote:
                >
                > ¤ Hi,
                > ¤
                > ¤ Can I read a .TXT File to a DataSet? How can I do that?
                > ¤ I want to read his lines to a DropDownList. This lines are the names of
                > ¤ employees that I export from an application that I have.
                > ¤ I export them to a .txt file and I want to "work" with this employees in[/color]
                my[color=blue]
                > ¤ aspx page. Can you help me?
                > ¤ It's a little bit strange, but it's what I need. :)
                >
                > Could you post a couple of lines from your text file so we can see the[/color]
                format?[color=blue]
                >
                >
                > Paul ~~~ pclement@amerit ech.net
                > Microsoft MVP (Visual Basic)[/color]


                Comment

                • ruca

                  #9
                  Re: Read txt File to a DataSet

                  And What I want to appear in my dropdownlist it's only the Name



                  "ruca" <ruuca@iol.pt > escreveu na mensagem
                  news:Oz9W4dR$DH A.2056@TK2MSFTN GP11.phx.gbl...[color=blue]
                  > It's quite easy:
                  >
                  > Name, Code, Password
                  > Name, Code, Password
                  > Name, Code, Password
                  > Name, Code, Password
                  > Name, Code, Password
                  > Name, Code, Password
                  >
                  >
                  > ruca
                  >
                  >
                  > "Paul Clement" <UseAdddressAtE ndofMessage@sws pectrum.com> escreveu na
                  > mensagem news:3ffs30pl9p ogs6s4cihiuv4hk d1pp0uk2r@4ax.c om...[color=green]
                  > > On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ruuca@iol.pt > wrote:
                  > >
                  > > ¤ Hi,
                  > > ¤
                  > > ¤ Can I read a .TXT File to a DataSet? How can I do that?
                  > > ¤ I want to read his lines to a DropDownList. This lines are the names[/color][/color]
                  of[color=blue][color=green]
                  > > ¤ employees that I export from an application that I have.
                  > > ¤ I export them to a .txt file and I want to "work" with this employees[/color][/color]
                  in[color=blue]
                  > my[color=green]
                  > > ¤ aspx page. Can you help me?
                  > > ¤ It's a little bit strange, but it's what I need. :)
                  > >
                  > > Could you post a couple of lines from your text file so we can see the[/color]
                  > format?[color=green]
                  > >
                  > >
                  > > Paul ~~~ pclement@amerit ech.net
                  > > Microsoft MVP (Visual Basic)[/color]
                  >
                  >[/color]


                  Comment

                  • Paul Clement

                    #10
                    Re: Read txt File to a DataSet

                    On Fri, 27 Feb 2004 09:56:23 -0000, "ruca" <ruuca@iol.pt > wrote:

                    ¤ It's quite easy:
                    ¤
                    ¤ Name, Code, Password
                    ¤ Name, Code, Password
                    ¤ Name, Code, Password
                    ¤ Name, Code, Password
                    ¤ Name, Code, Password
                    ¤ Name, Code, Password
                    ¤

                    Here is how you would get it into a DataSet:

                    Dim TextConnectionS tring As String
                    TextConnectionS tring = "Provider=Micro soft.Jet.OLEDB. 4.0;" & _
                    "Data Source=" & "c:\TestDat a" & ";" & _
                    "Extended Properties=""Te xt;HDR=NO;"""
                    Dim TextConn As New System.Data.Ole Db.OleDbConnect ion(TextConnect ionString)
                    TextConn.Open()

                    Dim da As New System.Data.Ole Db.OleDbDataAda pter("Select * from test.csv", TextConn)

                    Dim ds As DataSet = New DataSet("CSVFil es")
                    da.Fill(ds, "TestFile")

                    '...
                    '...
                    '...

                    TextConn.Close( )


                    Paul ~~~ pclement@amerit ech.net
                    Microsoft MVP (Visual Basic)

                    Comment

                    • Alvin Bruney [MVP]

                      #11
                      Re: Read txt File to a DataSet

                      it's going to be messy, i'd rather just read the strings from the file and
                      add them to the dropdown

                      //setup a loop here

                      Dropdownlist.it ems.add(new listitem(passwo rdtext,password text));



                      if you insist on using a dataset it would go something like this

                      DataSet dsTemp = new DataSet();

                      DataTable Tables = new DataTable();

                      dsTemp.Tables.A dd(Tables);

                      dsTemp.Tables[0].Columns.Add("P assWord", System.Type.Get Type(
                      "System.Str ing" ) );

                      DataRow myRow = dsTemp.Tables[0].NewRow();

                      myRow[0] = "password read from file";

                      dsTemp.Tables[0].Rows.Add(myRow );

                      dropdownlist.da tatext = "PassWord";
                      dropdownlist.da tasource = dsTemp;
                      dropdownlist.da tabind();

                      you can see that this is really messy code.
                      --
                      Regards,
                      Alvin Bruney [ASP.NET MVP]
                      Got tidbits? Get it here...

                      "ruca" <ruuca@iol.pt > wrote in message
                      news:uvEWuiS$DH A.3120@TK2MSFTN GP11.phx.gbl...[color=blue]
                      > And What I want to appear in my dropdownlist it's only the Name
                      >
                      >
                      >
                      > "ruca" <ruuca@iol.pt > escreveu na mensagem
                      > news:Oz9W4dR$DH A.2056@TK2MSFTN GP11.phx.gbl...[color=green]
                      > > It's quite easy:
                      > >
                      > > Name, Code, Password
                      > > Name, Code, Password
                      > > Name, Code, Password
                      > > Name, Code, Password
                      > > Name, Code, Password
                      > > Name, Code, Password
                      > >
                      > >
                      > > ruca
                      > >
                      > >
                      > > "Paul Clement" <UseAdddressAtE ndofMessage@sws pectrum.com> escreveu na
                      > > mensagem news:3ffs30pl9p ogs6s4cihiuv4hk d1pp0uk2r@4ax.c om...[color=darkred]
                      > > > On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ruuca@iol.pt > wrote:
                      > > >
                      > > > ¤ Hi,
                      > > > ¤
                      > > > ¤ Can I read a .TXT File to a DataSet? How can I do that?
                      > > > ¤ I want to read his lines to a DropDownList. This lines are the names[/color][/color]
                      > of[color=green][color=darkred]
                      > > > ¤ employees that I export from an application that I have.
                      > > > ¤ I export them to a .txt file and I want to "work" with this[/color][/color][/color]
                      employees[color=blue]
                      > in[color=green]
                      > > my[color=darkred]
                      > > > ¤ aspx page. Can you help me?
                      > > > ¤ It's a little bit strange, but it's what I need. :)
                      > > >
                      > > > Could you post a couple of lines from your text file so we can see the[/color]
                      > > format?[color=darkred]
                      > > >
                      > > >
                      > > > Paul ~~~ pclement@amerit ech.net
                      > > > Microsoft MVP (Visual Basic)[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • ruca

                        #12
                        Re: Read txt File to a DataSet

                        My dropdownlist it remains empty




                        "Paul Clement" <UseAdddressAtE ndofMessage@sws pectrum.com> escreveu na
                        mensagem news:2nnu30he9k viors463ln33kdu ptsceua0t@4ax.c om...[color=blue]
                        > On Fri, 27 Feb 2004 09:56:23 -0000, "ruca" <ruuca@iol.pt > wrote:
                        >
                        > ¤ It's quite easy:
                        > ¤
                        > ¤ Name, Code, Password
                        > ¤ Name, Code, Password
                        > ¤ Name, Code, Password
                        > ¤ Name, Code, Password
                        > ¤ Name, Code, Password
                        > ¤ Name, Code, Password
                        > ¤
                        >
                        > Here is how you would get it into a DataSet:
                        >
                        > Dim TextConnectionS tring As String
                        > TextConnectionS tring = "Provider=Micro soft.Jet.OLEDB. 4.0;" & _
                        > "Data Source=" & "c:\TestDat a" & ";" & _
                        > "Extended Properties=""Te xt;HDR=NO;"""
                        > Dim TextConn As New[/color]
                        System.Data.Ole Db.OleDbConnect ion(TextConnect ionString)[color=blue]
                        > TextConn.Open()
                        >
                        > Dim da As New System.Data.Ole Db.OleDbDataAda pter("Select * from[/color]
                        test.csv", TextConn)[color=blue]
                        >
                        > Dim ds As DataSet = New DataSet("CSVFil es")
                        > da.Fill(ds, "TestFile")
                        >
                        > '...
                        > '...
                        > '...
                        >
                        > TextConn.Close( )
                        >
                        >
                        > Paul ~~~ pclement@amerit ech.net
                        > Microsoft MVP (Visual Basic)[/color]


                        Comment

                        • Cor

                          #13
                          Re: Read txt File to a DataSet

                          Hi Alvin,

                          I can asure you that my sample is working.
                          (There was originaly a datagrid in it, which could be now a combobox at the
                          end)

                          But that is so basic.

                          Cor


                          Comment

                          • Cor

                            #14
                            Re: Read txt File to a DataSet

                            Hi Paul,

                            I can asure you that my sample is working.
                            (There was originaly a datagrid in it, which could be now a combobox at the
                            end)

                            But that is so basic.

                            Cor



                            Comment

                            • ruca

                              #15
                              Re: Read txt File to a DataSet

                              dtTxt = New DataTable("Fnc" )

                              dcTxt = New DataColumn("Nom e", System.Type.Get Type("System.St ring"))

                              dcTxt = New DataColumn("Pin ", System.Type.Get Type("System.St ring"))

                              dtTxt.Columns.A dd("Nome")

                              dtTxt.Columns.A dd("Pin")



                              dsTxt.Tables("F nc").Rows.Add(d rTxt)

                              usertxt.DataTex tField = "Nome"

                              usertxt.DataSou rce = dsTxt

                              usertxt.DataBin d()

                              usertxt is my dropdownlist and it remains empty


                              "Alvin Bruney [MVP]" <vapor at steaming post office> escreveu na mensagem
                              news:O0#r9aU$DH A.2180@TK2MSFTN GP09.phx.gbl...[color=blue]
                              > it's going to be messy, i'd rather just read the strings from the file and
                              > add them to the dropdown
                              >
                              > //setup a loop here
                              >
                              > Dropdownlist.it ems.add(new listitem(passwo rdtext,password text));
                              >
                              >
                              >
                              > if you insist on using a dataset it would go something like this
                              >
                              > DataSet dsTemp = new DataSet();
                              >
                              > DataTable Tables = new DataTable();
                              >
                              > dsTemp.Tables.A dd(Tables);
                              >
                              > dsTemp.Tables[0].Columns.Add("P assWord", System.Type.Get Type(
                              > "System.Str ing" ) );
                              >
                              > DataRow myRow = dsTemp.Tables[0].NewRow();
                              >
                              > myRow[0] = "password read from file";
                              >
                              > dsTemp.Tables[0].Rows.Add(myRow );
                              >
                              > dropdownlist.da tatext = "PassWord";
                              > dropdownlist.da tasource = dsTemp;
                              > dropdownlist.da tabind();
                              >
                              > you can see that this is really messy code.
                              > --
                              > Regards,
                              > Alvin Bruney [ASP.NET MVP]
                              > Got tidbits? Get it here...
                              > http://tinyurl.com/3he3b
                              > "ruca" <ruuca@iol.pt > wrote in message
                              > news:uvEWuiS$DH A.3120@TK2MSFTN GP11.phx.gbl...[color=green]
                              > > And What I want to appear in my dropdownlist it's only the Name
                              > >
                              > >
                              > >
                              > > "ruca" <ruuca@iol.pt > escreveu na mensagem
                              > > news:Oz9W4dR$DH A.2056@TK2MSFTN GP11.phx.gbl...[color=darkred]
                              > > > It's quite easy:
                              > > >
                              > > > Name, Code, Password
                              > > > Name, Code, Password
                              > > > Name, Code, Password
                              > > > Name, Code, Password
                              > > > Name, Code, Password
                              > > > Name, Code, Password
                              > > >
                              > > >
                              > > > ruca
                              > > >
                              > > >
                              > > > "Paul Clement" <UseAdddressAtE ndofMessage@sws pectrum.com> escreveu na
                              > > > mensagem news:3ffs30pl9p ogs6s4cihiuv4hk d1pp0uk2r@4ax.c om...
                              > > > > On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ruuca@iol.pt > wrote:
                              > > > >
                              > > > > ¤ Hi,
                              > > > > ¤
                              > > > > ¤ Can I read a .TXT File to a DataSet? How can I do that?
                              > > > > ¤ I want to read his lines to a DropDownList. This lines are the[/color][/color][/color]
                              names[color=blue][color=green]
                              > > of[color=darkred]
                              > > > > ¤ employees that I export from an application that I have.
                              > > > > ¤ I export them to a .txt file and I want to "work" with this[/color][/color]
                              > employees[color=green]
                              > > in[color=darkred]
                              > > > my
                              > > > > ¤ aspx page. Can you help me?
                              > > > > ¤ It's a little bit strange, but it's what I need. :)
                              > > > >
                              > > > > Could you post a couple of lines from your text file so we can see[/color][/color][/color]
                              the[color=blue][color=green][color=darkred]
                              > > > format?
                              > > > >
                              > > > >
                              > > > > Paul ~~~ pclement@amerit ech.net
                              > > > > Microsoft MVP (Visual Basic)
                              > > >
                              > > >[/color]
                              > >
                              > >[/color]
                              >
                              >[/color]


                              Comment

                              Working...