I am trying to populate a dropdownlist with queried data based on the selection of another dropdownlist. My first query returns a list of IDs using the following code:
Dim selectQuery = (From train In trainerEntities.TrainerTrainingCompanies
Where train.TrainingCompanyID = trainingCompany.ID
Select train.TrainerID).ToList()
I then create an arraylist and a second query
Dim trainers As New ArrayList
For Each trainerID In selectQuery
Dim selectQuery2 = (From pers In trainerEntities.Persons
Where pers.Person_ID = trainerID
Select pers.Person_ID, pers.Last_Name).ToList()
trainers.Add(selectQuery2)
Next
Finally I use the arraylist as a datasource and bind the data
TrainerDropDownList.DataSource = trainers
TrainerDropDownList.DataBind()
The list populates, but not with any data. It looks like this:
System.Collections.Gerneric.List'1[VB$AnonymousType_0'2[System.Int32,System.String]]
When debugging and looking at the array list I see the proper amount of items and the proper information in each specific item. Looking at what the list is populated with from above I am sure the Int32 value is the Person_ID and what I want to use for the dropdownlist value field and the String value is the Last_Name and what I want to used for the dropdownlist text field.
Using:
dropdownlist.DataTextField("Last_Name")
dropdownlist.DataValueField("Person_ID")
produces and error on databind.
does not contain a property with the name 'trainers.Last_Name'
How do I properly use this arraylist as my datasource for my dropdownlist
I rethought how I was gathering the data on selectQuery2. Instead of looping through the results, I altered the "where" clause of the LINQ query to what I believe is a called a lambda expression. This allows me to use my select query and match the person IDs.
Dim selectQuery2 = (From pers In trainerEntities.Persons
Where selectQuery.Any(Function(x) pers.Person_ID.Equals(x))
Select pers.Person_ID, pers.Last_Name).ToList()
The where clause now essentially says select where Person_ID = any of the original selectQuery results.
From here selectQuery2 can be used as the data source with Last_Name and Person_ID as the text and value fields.
TrainerDropDownList.DataSource = selectQuery2
TrainerDropDownList.DataTextField = "Last_Name"
TrainerDropDownList.DataValueField = "Person_ID"
TrainerDropDownList.DataBind()
Related
Say you had a table in SQL and you wanted to find all the rows where a particular column could be one of 3 options. You could write something like this:
SELECT * FROM MyTable WHERE uid IN (123, 456, 789)
That's effectively what I want to do with a DataTable in VB. I saw this post:
Query rows that the datatable field contain any item in the list<string>
Which seemed to be exactly what I wanted, but it's not working as I expected it to. I basically just used a C# -> VB converter and plugged in my variables and came up with this:
Dim item = From a In dtCodes.AsEnumerable() Where
lstCodes.Any(Function(x) a.Field(Of String)
("Code").ToUpper().Contains(x.ToUpper())) Select a
dtCodes is a DataTable with a column Codes in it. lstCodes is a List(Of String) with some values in it.
item just has all of the rows from dtCodes though, regardless of whether or not their Code column value exists in lstCodes.
What am I missing?
edit; Note that I don't have to use LINQ, if there's an easier or better way.
In the past, I've done this sort of like this:
Dim item = From r as DataRow in dtCodes.Rows
Where lstCodes.contains(r.Item("Codes"))
Select r
Does that work?
The code you have looks ok to me. I tried out this test and it (correctly) prints out
a2
b1
Dim dtCodes As New DataTable
dtCodes.Columns.Add(New DataColumn("Code"))
dtCodes.Rows.Add("a1")
dtCodes.Rows.Add("a2")
dtCodes.Rows.Add("b1")
dtCodes.Rows.Add("b2")
Dim lstCodes As New List(Of String)
lstCodes.Add("b1")
lstCodes.Add("c1")
lstCodes.Add("a2")
Dim item = From a In dtCodes.AsEnumerable()
Where lstCodes.Any(Function(x) a.Field(Of String)("Code").ToUpper().Contains(x.ToUpper()))
Select a
For Each itm In item
Debug.WriteLine(itm("Code").ToString)
Next
Here's a lamba expression that should also work.
Dim item = dtCodes.Select.Where(Function(x As DataRow) lstCodes.Contains(x.Item("Codes")))
What i want to do is te following... i have a gridview that loads the data from a SQL server, i have the e.NewValue and e.oldValue to determine which data was being modified/updated but i need to get the unique record ID which is store in "ID_ControlCharts" column
So, can you provide a code snippet/brief explanation in how to get the value from "ID_controlCharts" already filled by the gridview when i updated 1 cell (e.new/old)???
Note: The index is not what i needed since the row can be deleted and the reference to that row is now lost, so since the "ID_controlCharts" is filled automatically by SQL server key, its perfect for my situation.
codebehind
If e.Equals("Control_SeqRef_CH") = False Then
Dim oldvalue As String = e.OldValues("Control_SeqRef_CH")
Dim newvalue As String = e.NewValues("Control_SeqRef_CH")
Dim rowindex As Integer = e.RowIndex
Dim IDfromcontrol as integer = ???
MsgBox(oldvalue)
MsgBox(newvalue)
MsgBox(rowindex)
Msgbox(IDfromcontrol)
Else
MsgBox("same")
End If
You have a couple of options:
If the ID_ControlCharts column is in the record, couldn't you fetch it from e.OldValues?
Dim IDfromcontrol = e.OldValues("ID_ControlCharts")
Failing that, if you set ID_ControlCharts as a column for the DataKeyNames property, you should be able to fetch it using the ItemIndex:
Dim IDfromcontrol = GridView1.DataKeys(e.ItemIndex).Values(0)
(This is all from memory, so the syntax may be a little off.)
I want to search record based on following fields
P_num ,P_name, P_dob,P_code
I am using more than 4 tables to retrive data,
I have created dataset called P_search and i want to fill data into dataset and thsn display it according to which field is selected for search purpose.
Suggest me to write code by creating own function in which all fields and a boolean variable will be taken as parameters
Please help me to write code using VB.net.
I am very new to VB.net. Is ther anyone who can help me giving above code.
Thank You In advance
You use DataTable.Select Method
Private Sub GetRowsByFilter()
Dim table As DataTable = DataSet1.Tables("Orders")
' Presuming the DataTable has a column named Date.
Dim expression As String
expression = "Date > #1/1/00#"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)
Dim i As Integer
' Print column 0 of each returned row.
For i = 0 to foundRows.GetUpperBound(0)
Console.WriteLine(foundRows(i)(0))
Next i
End Sub
I have a datatable which has been dynamically generated from FoxPro tables using a UNION Select statement. e.g.
SELECT * FROM x UNION SELECT * FROM y UNION SELECT * FROM Z ORDER By v_alue1
This produces a datatable with about 100 rows, each containing many fields, one of which is c_olor. From this datatable, I would like to select the distinct colors and then output in a dropdown.
I have a public class Color which just has one property which I can then use as the DataTextField and DataValueField for the dropdownlist
Public Class Color
Private _c_olor As String
Public Property c_olor() As String
Get
Return _c_olor
End Get
Set(ByVal value As String)
_c_olor = value
End Set
End Property
End Class
My linq statment is
Dim colorDs = (From o In dt.Rows Select Color = New With {.c_olor = o("c_olor").ToString().Trim(Nothing).ToLower()}).Distinct().ToList()
However this never results in the distinct colors.
I have searched and searched for what I am looking for, and this seems to be one of the methods to produce a distinct set of results, but this and the others do not work.
My reasoning behind getting the colors this way, is that I need to get various other distinct values from the same UNION SELECT datasource, so would just do one DB call, cache the results, and then just used this cached datasource to retrieve all my distinct values.
I'm not a VB expert but it seems like you're creating your colors first and then trying to find the distinct colors
In this case, you need to implement Equals on your color class
Dim colorDs = (From o In dt.Rows Select Color = New With {.c_olor = o("c_olor").ToString().Trim(Nothing).ToLower()}).Distinct().ToList()
Alternatively, you could find disctinct values first and then create color instances
Dim colorDs = (From o In dt.Rows Select o("c_olor").ToString().Trim(Nothing).ToLower()).Distinct().ToList()
Should produce the list of unique string colors
I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains some records of a table in the data base.
So I did this :
DDL.DataSource = myDataReader
DDL.DataBind()
But it's giving me (5 records) "the number of records of the table" but like this :
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
You should set DataTextField and DataValueField, otherwise data binding will perform .ToString() on every row and put it as item:
DDL.DataSource = myDataReader;
DDL.DataTextField = "[Text column name]";
DDL.DataValueField = "[Value column name]";
DDL.DataBind();
you have to set the text and the key fields of the ddl before you databind
DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":
The code : ddl.datasource=reader is just setting the content present in reader (array of columns of table) as the main source of data.Now as ddl show only a single column in it so u need to write a piece of code which tells ddl that which column it has to display.So you will write: ddl.textfield="column name which you want to show";
and ddl.valuefield="column name which you want as a reference to pass to database";