Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Monday, May 28, 2007

VB.NET: Data Grid View & Reading From CSV File

Oh my goodness. It's really been ages since I did VB programming. I am so so lost! Today's code:

Public Class frmReport

Private Sub btnGenerateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerateReport.Click

'Datagridview dgvReport settings
Me.Controls.Add(dgvReport)

dgvReport.ColumnCount = 5
With dgvReport.ColumnHeadersDefaultCellStyle
.ForeColor = Color.White
.Font = New Font(dgvReport.Font, FontStyle.Bold)
End With

With dgvReport
.Name = "dgvReport"
.Location = New Point(8, 8)
.Size = New Size(500, 250)
.AutoSizeRowsMode = _
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
.CellBorderStyle = DataGridViewCellBorderStyle.Single
.GridColor = Color.Black
.RowHeadersVisible = False

'Define top column names in dgvReport
.Columns(0).Name = "Date-Time"
.Columns(1).Name = "Action"
.Columns(2).Name = "Area"
.Columns(3).Name = "Section"
.Columns(4).Name = "Link"
.Columns(4).DefaultCellStyle.Font = _
New Font(Me.dgvReport.DefaultCellStyle.Font, FontStyle.Italic)

.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.Dock = DockStyle.Fill
End With

'Open file info.csv & read
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
("C:\Susanna's Work Stuff\Projects\DiGi\DiGi MDL\2007_05_28\reports\reports\bin\info.csv")

'Specify that reading from a comma-delimited file
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
With Me.dgvReport.Rows
.Add(currentRow) 'Add new row to dgvReport
End With
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
End Using

End Sub

End Class

Got the chunk off 2 different MSDN pages. Thankfully it worked, after a bit of tweaking! Phew! All in a day's work...

Monday, May 21, 2007

Sound Embeds In HTML

Now this is a sure giveaway at what an amateur I am. Nonetheless, the existence of this blog serves as a reminder of lessons learnt, if nothing more.

So here I have some code I got from elsewhere on how to embed sound files in HTML:

<embed src ="your_waveormp3etc_filename" width = "275"height = "24" autostart = "false" loop = "false"></embed >

The src parameter is the source audio file. The autostart determines whether the file plays automatically when the page loads.

Wednesday, May 16, 2007

Squash That Bug!

Sometimes programming goes awry for the slightest of errors. In our lingo, we call those nasty things bugs.

I just spent maybe half an hour debugging a code that went wrong just because I forgot the "$" in front of a couple of my variable names.

Aargh. That's programming for you...

Anyways, I've learnt quite a bit of stuff on PHP by now. One of which is file operations such as reading & writing to the file.

Here, I'm gonna be awful nice & share...

Reading from a file:

$c_file = "counter.txt";
$check_counter = fopen($c_file, "r");
$curr_counter = fread($check_counter, filesize($c_file));
fclose($check_counter);

The code reads a counter value stored in the file counter.txt. $check_counter is a file handler which opens the file for reading (specified by the parameter "r"). $curr_counter is the variable which is assigned the value of the counter read from the file. The last line quite obviously closes the file after reading.

Writing to a file:

//read counter value from file counter.txt
$counter_file = "counter.txt";
$get_counter = fopen($counter_file, "r");
$counter = fread($get_counter, filesize($counter_file));

//close the counter.txt file
fclose($get_counter);

//open counter.txt file for writing
$alter_counter = fopen($counter_file, "w");

//decrement counter value by 1
$new_counter = $counter-1;

//write new decremented counter value to counter.txt then close the file
fwrite($alter_counter, $new_counter);
fclose($alter_counter);

This code writes a new counter value to the file counter.txt in 2 steps. First the value is read from the file then the file closed, then in the 2nd part minus 1 from $counter & store the new value in the file counter.txt.

I'm sure there are more efficient ways of doing this. If any veteran out there sees this & doesn't mind sparing me a lesson on how, please drop a comment.

& in the meantime, I spent more than half my day trying to figure out how to either call javascript functions from within PHP script or using javascript code to read my counter value from the file. All just to find a way of disabling the form elements when the registration limit is reached (determined by the counter value of zero). Only to talk to my boss later in the evening & realise that he doesn't mind me just echoing a statement in the window to the effect that the user is informed that their registration is rejected because the registration limit has been reached.

Hmmph.

Oh well, at least I am learning new things everyday. I did learn that there is such a thing as server side Javascript. & I did learn those small bits about event handling for stuff that should happen when HTML code loads onto a page. It's done like this:

< onload="check_limit()">

check_limit() being the javascript function, of course. The comment tags are just so that this post gets by the blogger specifications since HTML tags aren't technically allowed on the post :P

Okies, enough rambling. I aim to be home soon.