For a project i'm trying to make a dynamic query to a datatable field.
Let me explain what i'm doing.
Step 1. Read database file with OLEDB and create snapshot to datatable.
Step 2. Create dynamically checkboxes that represents the field names from the datatable.
Step 3. User can create query by selecting the checkboxes. Thus filtering the result.
The problem that i'm having is that i don't know upfront how many and what name's the Fields/Column are getting in the datatable. It is no problem making the query if i fill in the Field names hardcoded:
MyDataview = (From p In MyDataTable _
Where p.Field(Of Boolean)("customer") = True And _
p.Field(Of Boolean)("customer_ID") = True
Select p).AsDataView
However this would be a problem in the future as the database therefore datatable could change and expand or shrink.
So in SQL syntax this wouldn't be any problem. Simple append the field name that correspondent with the checkbox to a Where clause and where good to go.
This is exactly what i'm trying to achieve. I have searched for many hours and tried allot with system.linq.dynamic but i can't get it to work. example:
Dim valueQuery = MyDataTable.AsEnumerable.Where("customer").[Select]("customer")
causes exception:
No property or field 'customer' exists in type 'DataRow'
Or
Dim valueQuery = MyDataTable.AsEnumerable.Where("Field=customer").[Select]("customer")
causes exception:
No property or field 'customer' exists in type 'DataRow'
Related
I've tried to find a solution to this, but can't seem to find anything that quite matches my goal here. The answers I've found are either C# or are wanting the data to go from the DGV to the DT.
I have a datatable, which is built out using an SQL query. In this data table, I have a column "Lab ID" that I want to use as a search index. On my front end, there is a requirement for the Lab ID to be supplied. When the Lab ID is entered, the data table should be searched, and the resulting row (entire row) should be added to the datagridview. Each scan should add a row and not overwrite. I have specified the DGV columns at form load but would like it to grab the column names from the table if possible.
Here is my current front end - noting that the left datagridview is only temporary to confirm the data is coming out of SQL and references the same datatable I am wanting to use for the right DGV. The user will pick the dates, enter the Lab ID, then click Check. If the Lab ID checks out, it should be added.
The other thing I'll need to able to do is compare the results in the data table with what is in that datagridview - if an item in the table is missing in the datagridview, I need to be able to retrieve the missing Lab IDs (don't need the whole row for this one) and add those missing Lab IDs to a messagebox.
I don't have any code at this point because I've not found anything that works, with the exception of this:
dgScanned.Rows.Add(GetResults.Rows)
Which results in the string 'System.Data.DataRowCollection' being added to the 'Request ID' column.
I am using Visual Studio and have an Access database with a couple of tables that are related. They are using default drag/drop binding using WinForms to the dataset which was automatically generated as well.
I want the parent table to include a column with the count of the number of related records with that ID in the results. The tables look like this if it matters:
I added an additional query to the table adapter (as you can see from the screenshot) that does what it needs to do (without including the linked ID's) thinking that was how to do it. Am I at least on the right track? The query looks like this:
SELECT COUNT(*) AS WorkerCount
FROM (tblJobLaborTech INNER JOIN tblJobLabor ON tblJobLaborTech.JobLaborID = tblJobLabor.JobLaborID)
WHERE (tblJobLabor.JobLaborID = 494)
And of course I need to change 494 to something variable which will match the corresponding record when it runs, but
WHERE (tblJobLabor.JobLaborID = tblJobLaborTech.tblJobLaborTechID)
doesnt seem to preview at all (no value given for parameter)
so i also tried
WHERE (tblJobLabor.JobLaborID = #JLID)
which also doesnt preview.
But that's only the start of the problem. Let's say I can get that part working, how do I get the result of that Scalar query to run for each record in the calling query and return in the dataset as if it were another field in that table?
You don't need to do this using the DB. A datatable can count the records present along a child relation
In your tblJobLabor add another datacolumn (right click, add>>column) and set its Expression property to:
Count(Child(XXX).JobLaborID)
Replace XXX with the name of this DataRelation I've highlighted:
More information can be found by looking at the fairly lengthy documentation on DataColumn.Expression at https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression?view=netframework-4.8
I have a DataGridView in a form which displays a client list with the columns ID, First Name, Last Name, Address. I also included a TextBox to perform the search query. I want to filter my DataGridView based from the given columns using a single TextBox (like a multi data filter/search where I can select from those four columns by typing on a single TextBox).
The question is: am I required to create a binding source for here (I populate my DataGridView using sql database) or is there a way to create filters without having to add a binding source?
You don't populate your grid using a sql database. You populate your grid from a SQL Server database suing something else in between, e.g. a SqlDataAdapter and a DataTable.
A BindingSource is supposed to be a one-stop shop for working with bound data so, while you don't have to use one, I would recommend doing so. Whether it can help you filter your data depends on what it's bound to. The BindingSource doesn't actually do the work of filtering itself but rather passes the work on to the underlying IBindingListView implementation if there is one. For instance, if the underlying data source is a DataTable then the RowFilter of its DefaultView will be used. You could set the DefaultView.RowFilter yourself.
If you want to do all the heavy lifting yourself then you could also search the grid yourself and then hide the rows that don't match. Those rows would still exist though, so you'd have to take that into account when using the data in code.
You have to create a query to retrive a data using like keyword by passing parameter value in form
Example:
pass the parameter from
form(cmd.parameters.add(nvarchar, 20).value = "textbox.text")
create a stored procedure
CREATE PROCEDURE procedure_name
#searchvalue varchar(20)
AS
BEGIN
SELECT
ID, FirstName, LastName, Address
FROM
tablename
WHERE
FirstName LIKE '%#SearchTerm%'
END
This above stored procedure will retrieve all the rows where the FirstName column value corresponds to that name
Then pass the result to gridview
Im trying to display a dropdown with values from table 'Events'. I have created a Model.edmx which has the structure of the database. Now i have to just write LINQ code to display one column values. I am kind of new to LINQ.
Dim Events=" LINQ select statement part???"
ddlEvent.DataSource = Events
ddlEvent.DataBind()
ddlEvent.Items.Insert(0, New ListItem("-Type-", ""))
DropDownLists work with 2 fields:
the DataTextField represents the field holding the text of the options that will be displayed to the user
the DataValueField represents the field hoding the value associated with each option
So in your case, you could get it to work with something like:
Dim events As List(Of Event) = yourDbContext.Events.ToList()
ddlEvent.DataSource = events
ddlEvent.DataTextField = "NameOfThePropertyYouWantDisplayed"
ddlEvent.DataValueField = "NameOfThePropertyYouWantAsValue"
ddlEvent.DataBind()
Note, however, that the query will retrieve every property from the database. This can hurt performance if your Event class has a lot of properties.
To avoid this, you could use the LINQ Select operator, used for projection. This could be used to retrieve only the necesseray data from the database. You can have a look on the MSDN here
Context.Events.ToList();
Where, Context is your EF db context. Update your filter condition in where clause
ddlEvent.DataSource = from x in Context.Events select x.column;
here 'x' is a variable and 'column' in 'x.column' is required column you want to fetch.
Where, Context is your EF db context.
In my VB.Net desktop application, I have several places where I am accessing a table, both for reading and for updating. For example:
Dim tempCount As Integer = Glbl.GlobalDataSet.Tables("Profiles").Rows.Count
The above does not work. However, the following does:
Dim tempCount As Integer = Glbl.GlobalDataSet.Tables(4).Rows.Count
I am pretty sure it was working in th past (I have recently switched to VS 2010 - could this have anything to do with it?) but it is not working now.
How can I make the first statement work, so can access tables by table name instead of by index?
It should be working, make sure that the name of the DataTable is correct. Use the property window to check then rebuild the project.
Make sure that the table name is assigned to the tables 4 in your dataset, if not before writing the first code part in your question change the table name like
Glbl.GlobalDataSet.Tables(4).TableName = "Profiles"
after this try getting the count of the table using table name, or while filling the database values from database to the dataset you fill it by specifying the table name.
DAdapter.Fill(Glbl.GlobalDataSet.Tables("Profiles"))