Index of the current DataRow in a DataTable - asp.net

For Each dr As myDAL.UsersRow In data
str.AppendLine(dr.UserName)
Next
In the above code, I also need to include the row index so that it's something like
str.AppendLine(dr.IndexNumber & " " & dr.UserName)
How can I achieve this?
P.S. data is not a DataTable but a generic list of myDAL.UsersRow

If data is a List<myDAL.UsersRow> as you suggest, you can use a "for" loop rather than a "for each" loop:
for i = 0 to data.Count - 1
str.AppendLine(i & " " & data[i].UserName)
next
If, however, you're implying that your "data" list is not in the same order as the original DataTable's rows, you might be able to use the DataRowCollection.IndexOf method to locate the row in the original table:
for each dr as myDAL.UsersRow in data
str.AppendLine(dr.Table.Rows.IndexOf(dr) & " " & dr.UserName)
next

Declare a counter variable before the ForEach loop, and increment it inside it.
Dim counter As Integer = 0
For Each dr As myDAL.UsersRow In data
counter = counter + 1
str.AppendLine(counter.ToString() & " " & dr.UserName)
Next

Related

Multiple Where Parameters on EntityDataSource?

I have a gridview that uses an entity datasource to populate itself. Depending on what the user has access to see, I want the gridview to implement a where clause. At the lowest level of access, the user can only see themselves. In order to do this I implement the line of code:
EmployeeEntityDataSource.Where = "it.Person_ID = " + selectQuery.ToString()
This successfully reduces the data in the gridview to the one appropriate user. If the user has the next step in access, they should be able to see themselves plus all the employees that work for them. I have sucessfully created a list of employees Person_IDs and I'm trying to filter my gridview so that if the Person_ID column in the gridview matches one of the Person_IDs in my list it should show up.
I have tried the following bits of code:
1.
For Each employeeID In employeeList
If count2 <> count Then
whereString += "it.Person_ID = " + employeeID.ToString() + " OR "
count2 += 1
Else
whereString += "it.Person_ID = " + employeeID.ToString()
End If
Next
EmployeeEntityDataSource.Where = whereString
Essentially thought I could create a giant where statement with a bunch of ORs but this did not work
2.
EmployeeEntityDataSource.WhereParameters.Add(employeeList)
EmployeeEntityDataSource.Where = "it.Person_ID = #employeeList"
The error I get here says a List(of T) cannot be converted WebControl.Parameter
How do I properly create a WHERE statement that will compare the it.Person_ID of the gridview to each element in my list called employeeList?
I think an In statement should accomplish what you need. Something like this -
string employeeIDs = string.Join(",", employeeList.ToList());
EmployeeEntityDataSource.Where = String.Format("it.Person_ID IN ({0})", employeeIDs);
Or you may have to iterate through your list to create the employeeIDs string, depending on the types we're dealing with here.
Apparently I lied When I said my first bit of code did not work. The issue was that I was not generating the massive OR statement correctly.
From what I remember the statement I was originally generating was it.Column = ID1 OR ID2 OR ID3 and so on.
If the statement created is it.Column = ID1 OR it.Column = ID2 OR it.Column = ID3 and so on, this creates a statement that works properly.
The code that is working in my current project is:
For Each employee In employeeList
If count2 <> count Then
whereString += "it.Person_ID = " + employee.ToString() + " OR "
count2 += 1
Else
whereString += "it.Person_ID = " + employee.ToString()
End If
Next
EmployeeEntityDataSource.Where = whereString

loop through two different html tables

I am searching for the answer for the whole day.
Basically, I have two html tables and a recordset in getrows()
The page is designed so that there are two html tables and I need to generate it, no other choice to re-design the page :(
Here are the html tables. They are horizontally aligned.
table1 table2
row row
row row
row row
and so on...
How can I generate these tables and of course I do not know how many records are in the recordset.
lets assume that there are 3 fields, name, quantity and date. so, there will be two html tables and i get six columns, three of each table. i will not distribute different fields to different tables. so tables are just the same, side by side, with different data on them, not the fields
You can create a "container table" that will have the two side-by-side tables within so that they lay out as you desire. You didn't specify, so I have table 1 with the first half of the data, and table 2 with the second half. (It's a bit easier/more straightforward, but you can do it with only a little extra effort to have row 1 in table 1, row 2 in table 2, row 3 in table 1, row 4 in table 2, etc.)
So, starting from the array of your records obtained via GetRows (arrData in my example below), and assuming your array has them in the order (from left to right) you want them displayed:
Response.Write "<table><tr><td>"
dim intLastRow
intLastRow = ubound(arrData, 2)
dim intBreakPoint
intBreakPoint = fix(intLastRow/2)
dim intSecondStartPoint = intBreakPoint + 1
Response.Write "<table>"
dim i
for i = 0 to intBreakPoint
Response.Write "<tr><td>" & arrData(0, i) & "</td><td>" & arrData(1, i) & "</td><td>" & arrData(2, i) & "</td></tr>"
next
Response.Write "</table>"
if intLastRow > 0 then
Response.Write "<table>"
for i = intSecondStartPoint to intLastRow
Response.Write "<tr><td>" & arrData(0, i) & "</td><td>" & arrData(1, i) & "</td><td>" & arrData(2, i) & "</td></tr>"
next
Response.Write "</table>"
end if
Response.Write "</td></tr></table>"

How to get title of a columnt in TListView control?

All I know is ControlListView function: http://www.autoitscript.com/autoit3/docs/functions/ControlListView.htm
and there is an option GetText with two parameters for row and column but there I can't see anything to get the text of column heading, that is the place where user can click by mouse and for example sort by that column etc.
You could try using _GUICtrlListView_GetColumn. This function returns an array with information about the given ListView column.
Local $aCol = _GUICtrlListView_GetColumn($lvwMsgTable, 0)
ConsoleWrite('Title of column 0: ' & $aCol[5] & #LF)
$aCol = _GUICtrlListView_GetColumn($lvwMsgTable, 1)
ConsoleWrite('Title of column 1: ' & $aCol[5] & #LF)
$aCol = _GUICtrlListView_GetColumn($lvwMsgTable, 2)
ConsoleWrite('Title of column 2: ' & $aCol[5] & #LF)
I don't know if this is working with a TListView though.

Can I inner join two DataTables where tableA.myColumn = tableB.myColumn?

I am parsing an Excel workbook and extracting the data into two DataTables like so:
If SetDBConnect("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath & ";
Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""", True) Then
'Get total dollars table
sql.Append("SELECT * FROM [" & totalDollars & "]")
dt = _dh.GetTable(sql.ToString())
sql.Length = 0
sql.Append("SELECT * FROM [" & totalUnits & "]")
dt_units = _dh.GetTable(sql.ToString())
End If
The two spreadsheets are exactly the same with one difference. In the "Total Dollars" spreadsheet, there is a column with the dollar amounts, where in the "Total Units" spreadsheets, it is instead a column with unit amounts.
I would like to INNER JOIN these two tables WHEN the tableA.UPC = tableB.UPC. Is this possible? I have read about DataSets and DataRelations, but I was wondering if there was a simpler approach?
Thanks!
A simple approach using Linq would be:
var query = from c in dt.AsEnumerable()
join r in dt_units.AsEnumerable()
on c.Filed<string>("UPC") equals r.Field<string>("UPC")
select new {
UPC= r.Field<string>("UPC"),
//and so on.. you pick whatever columns you need from each table
}
Don't vote this up, but wanted to paste a VB .NET version:
Dim query = From c In dt.AsEnumerable() _
Join r In dt_units.AsEnumerable() _
On c.Field(Of String)("UPC") Equals r.Field(Of String)("UPC") _
Select New With
{
.UPC = r.Field(Of String)("UPC")
}

Further problems with counting occurrences of strings in an Array

I am copying a question and answer from elsewhere as it partly goes into what I need but not completely.
In ASP classic, is there a way to count the number of times a string appears in an array of strings and output them based on string and occurrence count?
For example if I have an array which contains the following :
hello
happy
hello
hello
testing
hello
test
happy
The output would be:
hello 4
happy 2
test 1
testing 1
The answer that was given was this:
I'm assuming the language is VBScript (since that's what most people use with classic ASP).
You can use a Dictionary object to keep track of the individual counts:
Function CountValues(pArray)
Dim i, item
Dim dictCounts
Set dictCounts = Server.CreateObject("Scripting.Dictionary")
For i = LBound(pArray) To UBound(pArray)
item = pArray(i)
If Not dictCounts.Exists(item) Then
dictCounts.Add item, 0
End If
dictCounts.Item(item) = dictCounts.Item(item) + 1
Next
Set CountValues = dictCounts
End Function
This is great but I can't work out how to grab the top 2 most used words, display them and be able to put them in their own variable for use elsewhere.
Can anyone help with this?
You can loop through the dictionary object using this method. Inside that loop keep track of the top two keys and their counts in either a new array or two new variables.
You can't sort a Dictionary object in VBScript, so you have to use something else.
My advice is using a disconnected Recordset object to hold the items and their occurrences. Such object natively support sorting and it's pretty easy to use. To achieve this have such function instead:
Function CountValues_Recordset(pArray)
Dim i, item
Dim oRS
Const adVarChar = 200
Const adInteger = 3
Set oRS = CreateObject("ADODB.Recordset")
oRS.Fields.Append "Item", adVarChar, 255
oRS.Fields.Append "Occurrences", adInteger, 255
oRS.Open
For i = LBound(pArray) To UBound(pArray)
item = pArray(i)
oRS.Filter = "Item='" & Replace(item, "'", "''") & "'"
If (oRS.EOF) Then
oRS.AddNew
oRS.Fields("Item").Value = item
oRS.Fields("Occurrences").Value = 1
Else
oRS.Fields("Occurrences").Value = oRS.Fields("Occurrences").Value + 1
End If
oRS.Update
oRS.Filter = ""
Next
oRS.Sort = "Occurrences DESC"
oRS.MoveFirst
Set CountValues_Recordset = oRS
End Function
And using it to achieve the output you want:
Dim myArray, oRS
myArray = Array("happy", "hello", "hello", "testing", "hello", "test", "hello", "happy")
Set oRS = CountValues_Recordset(myArray)
Do Until oRS.EOF
Response.Write(oRS("item") & " " & oRS("Occurrences") & "<br />")
oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing
Don't forget to close and dispose the recordset after using it.

Resources