How to concat string with For Varible Vb - asp.net

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

Related

How to have a SQLite query with a variable part?

Hi I am trying to create a query for SQLite which has a variable part in it. By variable I mean that a certain part within the string can possibly contain a variable but also an empy value
I tried this but I am not sure whether this works.
SELECT * FROM table WHERE attr LIKE 'ABC% %DEF'
Adding onto my comment, check the below code to test your values.
SELECT CASE WHEN 'ABC G DEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END as test_space,
CASE WHEN 'ABCGGGDEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END AS test_all

How can I dynamically change the where conditions of a for each loop?

I have a table of records that has two logical flags, holdn and holdl. I want to loop through this table with 3 different criteria.
Either flag is TRUE - We want to see everything that is on hold
Flag holdl is TRUE - We only want see items that are on hold for this one reason
Flag holdn is TRUE - We only want to see items that are on hold for this other reason.
I cannot figure out how to dynamically change the for each loop based on this. What I have tried so far is to set the value of a variable based on these conditions and then use the content of the variable as one of the where parameters. This does not work as Progress complains that there is a data mismatch. The variable is a string, the flags are logical, so that does make sense. See sample code below. This is a snippet of the actual code with the the table name changed. The which-hold, order-from, etc variables are defined and set in a different module which calls this one.
DEFINE VARIABLE which-hold# AS CHARACTER FORMAT "x(30)" NO-UNDO.
CASE which-hold:
WHEN "B" THEN which-hold# = "(widget.holdn or widget.holdl)".
WHEN "L" THEN which-hold# = "widget.holdl".
WHEN "N" THEN which-hold# = "widget.holdn".
END CASE.
for each widget where which-hold# and
widget.order-no >= order-from and widget.order-no <= order-thru and
widget.line-no >= line-from and widget.line-no <= line-thru and
widget.joint-no >= joint-from and widget.joint-no <= joint-thru
no-lock:
A bunch of code to make a nice report with the retrieved records...
end.
Self taught Progress programmer here, who has inherited a huge, poorly documented application. Please be gentle.
If you would prefer not to deal with handles a semi-dynamic approach is also possible:
define variable i as integer no-undo.
define query q for customer.
do while true:
update i.
case i:
when 0 then quit.
when 1 then open query q for each customer no-lock where custNum >= 1000.
when 2 then open query q for each customer no-lock where state = "nh".
otherwise open query q for each customer no-lock where name begins "u".
end.
do while true with frame a:
get next q.
if not available customer then leave.
display custNum name state with frame a 10 down.
down with frame a.
end.
close query q.
end.
What you want is actually a dynamic query. I'll get to it at the end, but first I'd like to explain why you won't be able to try and substitute the field name in the which-hold# variable: because the query is evaluated at compile time. And this is what it reads (supposing which-hold# has a value of widget.holdn
FOR EACH widget where "widget-holdn" (...)
And that does not evaluate to TRUE or FALSE. So what, you ask? Well, that is the key here. Every condition needs to evaluate to true or false, so you'd be more in luck if you try
for each widget where (if widget-hold# = 'widget.holdn' then widget.holdn = true else TRUE) (...)
Again, notice the condition will exist if widget-hold# has the value I want, otherwise it doesn't filter on this at all.
So you can just code the way I showed (for each of the conditions you have) and it should work fine.
BUT let me suggest a dynamic query instead.
You need to have:
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
CREATE QUERY hQuery.
hQuery:SET-BUFFERS(BUFFER widget:HANDLE).
hQuery:QUERY-PREPARE('<THIS IS THE CORE>').
hQuery:QUERY-OPEN().
DO WHILE hQuery:GET-NEXT():
A bunch of code to make a nice report with the retrieved records...
END.
So in the core you have a string that corresponds to your for each the way you want it to look. So it should be for example (store this in a variable, or assemble it inside the query prepare, it doesn't matter):
'FOR EACH widget NO-LOCK WHERE ' +
(if which-hold = 'B' then 'widget.holdn = true and widget.holdl = true'
else if which-hold = 'L' then 'widget-holdl = true'
else /* N */ 'widget-holdn = true').
Remember I said your query is evaluated at compile time? Well, just so you know, dynamic queries on the other end are evaluated at run time, so be prepared for errors to pop up only when you run. Another thing I should mention is dynamic queries are slower than static ones, so please evaluate and choose your poison :)
This should be what you need. Please let me know if it's helpful or any questions remain.

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.

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

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);

Request.Querystring(variablevalue) possible?

I'm creating an application which uses request.querystring to transfer variables between pages. However at the moment I am setting variable names in the URL dynamically in accordance with an array and I want to retrieve these values on another page using (essentially) the same array. So I came up with what I thought would be the simple solution:
For Each x In AnswerIDs
response.write(x)
ctest = Request.Querystring(x)
response.write(ctest)
Next
However this doesn't seem to work as it looks like you can't use Request.Querystring() with a variable as it's parameter. Has anyone got any idea how I could do this?
This is the query string:
QuizMark.asp?11=1&12=1
In this case AnswerIDs consists of "11" & "12" so in my loop I want it to write "1" & "1".
You can loop through the querystring like this:
For Each Item in Request.QueryString
Response.Write Item & ": " & Request.QueryString(Item) & "<br/>"
Next
Item contains the name of the querystring parameter, with Request.Querystring(Item) you retrieve the value of the parameter.
Your method will work with a simple modification:
For Each x In AnswerIDs
response.write(x)
ctest = Request.Querystring(CStr(x))
response.write(ctest)
Next
If your array might have Null elements, then replace the Cstr(x) with x & "".
If, on the other hand, you can make sure that every element of AnswerIDs has a value (i.e. is neither Empty nor Null), then you can omit the string conversion. In other words, the problem isn't with calling Request.Querystring with a variable, but with calling Request.Querystring with an Empty or a Null.

Resources