Showing posts with label txt. Show all posts
Showing posts with label txt. Show all posts

Wednesday, March 28, 2012

Text file

In my table, there is a field which stores the path of a text file (C:\\story.txt).

when i want to retrieve that table with gridview, i want that field will show the all text written in that text file. I mean not the path (C:\\story.txt). Instead it will grab all the text written in that text file. How can i do this?


using (StreamReader stream =newStreamReader(@."c:\story.txt"))

{

string fileContents = stream.ReadToEnd();

}


Plus make sure that ASPNET account as read access to C:/ directory.

HC

Monday, March 26, 2012

TEXT file as backup

Is it OK to save a backup copy of .CS or VB class file with .TXT extension to avoid errors at build? Thanks.

sure... I do it all the time. as long as you dont expect the app to USE it, your fine.

I save many of my .net files as txt, its handy.

Text file not being written using System.IO

I'm trying to figure out why when this runs, it runs with no errors but I'm not seeing a txt file outputed to the location I specified. I've replaced some of the filepath with sss for privacy purposes. If the compiler isn't giving any errors, then why isn't it creating the text file? All I see happens is the windows form defined in my code pops up...do I even need that if this is gonna be run as a scheduled task anyway?

What should I look at?


Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form
Public Shared Sub Main()
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("\\sss\f$\inetpub\wwwroot\sss\ssis\maintenance\sss\phase2\sss_input\sss_input.txt")
Dim input As String
input = sr.ReadLine()
While Not input Is Nothing

Dim sw As StreamWriter = New StreamWriter("\\sss\f$\inetpub\wwwroot\sss\ssis\maintenance\sss\phase2\sss_output\sss_output_test.txt")
sw.Write(input)
sw.Close()

End While

Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try
End Sub
End Class

I am not really following the logic there, especially the part where you are saying: "if input is nothing" (if that is the check to see if the file exists, System.IO.File.Exists is a much much better way of checking that)

Outside of that failure to follow your logic, try using a less complicated path to try to write to... for instance, try using "c:\temp", which is local to your application and your app probably has write permissions there... what will this do?
- Well, if your app works, then this tells you that the problem you are hitting is more than likely related to permissions on the other machine
- If your app still doesn't work, something is amiss with your code

Text file question

For example i have a file named textfile.txt Now what i want to do isthat i open that file and read first line(somestring =readline("textfile.txt"); or smth)then second and then third line. Howcan i do it.
VB.NET
Dim strReader as New FileStreamReader("textfile.txt")
While (you still want to read)
somestring = strReader.ReadLine()
DoSomethingWith_somestring
End While
strReader.Close()
- Dan
You could try this code:
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
This code is for a winform, but you can simply use it as you want.
regards

You will want to use the StreamReader class. Below is the link to MSDN.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiostreamreaderclasstopic.asp
Got it all solved... Thank you for your help!

Text file size limitation?

I have a program that writes records to a .TXT file.

The code seems to fail when the file reaches the 5mb mark.

Is this a limitation in ASP, or is it related to how much memory I have in the machine?

Thank you.how are you writing to the file?
if you are appending, it should not be a problem.
Well, here's a snippet...


fp = File.CreateText("c:\AccountFinal.txt")

...gather data from 50,000 accounts & other sources, etc here...

fp.WriteLine(BranchNo & AcctType & AcctNo & AcctTitle & totAddr & Phone & SSN & OwnType & SPACE(21) & ActTotals2 & SPACE(41))

fp.Close()

Is there an easier way to do this?

Thanks.
Does it throw an error? If so, what's the error.

Maybe your script times out because it takes to long?
No errors.

Just stops.

It goes through the first 12,000 records (~5mb), then stops.

I change the code to go through the next roughly 12,000 records (~5mb), and it stops again.

... and so on, until I get to the last record.

If I only go through any ~10,000 records, the program finishes normally. (has totals at the end)
Just a wild guess, but could this be related to the database access instead of the writing to file?

I remember from VB6 (a long time ago) that records got read into in blocks, and that you had to do .MoveLast .MoveFirst to get the total amount of records. Maybe there's still something like that, and that it goes wrong when it wants to read the next 'block'

Instead of writing to a file, try incrementing a variable once. If it goes above 12000 the problem is in the file writing, if it stops at 12000 I'm guessing database issue
You might be on to something there. (database issue)

I first changed the code to Append the records to the text file like this...

'Get Records from DBs, and do stuff to them...

fp = File.AppendText(FILENAME)
fp.WriteLine(BranchNo & AcctType & AcctNo & AcctTitle & totAddr & Phone & SSN & OwnType & SPACE(21) & ActTotals2 & SPACE(41))
fp.Close()

It still bombed out, around the same time the previous code did.

I tried your suggestion, and got the same results.

Any thoughts on where I should look next?

Thanks.
How are you getting your results? What database are you using?

I remember something vaguely from MySQL, there there was an option to determine the max size of the request sent back.

The maximum size of a BLOB or TEXT object is determined by its type, but the largest value you can actually transmit between the client and server is determined by the amount of available memory and the size of the communications buffers. You can change the message buffer size (max_allowed_packet), but you must do so on both the server and client ends. See section 7.5.2 Tuning Server Parameters.

http://www.mysql.com/doc/en/Server_parameters.html
max_allowed_packet current value: 1048576

I think this is the one. Don't know if your database has something similar, set to 5MB.
You will have to change the settings in web.config or machine.config file for the file size limit. 5 mb is the default limit set by web.config

Try adding the following lines in the <system.web
<httpRuntime executionTimeout="500" maxRequestLength="72000" /
72000 here refers to approximately 72 mb. change it to the limit you want to set and see if that works.
Access 2k is where all of the account info resides.

Basically, I open the table with the 50k records in a DS, and do a for...Next through the entire file. Do you think this can be part of the problem?

Here is the main routine... I didn't include all of the functions, they really just chain to other tables & grab phone#'s & such.


<%@. Import Namespace="System.IO" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.OLEDB" %>
<%@. Page Language="VB" Debug="true" %
<script language="VB" Debug="True" runat="server"
Public strConn As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("db2.mdb") &";"
Dim totAddr, AddressNumber, strPers, strOrg, lblStatus, fName, ActTotals2 as String
Dim myNewRow, totalCount, CKTot, JODTot, IRATot, INDTot, COATot, CSATot, SAVTot As Single
Dim x, fLen as Integer

Sub Page_Load(Src As Object, E As EventArgs)
Dim strSQL AS String = "Select * From Accounts2 Order BY AcctNbr"
Dim GetAcctsCommand As OleDbCommand = New OleDbCommand
Dim Connect as New OLEDBConnection(strConn)
GetAcctsCommand.Connection = Connect
Dim Adapter As New OleDBDataAdapter(strSQL, Connect)
Dim AccountsDS As DataSet = New DataSet
Dim RcdCount As Integer
Adapter.Fill(AccountsDS,"Accounts2")
RcdCount = AccountsDS.Tables("Accounts2").Rows.Count
Dim fp As StreamWriter
Dim AdrNbr, AcctNo, Phone, SSN, AcctType, BranchNo, OwnType, AcctTotal, AcctTitle, tmpField As String
Dim FILENAME as String = Server.MapPath("AccountFinal.txt")

Try
For x= 0 to RcdCount - 1
myNewRow = CType(AccountsDS.Tables("Accounts2").Rows(X).Item("BALAMT"), Single)
If myNewRow > 0 then
totalCount=totalcount + myNewRow
tmpField = AccountsDS.Tables("Accounts2").Rows(X).Item("BranchOrgNbr")
BranchNo = zFill(tmpField, 3)
tmpField = AccountsDS.Tables("Accounts2").Rows(X).Item("AcctNbr")
AcctNo = zFill(tmpField, 13)
AcctType = AccountsDS.Tables("Accounts2").Rows(X).Item("MJAcctTypCd") & SPACE(3-LEN(AccountsDS.Tables("Accounts2").Rows(X).Item("MJAcctTypCd")))
If AcctType = "CK " Then
AcctType = "40"
CKTot = CKTot + myNewRow
Else
AcctType = "10"
SAVTot = SAVTot + myNewRow
End If
OwnType = AccountsDS.Tables("Accounts2").Rows(X).Item("OwnCd") & SPACE(3-LEN(AccountsDS.Tables("Accounts2").Rows(X).Item("OwnCd")))
Select CASE OwnType
Case "JO ", "JA "
OwnType = "JOD"
JODTot = JODTot + myNewRow
Case "S "
If Left(AccountsDS.Tables("Accounts2").Rows(X).Item("CurrMiAcctTypCD"),1) = "R" Then
OwnType = "IRA"
IRATot = IRATot + myNewRow
Else
OwnType = "IND"
INDTot = INDTot + myNewRow
End If
Case "PUB"
OwnType = "COA"
COATot = COATot + myNewRow
End Select
If InStr(AcctTitle, "(Min") > 0 then
OwnType = "CSA"
CSATot = CSATot + myNewRow
End If

AcctTitle = GetTitle(AccountsDS.Tables("Accounts2").Rows(X).Item("AcctNbr"))
SSN = GetSSN(AccountsDS.Tables("Accounts2").Rows(X).Item("PERS_TAXID"), AccountsDS.Tables("Accounts2").Rows(X).Item("ORG_TAXID"))
Phone = GetPhone(AccountsDS.Tables("Accounts2").Rows(X).Item("TAXRPTFORPERSNBR"), AccountsDS.Tables("Accounts2").Rows(X).Item("TAXRPTFORORGNBR"))
If Phone = "" then Phone = Space(14)
AdrNbr = GetAddrNbr(AccountsDS.Tables("Accounts2").Rows(X).Item("TAXRPTFORPERSNBR"), AccountsDS.Tables("Accounts2").Rows(X).Item("TAXRPTFORORGNBR"))
totAddr = GetAddress(AdrNbr)
ActTotals2 = CType(Format(myNewRow,"0000000000.00"), String)
fp = File.AppendText(FILENAME)
fp.WriteLine(BranchNo & AcctType & AcctNo & AcctTitle & totAddr & Phone & SSN & OwnType & SPACE(21) & ActTotals2 & SPACE(41))
fp.Flush()
fp.Close()
lblStatus = "File Succesfully created!"
End If
Next
Catch err As Exception
lblStatus = "File Creation failed. Reason is as follows " & err.ToString()
Finally
End Try
End Sub


I tried all of the other suggestions, now I get this error:

File Creation failed. Reason is as follows System.IO.IOException: The process cannot access the file "c:\inetpub\wwwroot\AccountFinal.txt" because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.IO.StreamWriter.CreateFile(String path, Boolean append) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) at System.IO.StreamWriter..ctor(String path, Boolean append) at System.IO.File.AppendText(String path) at ASP.Accountlist6_aspx.Page_Load(Object Src, EventArgs E) in c:\inetpub\wwwroot\accountlist6.aspx:line 76 DONE!

going to try this next...

"You will have to change the settings in web.config or machine.config file for the file size limit. 5 mb is the default limit set by web.config

Try adding the following lines in the <system.web
<httpRuntime executionTimeout="500" maxRequestLength="72000" /
72000 here refers to approximately 72 mb. change it to the limit you want to set and see if that works. "

I'll let you know what happens.

Thanks.
BTW, if I make the following change, will I need to re-start the web server?

<httpRuntime executionTimeout="500" maxRequestLength="72000" /
Thanks.
That's just a web.config change. You don't even have to recompile for that to take effect, just save it to the server.
Ok, made the last change, now I get this dreaded message:

The page cannot be displayed...

But it looks like the ASPNET_WS.exe process is still going?

Any thoughts on how to keep the page from timing out?

Thanks.
It has to go in the system.web like so


<configuration>
<appSettings>
<add key="connString" value="sss" />
</appSettings>
<system.web>
<httpRuntime maxRequestLength="100000" />
<compilation defaultLanguage="vb" debug="true"></compilation>

Thursday, March 22, 2012

text/plain ContentType

I'm writing an ASPX page to open a .txt file, and display it in the browser.
Here's what I'm doing:

Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "text/plain"
Response.WriteFile(sExportPath)
Response.Flush()
Response.Close()

What happens is that I did an open/save dialog.

If I choose Save, it wants to save the file as MyASPXPageName.aspx
and if I choose open, it opens the .aspx file which contains text.

Anyway that I can actually get this to open in the browser?May be your IIS is writing out a header for caching.

Sekhar.

"George Durzi" <gdurzi@.hotmail.com> wrote in message
news:uYb$u1JfEHA.712@.TK2MSFTNGP09.phx.gbl...
> I'm writing an ASPX page to open a .txt file, and display it in the
browser.
> Here's what I'm doing:
> Response.ClearContent()
> Response.ClearHeaders()
> Response.ContentType = "text/plain"
> Response.WriteFile(sExportPath)
> Response.Flush()
> Response.Close()
> What happens is that I did an open/save dialog.
> If I choose Save, it wants to save the file as MyASPXPageName.aspx
> and if I choose open, it opens the .aspx file which contains text.
> Anyway that I can actually get this to open in the browser?
My page is building the response, and unless I add something manually to the
header, such as

Response.AddHeader "content-disposition", "attachment; filename=""" &
fileName & """"

the file should open automatically in the browser. It works fine for PDF
files...

"Chandra Sekhar" <Chandra.Shekhar@.am.joneslanglasalle.com> wrote in message
news:e23snIKfEHA.2848@.TK2MSFTNGP10.phx.gbl...
> May be your IIS is writing out a header for caching.
> Sekhar.
>
> "George Durzi" <gdurzi@.hotmail.com> wrote in message
> news:uYb$u1JfEHA.712@.TK2MSFTNGP09.phx.gbl...
> > I'm writing an ASPX page to open a .txt file, and display it in the
> browser.
> > Here's what I'm doing:
> > Response.ClearContent()
> > Response.ClearHeaders()
> > Response.ContentType = "text/plain"
> > Response.WriteFile(sExportPath)
> > Response.Flush()
> > Response.Close()
> > What happens is that I did an open/save dialog.
> > If I choose Save, it wants to save the file as MyASPXPageName.aspx
> > and if I choose open, it opens the .aspx file which contains text.
> > Anyway that I can actually get this to open in the browser?

text/plain ContentType

I'm writing an ASPX page to open a .txt file, and display it in the browser.
Here's what I'm doing:
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "text/plain"
Response.WriteFile(sExportPath)
Response.Flush()
Response.Close()
What happens is that I did an open/save dialog.
If I choose Save, it wants to save the file as MyASPXPageName.aspx
and if I choose open, it opens the .aspx file which contains text.
Anyway that I can actually get this to open in the browser?May be your IIS is writing out a header for caching.
Sekhar.
"George Durzi" <gdurzi@.hotmail.com> wrote in message
news:uYb$u1JfEHA.712@.TK2MSFTNGP09.phx.gbl...
> I'm writing an ASPX page to open a .txt file, and display it in the
browser.
> Here's what I'm doing:
> Response.ClearContent()
> Response.ClearHeaders()
> Response.ContentType = "text/plain"
> Response.WriteFile(sExportPath)
> Response.Flush()
> Response.Close()
> What happens is that I did an open/save dialog.
> If I choose Save, it wants to save the file as MyASPXPageName.aspx
> and if I choose open, it opens the .aspx file which contains text.
> Anyway that I can actually get this to open in the browser?
>
My page is building the response, and unless I add something manually to the
header, such as
Response.AddHeader "content-disposition", "attachment; filename=""" &
fileName & """"
the file should open automatically in the browser. It works fine for PDF
files...
"Chandra Sekhar" <Chandra.Shekhar@.am.joneslanglasalle.com> wrote in message
news:e23snIKfEHA.2848@.TK2MSFTNGP10.phx.gbl...
> May be your IIS is writing out a header for caching.
> Sekhar.
>
> "George Durzi" <gdurzi@.hotmail.com> wrote in message
> news:uYb$u1JfEHA.712@.TK2MSFTNGP09.phx.gbl...
> browser.
>