How do I dynamically append text to a Literal Class in asp.net instead of overwriting it? - asp.net

Using asp.net and vb, I am trying to dynamically add names to a Literal Class based on the date associated with the name. I have created two Lists, listDate and listShiftName, with the dates and the associated names respectively. Each name has a date associated with it, but there are more than one name for each date, so when I try to add them, it just overwrites each name so only the last name in the list appears. Here is the code I have so far for adding the names:
For i = 0 To listDate.Count - 1
If listDate(i) = DateTime.Today.ToShortDateString Then
litToday.Text = listShiftName(i)
End If
Next i
What do I need to change in the line litToday.Text = listShiftName(i) to in order for it to append each name instead of overwriting the previous names with the last name in the list? I'm a N00B to asp.net and vb, so please excuse my ignorance.

Try this,
litToday.Text &= " " & listShiftName(i);

Related

How to change date format while exporting as a csv file?

I use below code and its working fine. I don't want to change temp table field(dActiveDate) type but please help me to change the date format.
Note - Date format can be changed by user. It can be YY/MM/DD or DD/MM/YYYY or MM/DD/YY and so on...
DEFINE TEMP-TABLE tt_data NO-UNDO
FIELD cName AS CHARACTER
FIELD dActiveDate AS DATE.
CREATE tt_data.
ASSIGN
tt_data.cName = "David"
dActiveDate = TODAY
.
OUTPUT TO value("C:\Users\ast\Documents\QRF\data.csv").
PUT UNFORMATTED "Name,Activedate" SKIP.
FOR EACH tt_data NO-LOCK:
EXPORT DELIMITER "," tt_data. /* There are more than 15 fields available so using export delimeter helps to have less lines of code*/
END.
OUTPUT CLOSE.
As this a "part two" of this question: How to change date format based on variable initial value? why not build on the answer there?
Wrap the dateformat part in a function/procedure/method and call it in the EXPORT statement. The only change required will be to specify each field rather than just the temp-table.
EXPORT DELIMITER ","
dateformat(tt_data.dactivedate, cDateFormat)
tt_data.cName
This assumes that there's a function called dateformat that takes the date and format and returns a string with the formatted date (as in the previous question).
"and so on..." needs to be specified. Depending on the specification you may have to resort to a custom function like Jensd's answer.
If you can constrain the formats allowed, you can use normal handling by using:
session:date-format = "ymd" / "mdy" / "dmy".
session:year-offset = 1 / 1950. // for four vs two digit year
How you populate these two variables can be done in similar fashion as in the other question.
You may need to reset these session attributes to their initial state in a finally block.

Getting Column Index from SSDBGrid in VB6

I'm producing a report in Crystal Reports where the column order must match the order that they're displayed in in the SSDBGrid in VB6.
To do this, I decided it was best to loop over each column in the grid, and call a function to set the parameter field values to the correct heading (so, column 0's caption would be the value for parameter field #Col1, etc)
Anyway, the code I have for this is
Dim c As Column
Dim cName As String
For Each c In FShow_All_Accounts.grd_accounts.Columns
cName = "#Col" & c.ListIndex
Call setColumnHeaders(c.Index)
crxReport.ParameterFields.GetItemByName(cName).AddCurrentValue ("f")
Next
The problem is that first of all, setting the type of c to Column and looping over each c in grd_accounts seems to be incorrect - grd_accounts is an SSDBGrid, and secondly, it errors when trying to return the index.
So, my question(s):
What is the correct way to loop over each column in an SSDBGrid?
Secondly, how would I then get the column index for the correct column, to pass into the function?
The answer is actually more simple than you're imagining it to be. Your code is very nearly there.
Dim c As Column
Dim i As String
Dim cname As String
i = 0
For Each c in grd_accounts.Columns
i = i + 1
cName = "#Col" & i
crxReport.ParameterFields.GetItemByName(cName).AddCurrentValue (c.Caption)
Next
No need for the helper function. This will iterate over each column, set cName to be "#Col" and the value of i (Also the column number), and therefore that parameter field in the report will be captioned with that columns caption.

Prompt comes up when running report

I have created a report from the following query
SELECT TasksEntries.Project, TasksEntries.Task, Sum(TimeTracker.WorkHours) AS
TotalHours
FROM TasksEntries INNER JOIN TimeTracker
ON (TasksEntries.EmployeeId = TimeTracker.EmployeeId) AND (TasksEntries.TaskID
= TimeTracker.TaskId)
GROUP BY TasksEntries.Project, TasksEntries.Task;
The start and end dates are captured from a form and passed like shown below. WorkDate is a column in the TimeTracker table and is not present in any other table.
strWhere = "WorkDate BETWEEN #" & txtMgrRptStartDate & "# AND #" &
txtMgrRptEndDate & "#"
DoCmd.OpenReport "rptTotalProjectHours", acViewPreview,
"qryTotalProjectHours", strWhere, acWindowNormal
When the report is invoked, I get a box where it says
"Enter parameter value" for Workdate.. I am unsure why I get that.
The query runs fine when I run it separately with the start and end dates hard coded in there. The problem seems to occur only when it is passed through the docmd.openreport. Can someone help me get to the bottom of this? When passing the WorkDate thru the OpenReport, is it mandatory that the column name be present in the SELECT statement?
Appreciate your help in advance.
I solved it by including the date criteria in the WHERE clause of the SELECT statement and specifying the form text box names of start and end dates.
The other thing that I did was to eliminate the query name from the Docmd.openreport since the report was created from the named query.

How to concat string with For Varible Vb

I have 16 items named q1,q2,q3,...,q16 and txt_q1,txt_q2,...,txt_q16
And I need to check each one, so I have a for and inside I have the If like this:
For Cnt AS Integer = 1 To 16 Step 1
If("q"&cnt = "") Then
"txt_q"&cnt.Style.Add("color","blue")
....
..
End If
End For
The idea is check al items with a For to avoid the 16 Ifs, but I have a syntax error.
What can I do?
the txt_q1,txt_q2... are IDs for asp:labels, and I have a radiobuttonList, so I get the radiobutton text and I set q1, q2, q3... to the corresponding radiobuttonlist. So I want to check if there is a radiobuttonlist that is not checked, if not I change the color to blue to the asp:label. So I want to avoid making 16 ifs for each radiobuttonlist and make it with a For, because the only thing that change in the variables is the number is the same "q" and the same "txt_q" so I want to add the number to "q" or "txt_q" to make it one variable called q1 or txt_q1 that already exist and that way acces to the txt_q1.Style.Add() and change the color of that label.
Thank You
Just for your understanding:
When you write "txt_q"&cnt, you are creating a new string literal which holds e.g. "txt_q1", "txt_q2", etc. This resembles the name of the variables, but is really something completely different. The name of the variable is just for you to write in code. The value of the variable is a reference to the control object. What you need is to obtain the reference to the controls. This can be done on several ways, one is to use the FindControl() method.
For Cnt AS Integer = 1 To 16 Step 1
Dim c = FindControl("q" & cnt) 'pass in the ID here, you will get a reference
'you can use c to access the properties e.g. c.Style
End For
Again, just to clarify
c.Style 'c is a variable holding a reference to the control
"c".Style ' "c" is a simple string

Chart doesn't update when adding new rows to an existing Excel table (not without having to use named ranges)

I'm really new to the use of closedXMl and Excel too(at least for this purpose) so sorry if I'm asking silly questions.
I know that closedXML doesn't support charts yet so the only thing that came to mind to get around this was to create my chart using an excel table . That way I thought ClosedXML would update the ranges when I inserted new rows and the chart would pick up on it. Well , it didn't. At least not when I add the rows from code using the closedXML library.
What is curious is that adding new rows from inside excel automatically updates the chart but if I want to get that same result from code, I have to use OFFSET formula along with named ranges and then set the chart source data to these named ranges.
That's why I'd like to know if if there is anything wrong with the code I use to insert new rows:
Dim ruta As String = Server.MapPath("~/Templates/MyTemplate.xlsx")
Dim wb As New XLWorkbook(ruta)
Dim ws = wb.Worksheet(1)
Dim tblData = ws.Table("Table1")
Dim year As Integer = 2000
For i As Integer = 1 To 13
With tblData.DataRange.LastRow()
.Field("Year").SetValue(year)
.Field("Sales").SetValue(CInt(Math.Floor((2000 - 500 + 1) * Rnd())) + 500)
End With
tblData.DataRange.InsertRowsBelow(1)
year = year + 1
Next
tblData.LastRow.Delete()
As you can see the code is very simple and so is the template , that consists of only two columns : "Year"(table1[Year]) and "Sales"(Table1[Sales]
I don't think this has anything to do with my template because as I told you, adding new rows directly from excel works as expected and it is only when I generate the table from code that the chart series doesn't include the new row that were added
Being necessary to manually add the new ranges(Sheet1!Table1[Sales] and Sheet1!Table1[Year]) as it only includes the first row(the one added by default when you insert a table)
Any help would be much appreciated
P.S. Here is a link to a rar containing the full code as well as the excel template(\Templates\MyTemplate.xlsx)
If the problem is that your table doesn't recognise the additional rows, try adding this after the last row delete:
tblData.Resize tblData.Range(1, 1).CurrentRegion
That should resize the table. Then hopefully your table operations should work.

Resources