Read txt File to a DataSet

Discussion in 'ASP .Net Web Controls' started by ruca, Feb 26, 2004.

  1. ruca

    ruca Guest

    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
     
    ruca, Feb 26, 2004
    #1
    1. Advertising

  2. 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...
    http://tinyurl.com/3he3b
    "ruca" <> wrote in message
    news:eFD8r%23H$...
    > 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], Feb 26, 2004
    #2
    1. Advertising

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

    You could programmatically pick through the text file and add it to the
    DataSet, yes. But there's not a method in the DataSet class like,
    ReadTextFile().

    > I want to read his lines to a DropDownList.


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

    StreamReader sr = File.OpenText(filepath);
    while (sr.Peek() >= 0)
    myDDL.Items.Add(new ListItem(sr.ReadLine());
    sr.Close();


    Something like that ought to do the trick.

    Happy Programming!

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Feb 26, 2004
    #3
  4. ruca

    Cor Guest

    Hi Ruca,

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

    I hope this helps?

    Cor

    Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) 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=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
    Dim conn As New OleDb.OleDbConnection(ConStr)
    Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
    file, conn)
    da.Fill(ds, "TextFile")
    End If
    Catch ex As Exception
    MessageBox.Show(ex.ToString)
    End Try
    End Sub
    ///
     
    Cor, Feb 26, 2004
    #4
  5. ruca

    Paul Clement Guest

    On Thu, 26 Feb 2004 15:49:47 -0000, "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 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 ~~~
    Microsoft MVP (Visual Basic)
     
    Paul Clement, Feb 26, 2004
    #5
  6. ruca

    ruca Guest

    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" <> escreveu na
    mensagem news:...
    > On Thu, 26 Feb 2004 15:49:47 -0000, "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 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 ~~~
    > Microsoft MVP (Visual Basic)
     
    ruca, Feb 27, 2004
    #6
  7. ruca

    ruca Guest

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



    "ruca" <> escreveu na mensagem
    news:Oz9W4dR$...
    > 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" <> escreveu na
    > mensagem news:...
    > > On Thu, 26 Feb 2004 15:49:47 -0000, "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

    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 ~~~
    > > Microsoft MVP (Visual Basic)

    >
    >
     
    ruca, Feb 27, 2004
    #7
  8. ruca

    Paul Clement Guest

    On Fri, 27 Feb 2004 09:56:23 -0000, "ruca" <> 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 TextConnectionString As String
    TextConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & "c:\TestData" & ";" & _
    "Extended Properties=""Text;HDR=NO;"""
    Dim TextConn As New System.Data.OleDb.OleDbConnection(TextConnectionString)
    TextConn.Open()

    Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from test.csv", TextConn)

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

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

    TextConn.Close()


    Paul ~~~
    Microsoft MVP (Visual Basic)
     
    Paul Clement, Feb 27, 2004
    #8
  9. 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.items.add(new listitem(passwordtext,passwordtext));



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

    DataSet dsTemp = new DataSet();

    DataTable Tables = new DataTable();

    dsTemp.Tables.Add(Tables);

    dsTemp.Tables[0].Columns.Add("PassWord", System.Type.GetType(
    "System.String" ) );

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

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

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

    dropdownlist.datatext = "PassWord";
    dropdownlist.datasource = dsTemp;
    dropdownlist.databind();

    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" <> wrote in message
    news:uvEWuiS$...
    > And What I want to appear in my dropdownlist it's only the Name
    >
    >
    >
    > "ruca" <> escreveu na mensagem
    > news:Oz9W4dR$...
    > > 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" <> escreveu na
    > > mensagem news:...
    > > > On Thu, 26 Feb 2004 15:49:47 -0000, "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

    > 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 ~~~
    > > > Microsoft MVP (Visual Basic)

    > >
    > >

    >
    >
     
    Alvin Bruney [MVP], Feb 27, 2004
    #9
  10. ruca

    Cor Guest

    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
     
    Cor, Feb 27, 2004
    #10
  11. ruca

    Cor Guest

    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
     
    Cor, Feb 27, 2004
    #11
  12. ruca

    ruca Guest

    My dropdownlist it remains empty




    "Paul Clement" <> escreveu na
    mensagem news:...
    > On Fri, 27 Feb 2004 09:56:23 -0000, "ruca" <> 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 TextConnectionString As String
    > TextConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    > "Data Source=" & "c:\TestData" & ";" & _
    > "Extended Properties=""Text;HDR=NO;"""
    > Dim TextConn As New

    System.Data.OleDb.OleDbConnection(TextConnectionString)
    > TextConn.Open()
    >
    > Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from

    test.csv", TextConn)
    >
    > Dim ds As DataSet = New DataSet("CSVFiles")
    > da.Fill(ds, "TestFile")
    >
    > '...
    > '...
    > '...
    >
    > TextConn.Close()
    >
    >
    > Paul ~~~
    > Microsoft MVP (Visual Basic)
     
    ruca, Feb 27, 2004
    #12
  13. ruca

    ruca Guest

    dtTxt = New DataTable("Fnc")

    dcTxt = New DataColumn("Nome", System.Type.GetType("System.String"))

    dcTxt = New DataColumn("Pin", System.Type.GetType("System.String"))

    dtTxt.Columns.Add("Nome")

    dtTxt.Columns.Add("Pin")



    dsTxt.Tables("Fnc").Rows.Add(drTxt)

    usertxt.DataTextField = "Nome"

    usertxt.DataSource = dsTxt

    usertxt.DataBind()

    usertxt is my dropdownlist and it remains empty


    "Alvin Bruney [MVP]" <vapor at steaming post office> escreveu na mensagem
    news:O0#r9aU$...
    > 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.items.add(new listitem(passwordtext,passwordtext));
    >
    >
    >
    > if you insist on using a dataset it would go something like this
    >
    > DataSet dsTemp = new DataSet();
    >
    > DataTable Tables = new DataTable();
    >
    > dsTemp.Tables.Add(Tables);
    >
    > dsTemp.Tables[0].Columns.Add("PassWord", System.Type.GetType(
    > "System.String" ) );
    >
    > DataRow myRow = dsTemp.Tables[0].NewRow();
    >
    > myRow[0] = "password read from file";
    >
    > dsTemp.Tables[0].Rows.Add(myRow);
    >
    > dropdownlist.datatext = "PassWord";
    > dropdownlist.datasource = dsTemp;
    > dropdownlist.databind();
    >
    > 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" <> wrote in message
    > news:uvEWuiS$...
    > > And What I want to appear in my dropdownlist it's only the Name
    > >
    > >
    > >
    > > "ruca" <> escreveu na mensagem
    > > news:Oz9W4dR$...
    > > > 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" <> escreveu na
    > > > mensagem news:...
    > > > > On Thu, 26 Feb 2004 15:49:47 -0000, "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
    > > 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 ~~~
    > > > > Microsoft MVP (Visual Basic)
    > > >
    > > >

    > >
    > >

    >
    >
     
    ruca, Feb 27, 2004
    #13
  14. ruca

    Paul Clement Guest

    On Fri, 27 Feb 2004 15:41:01 -0000, "ruca" <> wrote:

    ¤ My dropdownlist it remains empty
    ¤

    You have to add the data from the DataSet. ;-)

    ¤ > Here is how you would get it into a DataSet:
    ¤ >
    ¤ > Dim TextConnectionString As String
    ¤ > TextConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    ¤ > "Data Source=" & "c:\TestData" & ";" & _
    ¤ > "Extended Properties=""Text;HDR=NO;"""
    ¤ > Dim TextConn As New
    ¤ System.Data.OleDb.OleDbConnection(TextConnectionString)
    ¤ > TextConn.Open()
    ¤ >
    ¤ > Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from
    ¤ test.csv", TextConn)
    ¤ >
    ¤ > Dim ds As DataSet = New DataSet("CSVFiles")
    ¤ > da.Fill(ds, "TestFile")
    ¤ >

    Dim dt As New DataTable
    dt = ds.Tables("TestFile")

    Dim RowCount As Int32
    For RowCount = 0 To dt.Rows.Count - 1
    ComboBox1.Items.Add((dt.Rows(RowCount)("F1").ToString))
    Next RowCount

    ¤ >
    ¤ > TextConn.Close()

    If your text file does not have a header the column names are F1, F2, F3, etc. In this example I am
    assuming that Name is first column (F1).


    Paul ~~~
    Microsoft MVP (Visual Basic)
     
    Paul Clement, Feb 27, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ruca

    Read txt File to a DataSet

    ruca, Feb 26, 2004, in forum: ASP .Net
    Replies:
    15
    Views:
    7,992
    Paul Clement
    Feb 27, 2004
  2. Sameen
    Replies:
    2
    Views:
    459
    Victor Bazarov
    Aug 29, 2005
  3. King

    Read .txt file like .py file

    King, Jul 27, 2008, in forum: Python
    Replies:
    2
    Views:
    258
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,680
    Roedy Green
    Sep 15, 2011
  5. Ram
    Replies:
    3
    Views:
    260
    Tad McClellan
    Apr 26, 2007
Loading...

Share This Page