Any ideas how to convert array into FOR Loop? - asp.net

The code below works great as is:
dashboard1.Text = charArray(0)
dashboard2.Text = charArray(1)
dashboard3.Text = charArray(2)
dashboard4.Text = charArray(3)
dashboard5.Text = charArray(4)
dashboard6.Text = charArray(5)
dashboard7.Text = ""
dashboard8.Text = ""
dashboard9.Text = ""
dashboard10.Text = ""
If dashboardl >= 7 Then
dashboard7.Text = charArray(6)
End If
If dashboardl >= 8 Then
dashboard8.Text = charArray(7)
End If
If dashboardl >= 9 Then
dashboard9.Text = charArray(8)
End If
If dashboardl >= 10 Then
dashboard10.Text = charArray(9)
End If
However, I would like to convert them to FOR Loop as in example below but I am getting errors.
For i = 1 To (dashboardl)
("dashboard" & CStr(i)) = charArray(i - 1)
Next i
Your assistance is greatly appreciated.

You can try this...
For i = 1 To dashboardl
Dim txtBox As TextBox = FindControl("dashboard" & i)
txtBox.Text = charArray(i - 1)
Next i

Ha, I missed the point. Still not on a real computer, but try this...
For i = 1 To dashboardl
Me.Controls("dashboard" & CStr(i)).Text = charArray(i - 1)
Next i

Related

Incrementing Serial Number in PDF Creation - ASP.NET

I am generating a PDF using ITextSharp in ASP.NET. I need to display a table, with some data and Serial No auto incremented. I tried the following (using loop - increment) but it doesn't seem to work.
Dim dsGetStudentFeeDetails = dbl.usp_GetFeeReceiptDetailsForStudent(sid).AsQueryable
For Each f1 In dsGetStudentFeeDetails
Dim i As Integer = 1
stuName = New PdfPCell(FormatPhrase(i))
'stuName.Colspan = 4
'stuName.Border = 0
stuName.NoWrap = True
stuName.HorizontalAlignment = Element.ALIGN_LEFT
pdftable4.AddCell(stuName)
stuName = New PdfPCell(FormatPhrase(f1.FeeAmountPaidDate))
'stuName.Colspan = 4
'stuName.Border = 0
stuName.NoWrap = True
stuName.HorizontalAlignment = Element.ALIGN_LEFT
pdftable4.AddCell(stuName)
i = i + 1
Next
When I run through this during execution, I noticed that the i = i + 1 doesn't get incremented. Why so?
you have to declare and set default value Above for loop use below code
Dim dsGetStudentFeeDetails = dbl.usp_GetFeeReceiptDetailsForStudent(sid).AsQueryable
Dim i As Integer = 1
For Each f1 In dsGetStudentFeeDetails
stuName = New PdfPCell(FormatPhrase(i))
'stuName.Colspan = 4
'stuName.Border = 0
stuName.NoWrap = True
stuName.HorizontalAlignment = Element.ALIGN_LEFT
pdftable4.AddCell(stuName)
stuName = New PdfPCell(FormatPhrase(f1.FeeAmountPaidDate))
'stuName.Colspan = 4
'stuName.Border = 0
stuName.NoWrap = True
stuName.HorizontalAlignment = Element.ALIGN_LEFT
pdftable4.AddCell(stuName)
i = i + 1
Next

There is no row at position 0 error

I have the following code:
Do Until i = dsSalesRep.Tables(0).Rows.Count
strRepID = dsSalesRep.Tables(0).Rows(i)("repID").ToString
strLBP = dsSalesRep.Tables(0).Rows(i)("officename").ToString
strLBPEmail = dsSalesRep.Tables(0).Rows(i)("cemail").ToString
strCCRepEmail = dsSalesRep.Tables(0).Rows(i)("cc1").ToString
strRepName = dsSalesRep.Tables(0).Rows(i)("firstname").ToString & " " & dsSalesRep.Tables(0).Rows(i)("lastname").ToString
strRepEmail = dsSalesRep.Tables(0).Rows(i)("email").ToString
strRepPhone = dsSalesRep.Tables(0).Rows(i)("phone").ToString
searchStr3 = "SELECT * FROM LookupSalesRep WHERE repID='" & strRepID & "'"
Dim SqlAdapter3 As New SqlDataAdapter(searchStr3, myConn2)
Dim dsTerritories As New DataSet
myConn2.Open()
SqlAdapter3.Fill(dsTerritories)
strCountry = dsTerritories.Tables(0).Rows(0)("country").ToString
'strCountry = dsTerritories.Tables(0).Rows.Count.ToString
When I run the web page, though, it gives me the error: There is no row at position 0.
But when I use the code
strCountry = dsTerritories.Tables(0).Rows.Count.ToString
strCountry gets the right number of rows. Please help :(
You are in a loop and at some point the table (dsTerritories.Tables(0)) is null. Check first if the table is null:
If dsTerritories.Tables(0).Rows.Count > 0 Then
strCountry = dsTerritories.Tables(0).Rows(0)("country").ToString
End If

Is there a better solution then ViewData

I have a view that runs on a model RETLogModel which pulls from an entirely different connection string then a table called RecordsTaskViews. I need to pass information from RecordsTaskViews based on information gathered by my model. Currently my function looks like this:
Function RETLog(model As RETModels.RETLogModel) As ActionResult
Dim id As Integer = model.ActivityIDReturn
Dim id2 As Integer
Dim PS As RecordsTaskView = GlobalVar.db.PS.RecordsTaskViews.Find(id)
If model.ActivityIDReturn > 0 Then
id2 = model.ActivityIDReturn
Else : id2 = PS.RefActionID
End If
If model.ActivityIDError > 0 Then
ViewData("ActivityIDErrorValue") = model.ActivityIDError
Else : ViewData("ActivityIDErrorValue") = PS.RefActionID
End If
Dim PS2 As RecordsTaskView = GlobalVar.db.PS.RecordsTaskViews.Find(id2)
If model.ActivityIDReturn > 0 Then
If PS IsNot Nothing Then
ViewData("QRefActionID") = PS.RefActionID
ViewData("QQutDesc") = PS.QutDesc
ViewData("QTaskDesc") = PS.TaskDesc
ViewData("QClientCode") = PS.CltCode
ViewData("QClientName") = PS.CltDesc
ViewData("QMemberID") = PS.BenIDin
ViewData("QMemberName") = PS.BenNameLast & ", " & PS BenNameFirst
ViewData("QDateStart") = CDate(PS.DateStart).ToString("MM/dd/yyyy")
ViewData("QDateRes") = CDate(PS.DateResolution).ToString("MM/dd/yyyy")
ViewData("QCreateUser") = PS.UserIDCreation
ViewData("QLastChangeUser") = PS.UserIDLastChanged
ViewData("QDateReport") = CDate(PS.DateReport).ToString("MM/dd/yyyy")
Else
ViewData("QQutDesc") = ""
ViewData("QTaskDesc") = ""
ViewData("QClientCode") = ""
ViewData("QClientName") = ""
ViewData("QMemberID") = ""
ViewData("QMemberName") = ""
ViewData("QDateStart") = ""
ViewData("QDateRes") = ""
ViewData("QCreateUser") = ""
ViewData("QLastChangeUser") = ""
ViewData("QDateReport") = ""
End If
If PS2 IsNot Nothing Then
ViewData("ORefActionID") = PS2.RefActionID
ViewData("OQutDesc") = PS2.QutDesc
ViewData("OTaskDesc") = PS2.TaskDesc
ViewData("OClientCode") = PS2.CltCode
ViewData("OClientName") = PS2.CltDesc
ViewData("OMemberID") = PS2.BenIDin
ViewData("OMemberName") = PS2.BenNameLast & ", " & PS.BenNameFirst
ViewData("ODateStart") = CDate(PS2.DateStart).ToString("MM/dd/yyyy")
ViewData("ODateRes") = CDate(PS2.DateResolution).ToString("MM/dd/yyyy")
ViewData("OCreateUser") = PS2.UserIDCreation
ViewData("OLastChangeUser") = PS2.UserIDLastChanged
ViewData("ODateReport") = CDate(PS2.DateReport).ToString("MM/dd/yyyy")
Else
ViewData("ORefActionID") = ""
ViewData("OQutDesc") = ""
ViewData("OTaskDesc") = ""
ViewData("OClientCode") = ""
ViewData("OClientName") = ""
ViewData("OMemberID") = ""
ViewData("OMemberName") = ""
ViewData("ODateStart") = ""
ViewData("ODateRes") = ""
ViewData("OCreateUser") = ""
ViewData("OLastChangeUser") = ""
ViewData("ODateReport") = ""
End If
End If
If ModelState.IsValid Then
End If
Return View(model)
End Function
My issue is this is obviously a boat load of ViewData elements. Being as I already have #ModelType RETModels.RETLogModel specified in the view itself is there a more efficient way to pass the data to the view?
Is there a more efficient way to pass the data to the view?
Yes, use a ViewModel that contains all the relevant (combined) fields and bind that to the View.
And use an automated object mapper, such as AutoMapper, to copy data from your business models to the ViewModel.

Resize multidimensional array and sort them by date

I think my code successfully creates the multi dimensional array because I get the right amount when I count it with UBound(DataArray).
But I get null value when I try to display one of the data as Response.Write DataArray(1,0).
Any help appreciated!
sDateArray = Split(DateArray, ",")
sVenueArray = Split(VenueArray, ",")
Dim DataArray()
For i = 0 to uBound(sDateArray)-1
ReDim DataArray(i, 1)
DataArray(i, 0) = sDateArray(i)
DataArray(i, 1) = sVenueArray(i)
Next
Response.Write UBound(DataArray) & "<br /><br />"
DataArray(1,0)
Response.Write DataArray(1,0)
Try Redim Preserve DataArray(i, 1) instead of ReDim DataArray(i, 1)
...or...
sDateArray = Split(DateArray, ",")
sVenueArray = Split(VenueArray, ",")
Dim DataArray(uBound(sDateArray)-1, 1)
For i = 0 to uBound(sDateArray)-1
DataArray(i, 0) = sDateArray(i)
DataArray(i, 1) = sVenueArray(i)
Next
Response.Write UBound(DataArray) & "<br /><br />"
' DataArray(1,0) ' <== commented out cos I think this might be an error - ?
Response.Write DataArray(1,0)
Ok I was bored so I wrote this.
May not be perfect - it's been a while since I used Classic ASP
Function SortByDate(a_input)
x = UBound(a_input, 1) - 1
if( x < 1 ) Then
Response.Write "<p>Invalid input array - first element is empty</p>"
Stop
End If
Dim a_output(x, 1)
Dim earliest_date
For j=0 To x
earliest_date = -1
For i=0 To UBound(a_input, 1) - 1
If a_input(0, i) <> "" Then
If earliest_date = -1 Then
earliest_date = i
Else
If CDate(a_input(0,i)) > CDate(a_input(0,earliest_date)) Then
earliest_date = i
End If
End If
End If
Next
a_output(0, i) = a_input(0, earliest_date)
a_output(1, i) = a_input(1, earliest_date)
a_input(0, earliest_date) = "" ' this one is done so skip next time '
Next
SortByDate = a_output
End Function

merge cells with same values in microsoft office interop excel

i created a report which exports to excel. it exports just fine. what i want to do now is merge the consecutive cells in a column which have the same values. how do i do this? please help me.
this is code that generates the excel body:
Protected Sub generateExcelBody(ByVal xcelworksheet As Microsoft.Office.Interop.Excel.Worksheet, ByVal recarray As Array, ByVal numofrecords As Integer)
Dim chartrange As Microsoft.Office.Interop.Excel.Range
chartrange = Nothing
chartrange = xcelworksheet.Range("B5", "F5")
chartrange.MergeCells = True
chartrange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft
chartrange.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter
chartrange = Nothing
chartrange = xcelworksheet.Range("A8", System.Reflection.Missing.Value)
chartrange.FormulaR1C1 = "Record Series : " & hiddenrs.Value
chartrange = Nothing
chartrange = xcelworksheet.Range("A9", System.Reflection.Missing.Value)
chartrange.FormulaR1C1 = "Department : " & hiddendept.Value
chartrange = Nothing
chartrange = xcelworksheet.Range("A10", System.Reflection.Missing.Value)
chartrange.FormulaR1C1 = "Number of Records : " & numofrecords
chartrange = Nothing
chartrange = xcelworksheet.Range("A14", "F14")
chartrange.Resize(numofrecords, 6).Value2 = recarray
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium
chartrange.Resize(numofrecords, 6).WrapText = True
chartrange.Resize(numofrecords, 6).EntireRow.AutoFit()
chartrange.Resize(numofrecords, 6).Font.Size = 10
chartrange.Resize(numofrecords, 6).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
chartrange.Resize(numofrecords, 6).VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter
End Sub
What you want to do is a if - else statement to check if the value of one column is duplicated or not. If yes -> merge the cells. You can either use built in merge cell function in Excel or "hide" the value of the duplicated cell + hide the border between the original and the duplicated cell. Found this while searching.
Sub FormatLikeDates()
Dim d As Date, r As Long, n As Integer, c As Range
For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Int(Cells(r, 1)) = Int(Cells(r + 1, 1)) Then
n = n + 1
End If
If Int(Cells(r, 1)) <> Int(Cells(r + 1, 1)) And n > 0 Then
For Each c In Range(Cells(r - n + 1, 1), Cells(r, 1))
c.Font.ColorIndex = 2
c.Interior.ColorIndex = 2
Next c
Range(Cells(r - n, 1), Cells(r, 1)).BorderAround ColorIndex:=3, Weight:=xlThin
n = 0
End If
Next r
End Sub
Source : http://www.ozgrid.com/forum/showthread.php?t=57537
I just received a notification that this question got 1000+ views so I thought I'd post what I did a year ago in solution to this problem. I hope you guys find it useful.
Public Sub mergeRows(ByVal grid As GridView)
For rowIndex As Integer = (grid.Rows.Count - 2) To 0 Step -1
Dim currRow As GridViewRow = grid.Rows(rowIndex)
Dim prevRow As GridViewRow = grid.Rows(rowIndex + 1)
For i As Integer = 0 To (currRow.Cells.Count - 1)
If currRow.Cells(0).Text = prevRow.Cells(i).Text Then
currRow.Cells(0).RowSpan = IIf(prevRow.Cells(0).RowSpan < 2, 2, prevRow.Cells(0).RowSpan + 1)
prevRow.Cells(0).Visible = False
If currRow.Cells(1).Text = prevRow.Cells(1).Text Then
currRow.Cells(1).RowSpan = IIf(prevRow.Cells(1).RowSpan < 2, 2, prevRow.Cells(1).RowSpan + 1)
prevRow.Cells(1).Visible = False
currRow.Cells(2).RowSpan = IIf(prevRow.Cells(1).RowSpan < 2, 2, prevRow.Cells(1).RowSpan + 1)
prevRow.Cells(2).Visible = False
End If
End If
Next
Next
End Sub
Protected Sub Gridview1_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Gridview1.PreRender
mergeRows(Gridview1)
End Sub

Resources