How to reuse an array in asp.net vb - asp.net

I'm having to build a table of web pages and languages, i.e. page 1: en it de in a schema where there could be up to 14 languages. The page contents are held in a database. So to build the table I'm doing the following:
Dim rowArrayList As New ArrayList
Dim thisRow(languageNum) As String 'languageNum equates to number of columns -1
Database access then:
'# Create row array of cell arrays
If pageName <> lastPageName Then
lastPageName = pageName
If j >= languageNum Then
rowArrayList.Add(thisRow)
Array.Clear(thisRow, 0, thisRow.Length)
j = 0
End If
thisRow(0) = "<td class=""pageName"">" & pageName & "</td>"
End If
'# Iterate each cell in the row
For i As Integer = 1 To languageNum - 1
If thisRow(i) = "" Then
If transReady = False And active = False Then
thisRow(i) = "<td class=""trans"">" & langISO & "</td>"
ElseIf transReady = True And active = False Then
thisRow(i) = "<td class=""notActive"">" & langISO & "</td>"
ElseIf transReady = True And active = True And i = thisLangID Then
thisRow(i) = "<td class=""active"">" & langISO & "</td>"
End If
End If
j = j + 1
Next
The build the table:
'# Build output table
For Each row As String() In rowArrayList
tableBody.Text += "<tr>"
For Each cell As String In row
If cell = "" Then
tableBody.Text += "<td class=""notTrans""> </td>"
Else
tableBody.Text += cell
End If
Next
tableBody.Text += "</tr>"
Next
The table displays beautifully BUT every row contains the data for what should be the last row. How can it be fixed it so each thisRow is unique in the the rowArrayList? At the moment, every time thisRow is added to rowArrayList, every rowArrayList index is overwritten, not just the one being added.

For the quick fix, instead of this:
Array.Clear(thisRow, 0, thisRow.Length)
Do this:
thisRow = New String(languageNum) {}
or this:
ReDim thisRow(languageNum)
However, I suspect there are some simple design choices you could change that would drastically change this code for the better.

Related

create html table in outlook with specific style color and border using vba

I am trying to create a table in html body of outlook. Table has been created very well with correct headings and data rows, but the style is not what it suppose to. I need to have every other row with different color also the header text and background color need to be changed, I am looking for a comprehensive answer for the possibilities of styles we can manage?
Following is VBA code:
Dim EmailApp As Outlook.Application
Dim EmailItem As Outlook.MailItem
Set EmailApp = New Outlook.Application
For Each cCel In eSh.Range("A2:A" & eSh.Cells(eSh.Rows.Count, "A").End(xlUp).Row)
' Clearing values
cCel.Offset(0, 2) = ""
If IsEmpty(cCel.Offset(0, 1)) Then GoTo NX
mSend = False
EmailBody = ""
' Salutation
' this is the possible line to be changed in order to get the particular style ???
EmailBody = "<style> th , td { border: 1px solid #e3e3e3; padding: 4px 8px; text-align: left; } tr:nth-child(odd) td { background-color: #e7edf0; }</style>Ola, " & _
cCel & "<br><br>" & _
"<table><tr><th>Numero do Contratro</th><th>Contratante</th><th>Saldo de Contrato</th></tr>"
' Filter
DB.Range("1:1").AutoFilter Field:=gestI.Column, Criteria1:=cCel
If DB.Range("A" & DB.Rows.Count).End(xlUp).Row = 1 Then GoTo NX
For Each mCel In DB.Range("AG2", DB.Range("AG" & DB.Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
If mCel.Row = 1 Then GoTo NXX
If Not IsDate(mCel) Then GoTo NXX
'mCel.Select
exDate = DateSerial(Year(mCel) + 1, Month(mCel), Day(mCel))
If DateDiff("d", Date, exDate) < 0 Then
mSend = True
EmailBody = EmailBody & "<tr>" & _
"<td>" & DB.Cells(mCel.Row, "C").Text & "</td>" & _
"<td>" & DB.Cells(mCel.Row, "I").Text & "</td>" & _
"<td>" & DB.Cells(mCel.Row, "X").Text & "</td></tr>"
End If
NXX: Next mCel
If mSend = False Then GoTo NX
Set EmailItem = EmailApp.CreateItem(olMailItem)
With EmailItem
.To = cCel.Offset(0, 1).Text 'Email address from DataBase
.Subject = "InformaciĆ³n Sobre Contractos"
.HTMLBody = EmailBody & "</table><br><br>Atenciosamente"
.Display
.Send
cCel.Offset(0, 2).Value = "Sent" 'mark the mail
X = X + 1
End With
NX: Next cCel
Why don't you use GetInspector.WordEditor property to paste the formatted table?
Several times ago I've written a function to paste table to e-mail
Function SendTable(rng As Range, Email As String, Subja As String) ', Emltext As String)
rng.CurrentRegion.Copy
Dim myInspector As Object
Dim wdDoc As Object
Dim myitem As Object
Set myitem = CreateObject("Outlook.Application").CreateItem(0)
Set myInspector = myitem.GetInspector
Set wdDoc = myInspector.WordEditor
With myitem
.To = Email
.Subject = Subja
' .Body = Emltext
.Display
End With
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(1).Range.Text = Emltext& vbCr
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(3).Range.Text = "Hello2" & vbCr
wdDoc.Range.Paragraphs.Add
rng.CurrentRegion.Copy
wdDoc.Range.Paragraphs(2).Range.PasteExcelTable False, True, False'here is pastes the table
wdDoc.Range.Paragraphs(2).Range.Select
End Function
and in code this function looks like
Sub test()
Dim OA As Object, oitem As Object
Set OA = CreateObject("Outlook.Application")
Set oitem = OA.CreateItem(0)
oitem = SendTable(Range(Cells(2, 2), Cells(4, 4)), "xxxxxx#xxxx.ru", "Report "&Now)
End Sub

Retrieving values from dynamically created controls

First post, so go easy on me.
I've been coding for years, first with VB6, then VB.NET and more recently ASP.NET. I'm ashamed to say, this issue has beaten me to the point where I need to ask for help. What's more annoying is that this should be a simple thing to achieve! I'm clearly missing something here.
I'm creating checkbox controls dynamically, quite a few of them in fact. Two per dynamically created table row and their IDs are appended with the ID of the particular DB record on the row, row 1, 2, 3 etc. So on each row there would be two checkboxes, ihave_check_1, ineed_check_1. The next row would be ihave_check_2 and ineed_check_2 and so on.
There is a submit button at the bottom of the page, and when clicked, it's supposed to loop through each row (and cell) in the table and pick out controls whose IDs contain "ihave_check_" and "ineed_check_" then get their Checked value. Once I have the values, I add a record into the database.
Problem is, when you click the button, the table disappears and so do the values.
From what I've read so far, this is happening because the controls are dynamically created, if they were static (coded in the HTML section) I wouldn't have this problem.
So first question, what do I need to do to get it working?
And second question, why is using dynamic controls so difficult?
Here's the code setting up the table, which works great:
Private Sub ddCardSeries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddCardSeries.SelectedIndexChanged
If IsPostBack = True And Not ddCardSeries.SelectedValue = "Select..." Then
cardsTable.Visible = True
Dim dat As New DataLayer3.DataConnector
dat.DataConnector("Provider=SQLOLEDB;Server=192.XXX.XXX.XXX;Database=GPKDB;User Id=sa;Password=XXXXXXXXXXX;")
Dim dtSections As New DataTable
dtSections = dat.DataSelect("SELECT baseCardID,baseCardSeries,baseCardNumber,baseCardName,frontArtist,conceptArtist,backArtist,backWriter,isBaseCard,isDieCut,isMatte,isGlossy,differentBack,frontImage,backImage FROM baseCards where baseCardSeries = '" & Split(Split(ddCardSeries.Text, "(ID:")(1), ")")(0) & "' and isBaseCard = 'Yes'")
If dtSections.Rows.Count > 0 Then
For i As Integer = 0 To dtSections.Rows.Count - 1
Dim row As New TableRow
For x = 0 To dtSections.Columns.Count - 1
Dim cell1 As New TableCell
If Not IsDBNull(dtSections.Rows(i)(x)) Then
If x = 0 Then
cell1.Text = dtSections.Rows(i)(x)
ElseIf x = 1 Then
cell1.Text = get_card_series(dtSections.Rows(i)(x))
ElseIf x = 13 Then
cell1.Text = "<img src='" & dtSections.Rows(i)(x) & "' height='120'"
ElseIf x = 14 Then
cell1.Text = "<img src='" & dtSections.Rows(i)(x) & "' height='120'"
Else
cell1.Text = dtSections.Rows(i)(x)
End If
Else
cell1.Text = ""
End If
row.Cells.Add(cell1)
Next x
Dim newbutton As New Button
Dim newlabel As New Label
newlabel.Text = "<br />"
newbutton.Text = "Modify this entry"
newbutton.Width = 120
newbutton.ID = "modify_button_" & dtSections.Rows(i)(0)
Dim newcheck1 As New CheckBox
Dim newlabel2 As New Label
newlabel2.Text = "<br />"
newcheck1.Text = "I own this card"
newcheck1.Width = 120
newcheck1.ID = "ihave_check_" & dtSections.Rows(i)(0)
Dim newcheck2 As New CheckBox
newcheck2.Text = "I need this card"
newcheck2.Width = 120
newcheck2.ID = "ineed_check_" & dtSections.Rows(i)(0)
Dim cell2 As New TableCell
If is_user_admin() = True Then
newbutton.Enabled = True
Else
newbutton.Enabled = False
End If
cell2.Controls.Add(newbutton)
cell2.Controls.Add(newlabel)
cell2.Controls.Add(newcheck1)
cell2.Controls.Add(newlabel2)
cell2.Controls.Add(newcheck2)
row.Cells.Add(cell2)
cardsTable.Rows.Add(row)
Next
End If
Else
cardsTable.Visible = False
End If
End Sub
Here's the code that loops through the table and tries to save the results to the database:
Protected Sub SubmitChanges_Click(sender As Object, e As EventArgs) Handles SubmitChanges.Click
For Each pcontrol As control In Page.Controls
Dim havecard As String = Nothing
Dim needcard As String = Nothing
Dim rowcardid As String = Nothing
'For Each tabcell As TableCell In tabrow.Cells
'For Each pgcontrol As Control In tabcell.Controls
If TypeOf pcontrol Is CheckBox And Split(pcontrol.ID, "_")(0) = "ihave" Then
rowcardid = Split(pcontrol.ID, "_")(2)
Dim chkbox As CheckBox = pcontrol
If chkbox.Checked = True Then
havecard = "Yes"
Else
havecard = "No"
End If
End If
If TypeOf pcontrol Is CheckBox And Split(pcontrol.ID, "_")(0) = "ineed" Then
rowcardid = Split(pcontrol.ID, "_")(2)
Dim chkbox As CheckBox = pcontrol
If chkbox.Checked = True Then
needcard = "Yes"
Else
needcard = "No"
End If
End If
'Next
If Not havecard = Nothing And Not needcard = Nothing Then
If add_card_to_user_list(Session("username"), rowcardid, havecard, needcard) = True Then
Label1.Text = "Update complete"
Else
Label1.Text = "Update failed"
End If
End If
'Next
Next
End Sub
Public Function add_card_to_user_list(ByVal userid As String, ByVal cardid As String, ByVal own As String, ByVal need As String) As Boolean
Try
Dim dat As New DataLayer3.DataConnector
dat.DataConnector("Provider=SQLOLEDB;Server=192.XXX.XXX.XXX;Database=GPKDB;User Id=sa;Password=XXXXXXXX;")
Dim dtCardSeries As New DataTable
dtCardSeries = dat.DataSelect("select CardID from [" & userid & "_cards] where cardid = '" & cardid & "'")
If dtCardSeries.Rows.Count > 0 Then
dat.DataDelete("delete from [" & userid & "_cards] where cardid = '" & cardid & "'")
End If
dat.DataInsert("insert into [" & userid & "_cards] (Username,CardID,Own,Need) values ('" & userid & "', '" & cardid & "', '" & own & "', '" & need & "');")
Return True
Catch ex As Exception
Return False
End Try
End Function
Any help at this point would be gratefully received.
Thanks!

ASP Classic - How to count the number of sub items in a nested list

I have a nested list that I use to display available videos from the db grouped under their respective headers.
I'm wondering if it's possible to count the number of videos in each grouping so that I could display it like this:
Video Group Title 1 (3 videos)
1. Video 1
2. Video 2
3. Video 3
Video Group Title 2 (6 videos)
1. Video 1
2. Video 2
...etc.
The SQL is pretty simple right now:
SELECT * FROM video_module_type;
I'm using ASP VBscript and the code for what I've got is:
<%
'----------------------------------------------------------------------------------------------------
'VARIABLES
'----------------------------------------------------------------------------------------------------
Dim previous_video_module_name
Dim first_video_module_name
Dim DIV_vms_vid_title
Dim DIV_vms_grouping
Dim DIV_video_section_group
Dim DIV_vms_video_holder
Dim DIV_vms_sm_thumb
Dim DIV_vms_results
Dim vms_shim
Dim DIV_end
previous_video_module_name = ""
first_video_module_name = true
DIV_vms_vid_title = "<div class=""vms_vid_title"">"
DIV_vms_grouping = "<div id=""sustvid_webinar"" class=""vms_grouping"">"
DIV_video_section_group = "<div class=""video_section_group"">"
DIV_vms_video_holder = "<div class=""vms_video_holder"">"
DIV_vms_sm_thumb = "<div class=""vms_sm_thumb"">"
DIV_vms_vid_synopsis = "<div class=""vms_vid_synopsis"">"
DIV_vms_results = "<div class=""vms_results"">"
vms_shim = "<img src=""/images/shim.gif"" width=""16"" height=""16"">"
DIV_end = "</div>"
'----------------------------------------------------------------------------------------------------
'IF NOT THE SAME VALUE AS PREVIOUS
'----------------------------------------------------------------------------------------------------
for i = 0 to videoModulesListerNumber-1
if video_module_name(i) <> previous_video_module_name then
'------------------------------------------------------------------------------------------
'IF NOT THE FIRST TIME, CLOSE THE NESTED LIST
'------------------------------------------------------------------------------------------
if first_video_module_name then
response.Write("<h2>" & video_module_name(i) & "</h2>")
end if
'------------------------------------------------------------------------------------------
'DISPLAY THE CATEGORY
'------------------------------------------------------------------------------------------
response.Write("<!--START VIDEO GROUP: " & video_module_name(i) & "-->")
'------------------------------------------------------------------------------------------
'IF NOT THE SAME VALUE AS PREVIOUS OPEN THE NESTED LIST AND STORE THE CURRENT
'VALUE FOR COMPARISON NEXT TIME
'------------------------------------------------------------------------------------------
previous_video_module_name = video_module_name(i)
end if
'------------------------------------------------------------------------------------------
'DISPLAY THE VIDEOS
'------------------------------------------------------------------------------------------
response.Write(DIV_vms_grouping)
response.Write(DIV_video_section_group)
response.Write(DIV_vms_video_holder)
response.Write(DIV_vms_vid_title & "<h2>" & video_module_video_name(i) & "</h2>" & DIV_end)
response.Write(DIV_vms_sm_thumb & video_module_thumbnail(i) & DIV_end)
response.Write(DIV_vms_vid_synopsis)
response.Write("<p>" & video_module_synopsis(i) & "</p>")
response.Write(DIV_vms_results & vms_shim & DIV_end)
response.Write(DIV_end) ' close DIV_vms_grouping
response.Write(DIV_end) ' close DIV_video_section_group
response.Write(DIV_end) ' close DIV_vms_video_holder
'------------------------------------------------------------------------------------------
'IT'S NO LONGER THE FIRST TIME; CHANGE THE "first_video_module_name" VARIABLE
'------------------------------------------------------------------------------------------
first_video_module_name = false
next
%>
I think the problem may be that I don't know what the term for this is, so I can't search for it, but any help would be appreciated.
You can change your query to something like
SELECT video_module_type.*, (SELECT COUNT(*)
FROM video_module_type as t2
WHERE t2.Group = video_module_type.VideoGroup) as GroupCount
FROM video_module_type
And use the GroupCount field.
OR
You can traverse the results two times, the first time counting the items and the second time displaying the results
OR
You can use Javascript to count the items and display the results
->JsFiddle Example

How show colors in export to excel in asp.net

iam doing export functionality in asp.net1.1....
I want to highlight some records in red color when i export to excel...
following is my code to export records in excel but i want some records in red colors...
So how to do this plz help me out.
Public Shared Sub ExportToExcelInvitee(ByVal query As String, _
ByRef Response As System.Web.HttpResponse, _
Optional ByVal exportDataset As DataSet = Nothing)
Dim index As Integer
Dim colIndex As Integer
Dim columnCount As Integer
Dim excelDataSet As DataSet
Dim cnt As Integer
Const PROC As String = CLASSNAME & ".ExportToExcelInvitee"
Try
If IsNothing(exportDataset) Then
excelDataSet = ExecuteDataset(query)
Else
excelDataSet = exportDataset
End If
If Not IsNothing(excelDataSet) Then
If excelDataSet.Tables(0).Rows.Count <> 0 Then
Response.Clear()
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Response.AddHeader("Content-Disposition", "attachment; filename=List.xls")
Response.Write("<TABLE border=1>")
Response.Write("<TR>")
Response.Write("<TD><DIV align=center><B>Sr. No.</B></DIV></TD>")
columnCount = excelDataSet.Tables(0).Columns.Count - 1
For index = 2 To columnCount
Response.Write("<TD>" & _
"<DIV align=center>" + _
"<B>" & excelDataSet.Tables(0).Columns(index).ColumnName.ToString & "</B>" + _
"</DIV>" & _
"</TD>")
Next
Response.Write("</TR>")
Response.Write("<TR>")
' Loop to leave one empty line after header,
' Loopimg to add the TD with black boders which doens not get added if only TR added
For index = 2 To columnCount
Response.Write("<TD></TD>")
Next
Response.Write("</TR>")
cnt = 1
For index = 0 To excelDataSet.Tables(0).Rows.Count - 1
If Not (excelDataSet.Tables(0).Rows(index).RowState = DataRowState.Deleted) Then
Response.Write("<TR>")
Response.Write("<TD>" & _
"<DIV align=left>" & _
(cnt).ToString() & _
"</DIV>" & _
"</TD>")
For colIndex = 2 To columnCount
Response.Write("<TD valign=top>" & _
"<DIV align=left>" & _
excelDataSet.Tables(0).Rows(index).Item(colIndex).ToString() & _
"</DIV>" & _
"</TD>")
Next
Response.Write("</TR>")
cnt = cnt + 1
End If
Next
Response.Write("</TABLE>")
Response.End()
End If 'DataSet must contain data
End If 'DataSet must contain data
Catch ex As Exception
Call ErrorLog(PROC & ", " & ex.Source, ex.Message)
End Try
End Sub
In your case, that would be simply:
<TD style='color: red'>Some value</TD>
I can't see where you decide which cells should be highlighted red - however, since you're basically outputting an html table, you should be able to use standard markup to change either the 'color' or 'backgroundColor' attribute for that html element.

asp pagination problem

Hi i have a problem with this asp pagination. it seems to be putting all the links in the one row, so i think it might have something to do with the check of the int i...
but im not that familar with asp. can anyone shed any light on this problem.
the folders contain pdfs for each day of the month, named A08P2.pdf A09P2.pdf etc...
Thanks
i = 1
Set fc = f.Files
Set ff = f.SubFolders
For Each f1 in fc
intPage = cint(mid(f1.name,2,2))
chrEdition = mid(f1.name,1,1)
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
end if
i = i + 1
Next
You should move the incrementing of the i (i=i+1) inside the if...end if, since if i is 9 and you encounter two chrEditions that are not 'A' then i will become 11 and will never match the closing condition i=10:
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
i = i + 1
end if

Resources