Read txt File to a DataSet

  • Thread starter Thread starter ruca
  • Start date Start date
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. :)
 
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.
 
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!
 
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
///
 
¤ 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)
 
It's quite easy:

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


ruca
 
¤ 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)
 
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.
 
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
 
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
 
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
 
¤ 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)
 
Back
Top