Simple Question, how to I create a dynamic CSV - ASP.NET, VB - asp.net

Howdy, Now I'm a complete newby to .Net and the likes of VB (I'm more of a Ruby/Rails, PHP kinda guy).
Anyway I currently have a sub that has a string builder that I'm wish to use as the content of a CSV which I will email later from a webpage. My stringbuilder is creating the content for the CSV but I don't know how to create the CSV dynamically (I don't want the CSV on disk).
Private Shared Sub BuildClientTrustBalanceCSV(ByVal sb As StringBuilder, ByVal clientsWithTrustBalance As List(Of NonPaymentHistory))
If clientsWithTrustBalance Is Nothing OrElse clientsWithTrustBalance.Count = 0 Then
Exit Sub
End If
' need to somewhere create the CSV then inject the StringBuilder into it?
With sb
' create the CSV
For Each zeroBalanceClient As NonPaymentHistory In clientsWithTrustBalance
.AppendFormatLine("{0},", zeroBalanceClient.Number)
Next zeroBalanceClient
End With
End Sub
Any ideas? I know I should be able to sort this but .NET just isn't my thing?

Here is a link that might be helpful, but I agree with Oded that the question is misleading.
How do I best generate a CSV (comma-delimited text file) for download with ASP.NET?

Related

search folder through directory ASP VB.NET

Is there a way to search folder name(whether its root folder or subfolder and with files inside) through web vb.net? I searched all over the internet and haven't found single help. this is what i've done so far. but this is in windows forms.
I am looking for web forms. thank you
Protected Sub search_Click(sender As Object, e As EventArgs) Handles Search.Click
Dim MainFldr = "d:\shared\"
Dim SKfiles() As System.IO.FileInfo
Dim FldrInfo As New System.IO.DirectoryInfo(MainFldr)
Dim flpth As String
flpth = ""
ListBox2.Items.Clear()
SKfiles = FldrInfo.GetFiles("*" & txtfolder.Text & "*.*", IO.SearchOption.AllDirectories)
For Each MySearchfile In SKfiles
flpth = ""
flpth = MySearchfile.DirectoryName + "\" + MySearchfile.Name
ListBox2.Items.Add(flpth)
Application.DoEvents()
Next
End Sub
Well, it turns out you can do the same in web land.
However, one huge "issue" to remember?
For any web page url, web page link, web page document reference? You will use the web site URL for that file in question.
So, say you have the web site (root) and then a folder called Images.
images\cat.jpg
So, the path name for the WEB SITE MARK up will be:
"~/images/cat.jpg
however, WHEN you run code behind? (your vb.net code). Now, ALL AND EVERY file reference MUST be a full normal standard windows path name!!!
so, if say your vb.net code was to "test" if the above file existed?
Lets say we drop a button and a image control on a web page - we have this:
and lets just make this super simple - we want in vb code to set the above image control to a picture we have in our Content folder.
Well, you have to translate the web based url into a plane jane good old fashioned windows file path name!
So, you would do this:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strF As String
strF = "~/Content/uncheck1.jpg"
Dim strFInternal = Server.MapPath(strF)
Debug.Print(strFInternal)
If File.Exists(strFInternal) Then
Image1.ImageUrl = strF
End If
End Sub
Note how our INTERNAL code MUST use a good old plane jane windows file path name.
but for things on the web page? They are ALWAYS referenced from the point of view of the web site. After I run the above code I get this:
But that debug.print of the file name? This is the output:
Output:
C:\Users\AlbertKallal\source\repos\WebFriendlyTest\Content\uncheck1.jpg
Note VERY careful the above? It was a plane jane INTERNAL file name. I can't tell you how many times I struggled with above simple concept:
Web page controls and markup: use correct URL path names
Code behind: use full windows path name!!!
So, your posted code? it WILL work near as it stands!!!!
The only issue here is what folder you looking to push out to that list box?
Say in above, I wanted to push out ALL files in that Content Folder.
I mean, I can type in this to display that check box if I wanted to:
So the web page now displays that one simple image on the page - since I typed in the FULL correct url!!!
So, how about we drop a list box on the web form, and when we click our button, we display ALL files in that Content folder. Now I could "guess" the location of the file folder, but for ANY WEB page to use that folder, it HAS to be part of the web site folders (or sub folders). The web site can NOT just out of the blue map to steal or grab ANY file on the computer - those URL's can ONLY point to say root of the site + any sub folder.
So that content folder on this site is this:
So, as you can see, then I can type in a url that is the base site, and then that folder, and then that picture (uncheck1.jpg) that exists in that folder.
So, lets drop in a listbox, and push out all the files from that folder to the listbox on the form:
Our markup will be this:
So, just a button, and a list box.
And the button code for the Content folder? Well, we would use this:
(I removed the search part - but that is standard code - JUST like you ALWAYS used in the past!!!
So:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MainFldr = "~/Content"
Dim FldrInternal = Server.MapPath(MainFldr)
Dim SKfiles() As System.IO.FileInfo
Dim FldrInfo As New System.IO.DirectoryInfo(FldrInternal)
ListBox1.Items.Clear()
SKfiles = FldrInfo.GetFiles("*.*", IO.SearchOption.AllDirectories)
For Each MyFile In SKfiles
ListBox1.Items.Add(MyFile.Name)
Next
End Sub
I decided to NOT use the full path name - but I could have.
The output:
so just remember, that any web page? It can ONLY use files and things that are PART of the web site. And any url - even for a picture, file etc.? Then it MUST be a valid URL.
However, the code behind? It is NOT limited to JUST the folders and files from the root and sub folders. You can actually use your d: drive, or anything else that box on the network can get/grab/use/fetch. So the code behind is your plane jane windows code.
The web site URL and files from web pages thus can only use folders/files that are part of the web site. However, in some cases, say you had some other big server with folders for pictures and files - but your web site (and valid URL's) need to use that folder? Well, you then setup what is a called a virtual directory. This you quite much need to use IIS as opposed to using IIS express edition. (you can google for express edition + setup virutal folders).
Once that virtual folder is setup? It will look like any other sub folder in teh site - but they are not in the site anymore. (and this is also used for documents or PDF folders often too).
This is simply LOOKS like a sub folder, but it really is a folder that can point to any other folder on your computer network. of course this would only work if the final web server and published site location is running on a box on your server, and say not some hosting plan.
So one should code and NOT think in terms of full valid local server file names, but try to always think in terms of the URL, and the path name, and even path names in sub folders in that web site. But in all cases, the code behind operations are against plane jane valid full path names.
However, if you thought that debug.print of the simply .jpg file name was long or funny? Well, on a published server those file path names can be super crazy paths - but you NEVER care if you reference the folders as per above.
I solve the problem by creating search like below. searching folder name and subfolder.
Dim files As FileInfo() = Nothing
Dim dirInfo As DirectoryInfo = New DirectoryInfo(startFolder)
Dim dir As DirectoryInfo() = Nothing
dir = dirInfo.GetDirectories(folderName, SearchOption.AllDirectories)
For i = 0 To dir.Length - 1
If dir(i).Name = folderName Then
Dim path2 = Directory.GetDirectories(dir(i).FullName)
If path2.Length = 0 Then
files = dir(i).GetFiles()
For Each file In dir(i).GetFiles()
path = CStr(dir(0).FullName)
path = path.Remove(0, 11)
HiddenField1.Value = path & "\" & file.Name
HiddenField2.Value = file.Name
Dim tr As New TableRow
tr.Cells.Add(New TableCell With {.Text = file.Name})
grd.Rows.Add(tr)
Dim tdlnk As New TableCell
Dim lnk As New HtmlAnchor
tdlnk.Controls.Add(lnk)
tr.Cells.Add(tdlnk)
Next
Exit For
Else
Dim aaa As String = path2(0).Substring(path2(0).Length - 10)
startFolder = path2(0).ToString
folderName = aaa
Dim files1 As FileInfo() = Nothing
Dim dirInfo1 As DirectoryInfo = New DirectoryInfo(startFolder)
Dim dir1 As DirectoryInfo() = Nothing
dirInfo1.GetDirectories(folderName, SearchOption.AllDirectories)
files1 = dirInfo1.GetFiles
For Each file In dirInfo1.GetFiles
path = CStr(dirInfo1.FullName)
path = path.Remove(0, 11)
HiddenField1.Value = path & "\" & file.Name
HiddenField2.Value = file.Name
Dim tr As New TableRow
tr.Cells.Add(New TableCell With {.Text = file.Name})
grd.Rows.Add(tr)
Dim tdlnk As New TableCell
Dim lnk As New HtmlAnchor
tdlnk.Controls.Add(lnk)
tr.Cells.Add(tdlnk)
Next
Exit For
End If
End If
Next

Using RTF in VB.NET to create a document

Thanks for viewing my post. I'm looking for ideas regarding creating a document with rtf. I started using word to create a word document automatically, but word automation does not work too well server side, and it is not recommended to do so by Microsoft. I know how to do basic text with rtf, but I'm wondering if I could create a template with bookmarks and fill in these bookmarks with data from a database. Any ideas would be appreciated. I'm not looking for handouts (ex- coding), just looking for ideas. Thank again for your time! Any info will be greatly appreciated! I'm using asp.net.
Josh
RTF stand for Rich Text Format. the toolbox contains a RichTextBox control which will read,edit, and write rtf documents. You can easily extend this to get the functionality you want. Here's an example
Here's an example of a rich text editing control for asp.net
I'm going to go at this in a new way. I'm going to create a html document with 'placeholders'. Then I will call on the document to save with the .doc extension. Testing has shown that Microsoft Word will open this document. Thanks again.
Josh
Public Function GetTemplate( _
ByVal strTemplateFile As String) As String
Dim strPath As String = ""
strPath = Server.MapPath("~/Templates") & "\" & strTemplateFile
Return My.Computer.FileSystem.ReadAllText(strPath)
End Function
and
Dim strTemplate As String = ""
Dim blnLB As Boolean = True
strTemplate = GetTemplate("Legislative_Bulletin.htm")
Response.AppendHeader("Content-Type", "application/msword")
Response.AppendHeader("Content-disposition", "attachment; filename=Legislative_Bulletin" & ".doc")
Response.Write(strTemplate)
Response.End()
this is just example coding. you will still need to insert the data into the placeholders of the html template. I hope this helps someone!
Josh

Read source of popup windows opened with javacript:void()?

I am going to do my best to ask this question as clearly as possible. Is it possible to write some asp.net code that can go through and read the source of pop up windows that are normally opened by clicking a javascript:void() link. Basically i want to read the source of said popups, extract specific links and then render those links in a web page. What i am trying to achieve is a way to make it easy to download the videos of the Senate floor - their videos open in a new popup that uses silverlight. The mp4 file location is in the source so i want to read that url. I did something similar using the code below. The difference was that the mp4 links were in main page. The code read the source and then outputted the video links so i could just right click and do a save as. This was for the videos at the German security conference. Apologies if this is not seen as a constructive question and ends up being closed.
Protected Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnClick.Click
Dim r As New Regex("\bhttps?://\S+\.(?:jpg|png|gif|mp3|mp4|3gp)\b", RegexOptions.IgnoreCase)
Dim c As New WebClient
Dim s As String = c.DownloadString(txtUrl.Text)
Dim sb As New StringBuilder
For Each m As Match In r.Matches(s)
sb.Append("" & m.Value & "<br />")
divLinks.InnerHtml = sb.ToString
Next
End Sub

How to add a hyperlink in a text file in ASP.NET using VB

I have tow pages, first one for displaying the data in the text file and the other one is to store the data. I want to save a site in the text file but when i display the data will be just text and I can't click it as a link. The code I post is when I write to a file in display page.
Try
Dim fs As String
fs = Server.MapPath("Footer.txt")
lblsplittext.Text = ""
Dim filestream As StreamReader
filestream = New IO.StreamReader(fs)
Dim readcontents As String = filestream.ReadToEnd()
Dim textdelimiter As String = "#"
Dim splitout = Split(readcontents, textdelimiter)
Dim i As Integer
For i = 0 To UBound(splitout)
lblsplittext.Text &= splitout(i) & "<br>"
Next
filestream.Close()
Catch ex As Exception
Dim str As String
str = ex.Message
End Try
If you have a different suggestion about how to read from a file or database (it doesnt matter from where for now) just keep in mine when i display them i just need to have hyperlinks among my text... Or can I write to html file instead of text file if so what is the difference i need to make to write to html. I really really need help here and i have done many searches but i found nothing.
Thanks in advance.
There are a number of ways you could do this. One would be to use a series of dynamically-added Label controls. Your Hyperlink control could simply be inserted between one Label control and the next.
Do you intend to retrieve information from your series of labels on postback? (It would be redundant to do that, since you already know what the information is, but just in case.) Using multiple controls would make that more complicated. You could try one or more Literal controls, created dynamically and added as child controls to a Panel control. Again, the Hyperlink control would be added at whatever time you need it.

Creating a dynamic graphic in asp.net

I am busy creating a control in asp.net. The control consists of some text and an image. The image will be used to display a graph. The control gets the data for the graph by reading it from a database. It seems I have two options to display the graph in the image box. First, I can create a jpg from the data and save the file on the server. I can then load this file into the image box. I think this would cause a problem if various users try to access the site at the same time, so I don't think it is a good option. The other options is to create a file called output, that outputs the graph like this: objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
I can then display the image like this in my control: Image1.ImageUrl = "output.aspx"
The problem that I am facing is how do I get the data from my control to the output page? As far as I know it is too much data to pass as a parameter. If there is another better method of doing it, please let me know
Thanks
You can write the image content to the response of output.aspx. Something like this (taken from one of my existing GetImage pages:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strFilename As String
strFilename = Request.QueryString("filename")
Dim strPath As String = AppSettings("ProxyImageSavePath") & strFilename
Response.Clear()
Response.ContentType = "image/jpeg"
If Not IO.File.Exists(strPath) Then
'erm
Response.Write("file not found")
Else
Response.WriteFile(strPath)
End If
Response.End()
End Sub
Edit: I've just realised this may not be the solution you're looking for; Your webserver should be able to cache this though.
You need handlers. See in this article, Protecting Your Images, section "Creating the ImageHandler HTTP Handler", you'll find the code how to write the handler, so it outputs an image. You don't need all, it's only an example.
You also need to register the handler in the web.config.
To reference it Image1.ImageUrl = "handler.ashx?graphdata=xxxx". Use QueryString to get the data source to generate the graph.

Resources