Read txt File to a DataSet

R

ruca

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. :)
 
A

Alvin Bruney [MVP]

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

Guest

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/quickstart/howto/doc/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
 
S

Scott Mitchell [MVP]

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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

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

ruca

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 said:
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/quickstart/howto/doc/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.
 
C

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

Paul Clement

¤ 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 ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
R

ruca

It's quite easy:

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


ruca
 
P

Paul Clement

¤ 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 ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
A

Alvin Bruney [MVP]

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

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
 
C

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
 
R

ruca

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 said:
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 said:
And What I want to appear in my dropdownlist it's only the Name



names
of employees
 
P

Paul Clement

¤ 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 ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top