473,505 Members | 15,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 18 '05 #1
15 4736
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" <ru***@iol.pt> wrote in message
news:eF****************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #2
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
http://samples.gotdotnet.com/quickst...c/Anagrams.asp

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

Nov 18 '05 #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
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

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


Nov 18 '05 #4
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" <an*******@discussions.microsoft.com> escreveu na mensagem
news:AF**********************************@microsof t.com...
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:
http://samples.gotdotnet.com/quickst.../Anagrams.aspx

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 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

Nov 18 '05 #5
Cor
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
///
Nov 18 '05 #6
On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ru***@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 18 '05 #7
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" <Us***********************@swspectrum.com> escreveu na
mensagem news:3f********************************@4ax.com...
On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ru***@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)

Nov 18 '05 #8
And What I want to appear in my dropdownlist it's only the Name

"ruca" <ru***@iol.pt> escreveu na mensagem
news:Oz**************@TK2MSFTNGP11.phx.gbl...
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" <Us***********************@swspectrum.com> escreveu na
mensagem news:3f********************************@4ax.com...
On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ru***@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)


Nov 18 '05 #9
On Fri, 27 Feb 2004 09:56:23 -0000, "ruca" <ru***@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 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(TextConnectionSt ring)
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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 18 '05 #10
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" <ru***@iol.pt> wrote in message
news:uv**************@TK2MSFTNGP11.phx.gbl...
And What I want to appear in my dropdownlist it's only the Name

"ruca" <ru***@iol.pt> escreveu na mensagem
news:Oz**************@TK2MSFTNGP11.phx.gbl...
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" <Us***********************@swspectrum.com> escreveu na
mensagem news:3f********************************@4ax.com...
On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ru***@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)



Nov 18 '05 #11
My dropdownlist it remains empty


"Paul Clement" <Us***********************@swspectrum.com> escreveu na
mensagem news:2n********************************@4ax.com...
On Fri, 27 Feb 2004 09:56:23 -0000, "ruca" <ru***@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 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(TextConnectionSt ring) 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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)

Nov 18 '05 #12
Cor
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
Nov 18 '05 #13
Cor
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

Nov 18 '05 #14
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**************@TK2MSFTNGP09.phx.gbl...
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" <ru***@iol.pt> wrote in message
news:uv**************@TK2MSFTNGP11.phx.gbl...
And What I want to appear in my dropdownlist it's only the Name

"ruca" <ru***@iol.pt> escreveu na mensagem
news:Oz**************@TK2MSFTNGP11.phx.gbl...
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" <Us***********************@swspectrum.com> escreveu na
mensagem news:3f********************************@4ax.com...
> On Thu, 26 Feb 2004 15:49:47 -0000, "ruca" <ru***@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 ~~~ pc******@ameritech.net
> Microsoft MVP (Visual Basic)



Nov 18 '05 #15
On Fri, 27 Feb 2004 15:41:01 -0000, "ruca" <ru***@iol.pt> 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(TextConnectionSt ring)
¤ > 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").ToStr ing))
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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 18 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6753
by: M O J O | last post by:
Hi, (I'm new to XML) I can export data from Access as an XML file (with schema integrated in the XML file). I can read it into a DotNet DataSet, but the schema is not correct that is - I do...
1
10239
by: Sal | last post by:
Hello all, I am working on a project with the following characteristics: 1. Load data from a SQL server table to an xml file 2. Read the xml file into a dataset. 3. Load data from the...
0
4675
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
2
1599
by: Martyn Fewtrell | last post by:
I am having a problem reading an Xml file and loading it into a dataset in VB.net (ASP). I seem to be able to load the remote file from the Url but once in memory I cant manage to load it in to...
3
4609
by: BobAchgill | last post by:
I am trying to read in a xml file that I exported from my MSAccess table using the following lines of code but it bombs. Is this the right code? Is there a way to get a clean export from Access...
0
12776
by: ntuyen01 | last post by:
Hi All, I try to read the data from the excel, but I ran into this problem I have two excel files. file one.xls data inside like this: 1234 1234 1234 1234 abc-123 ...
0
2261
by: tom | last post by:
When I try to read in a csv file it gives me this error message. 'Cannot update. Database or object is read-only.' If I change the extension to txt it processes just fine. I have googled all...
0
3266
by: =?Utf-8?B?bXJjc2hhcnBtYW4=?= | last post by:
Hello, There is a space(as a data) in one of the columns. And i save my DataTable as a xml file using DataSet. I used DataSet.WriteXml method to save as a xml file. Now if I read that .xml file...
4
2736
rizwan6feb
by: rizwan6feb | last post by:
I am working on an application in VB.NET and MySql. I am getting a duplicate entry for key 1 error. My application goes through the following steps. 1. Populate my DataSet from the database. 2....
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.