Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Load a textfile on a textbox

by the3factory 5/6/2008 8:23:00 AM
Question:

Hi 
I need the contents of textfiles on my webserver.

It's possible to load the file on a textbox?

Thank you 


Answer1:
Answer2:

Hi,  let's take an example:

 

Suppose you have a textfile like:  Product.txt in the root of your project, a button - btn1, and a textbox - txt1.

You want the text in the textfile to be appear in the textbox when you press the button. you go to the button click even(doubleclick...)

In the top of the page import as following: using System.IO;

in the click event:

protected void Button1_Click(object sender, EventArgs e)

{

StreamReader sr=new StreamReader(Server.MapPath("~/Product.txt"); //If your txt file is somewhere in your computer - you have to write the full path - //c://blablabla...

this.TextBox1.Text=sr.Read();

 

It should work - try it, if you have problems, tell me I'll try to solve them.

 

Regards,

Tengo

 


Answer3:

HI Tengo

I converted you code to vb.net ang i got the following:

Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim sr As StreamReader = New StreamReader("c:\..\quem.txt")

Me.TextBox3.Text = sr.Read()

End Sub

There is my file quem.txt

mario
jose
santos
lopes

as a result i got in my textbox 109. Why?


Answer4:

Hi, I'm sorry mister, I didn't notice - accidently I wrote sr.Read() instead of sr.ReadToEnd().

So write Me.TextBox3.Text=sr.ReadToEnd();

Oh and add the sr.Close(); OtherWise it will give you later errors.

Regards

Tengo


Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0