Francismori7
New member
- Joined
- Dec 31, 2009
- Messages
- 4
- Programming Experience
- 1-3
I have another question.
I have taken an HTML file called "template.html" and got its content. Then I change some variables and save it to a new file in the same directory. Afterwards, there is something else I need to do before saving but I don't know how.
In the template.html file, I have a table which should represent a table from a SQL database which means I would need to loop it. But I don't know how to loop that.
This is my function:
As I said, I need to loop this:
And fill the first one with "StartTime", the second one with "EndTime" and the last column with "Duration". All that gotten from the SQL statement:
Is that possible? If so, then how?
I have taken an HTML file called "template.html" and got its content. Then I change some variables and save it to a new file in the same directory. Afterwards, there is something else I need to do before saving but I don't know how.
In the template.html file, I have a table which should represent a table from a SQL database which means I would need to loop it. But I don't know how to loop that.
This is my function:
VB.NET:
Public Shared Function GenerateHTMLFile(ByVal ProjectID As Integer)
Dim Content As String = ""
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader(Directory.GetCurrentDirectory() & "\HTML\template.html")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
Content = Content & vbCrLf & line
'Console.WriteLine(line)
Loop Until line Is Nothing
sr.Close()
Dim conn As New SqlConnection(LUMART.My.Settings.ProjectsConnectionString.ToString())
Dim rdrQuery As String = "SELECT * FROM Projects WHERE [ID] = " & ProjectID
Try
conn.Open()
Dim cmd As New SqlClient.SqlCommand(rdrQuery, conn)
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Content = Replace(Content, "PROJNAME", rdr.Item("Name"))
Content = Replace(Content, "PROJSTARTDATE", rdr.Item("CreatedAt"))
Content = Replace(Content, "PROJENDDATE", rdr.Item("DoneAt"))
Content = Replace(Content, "PROJTOTALTIMESPENT", SecondsToText(rdr.Item("TotalTimeSpent")))
Content = Replace(Content, "CLIENTNAME", rdr.Item("Client"))
Content = Replace(Content, "COORDS", rdr.Item("Coords"))
Content = Replace(Content, "PROJTYPE", rdr.Item("Type"))
Dim descr As String = rdr.Item("Description")
descr = Replace(descr, vbCrLf, "<br />")
Content = Replace(Content, "PROJDESCR", descr)
Content = Replace(Content, "PROJ_STARTDATE", rdr.Item("CreatedAt"))
Content = Replace(Content, "PROJ_ENDDATE", rdr.Item("DoneAt"))
Content = Replace(Content, "PROJ_TOTALTIMESPENT", SecondsToText(rdr.Item("TotalTimeSpent")))
rdr.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
Return False
Finally
conn.Close()
End Try
Return True
SaveTextToFile(Content, Directory.GetCurrentDirectory() & "\HTML\" & ProjectID & ".html")
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
Return True
End Function
Public Shared Function SaveTextToFile(ByVal strData As String, _
ByVal FullPath As String, _
Optional ByVal ErrInfo As String = "") As Boolean
Dim bAns As Boolean = False
Dim objReader As StreamWriter
Try
objReader = New StreamWriter(FullPath)
objReader.Write(strData)
objReader.Close()
bAns = True
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
Return bAns
End Function
As I said, I need to loop this:
VB.NET:
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
VB.NET:
"SELECT * FROM WorkedHours WHERE ProjectID = " & ProjectID
Is that possible? If so, then how?
Last edited: