i have a problems regarding on filtering my code in retrieving companyName from its table..when i query here is the result .. please see my code
Sub call_customers(ByVal dgv_customers As DataGridView)
Dim customers_name = From cust In dbcon.Customers _
Select cust.CustomerID, cust.CompanyName Order By CompanyName Ascending
dgv_customers.DataSource = customers_name
End Sub
the display is CustomerID and CompanyName.. yeah its true.. no problem with the code..
and if i only select cust.companyname the result is this ....like this code
Sub call_customers(ByVal dgv_customers As DataGridView)
Dim customers_name = From cust In dbcon.Customers _
Select cust.CompanyName
dgv_customers.DataSource = customers_name
End Sub
the output is this...
enter image description here
no display and it says length.. why? newbie in LINQ sir ..
please help..
Originally, you were giving the grid an object, and it was displaying each of the properties of that object.
Now that you are giving it a string, it is doing the same thing, but now, the only property on the string is the length,
Related
I am setting a datalist which looks like this:
And I have table like this:
+-------------------+
|---ID----|---NAME--|
+-------------------+
|---101---|---CNN---|
|---102---|---BBM---|
+-------------------+
On the datalist I am using ID for listfield. I want show the name directly in the textbox when I choose ID list from listfield.
e.g. : i choose ID 101 in datalist then show "CNN" in textboxname
Private Sub DataList1_Click()
Txtnama.Text = DataList1.ListField = "nama"
End Sub
Problem: the textbox just shows "false".
How do I fix this code.
please help me
thanks
The problem is in the line:
Txtnama.Text = DataList1.ListField = "nama"
The right hand side of this assignment is the expression
DataList1.ListField = "nama"
which is a boolean expression. So the value of the .Text property is set to that boolean value.
Probably what you actually want to do is this:
Txtnama.Text = DataList1.ListField("nama")
so that you select only the nama value from the DataList object.
Overview
This is a homework assignment using LINQ to SQL in a Visual Basic application. It is correct in most ways except that I have a partially broken result. Rather than adding the result of my second query to my listbox, my code adds a weird representation of the query itself. Below is a description of the DB, followed by my output (intended and actual), and finally my code. Please point me toward the broken concept so I can figure out what I am missing. Thanks much.
DB info
I am using two tables, called Members and Payments, from one DB. Members has a primary key called ID and also has the fields first_name and last_name. Payments has a foreign key called Members_Id, which is associated to the Member's primary key; Payments also has the payment values under the column Payments.
Output should be like this
Member name = John Smith
$48.00, 10/20/2005
$44.00, 3/11/2006
But is this instead
Member name = SELECT ([t0].[First_Name] + #p1) + [t0].[Last_Name]
AS [value]FROM[dbo].[Members] AS [t0].[ID] = #p0
$48.00, 10/20/2005
$44.00, 3/11/2006
My VB Code
Public Class FormPaymentsGroup
Private db As New KarateClassesDataContext
Private Sub FormPaymentsGroup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.PaymentsTableAdapter.Fill(Me.KarateDataSet.Payments)
'Group payments by Member_ID (FKey) in the Payments table. (Working fine)
Dim IdQuery = From aMember In db.Payments
Group aMember By aMember.Member_Id
Into MemberPayments = Group
For Each memberID In IdQuery
' Use the passed member_Id to target the row in the Members table and return the first_name & last_name.
' PROBLEM: This only seems to be returning the query itself; not the result.
Dim currMemberID = memberID.Member_Id
Dim nameQuery = From aName In db.Members
Where aName.ID = currMemberID
Select aName.First_Name + " " + aName.Last_Name
Dim currName = nameQuery.ToString ' Load the query result into a portable variable.
LbxMemberPayments.Items.Add("Member name = " & currName) ' PROBLEM: This is where the name SHOULD BE posted to the listbox.
' This is bound to the Members table but directs it based on the above IdQuery.
For Each enteredPayment In memberID.MemberPayments
LbxMemberPayments.Items.Add(vbTab & FormatCurrency(enteredPayment.Amount) & ", " & enteredPayment.Payment_Date)
Next
LbxMemberPayments.Items.Add(vbCr) ' Carriage return formatting
Next
End Sub
End Class
change
Dim currName = nameQuery.ToString
to
Dim currName = nameQuery.FirstOrDefault
I am getting the correct Employee Id in the VarEmpID variable. When I click on delete
It is giving me
Unable to cast object of type 'System.Data.Linq.DataQuery`1[my name space]' to type 'namespace'.
enter code here
Protected Sub radGrid1_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles radGrid1.DeleteCommand
Dim VarEmpId As String = (CType(e.Item, GridDataItem)).OwnerTableView.DataKeyValues(e.Item.ItemIndex)("EmpId").ToString()
Using dc1 As New EmployeesDataClassesDataContext()
Dim EmployeeEntry = (From p In dc1.Employees
Where (p.EmpId = VarEmpId)
Select p)
dc1.Employees.DeleteOnSubmit(EmployeeEntry)
dc1.SubmitChanges()
Dim queryResults = (From queryItem In EmployeeEntry Select queryItem).ToList()
If queryResults.Any Then
radGrid1.DataSource = queryResults
radGrid1.DataBind()
End If
End Using
End Sub
dc1.Employees.DeleteOnSubmit(EmployeeEntry)
That method expects an Employee instance. Instead, you passed in an employee query.
Dim EmployeeEntry = ( query )
This is a query, not an entry. Consider calling Enumerable.First to get the first result of the query, and then deleting that.
Modified added Dim EmployeeEntry = (From p In dc1.Employees Where (p.EmpId = VarEmpId) Select p).singleorDefault() After that commented out the queryresults part and binded data again it solved my problem. – SmilingLily
I am using shared editor templates to render nested collections in my view with the 'EditorFor' HTML helper, similar to the following. I'm not in love with all of these nested partial views but doing it this way names the elements appropriately so that they post back to my controller in the ViewModel without an issue.
How would I sort the order at the most resolute level of the nest? In this case, how would I get "Budget.vbhtml" to display in Year order (descending)?
Thanks in advance!
Top-level view (Organization.vbhtml):
<div id="budgets">
#Html.EditorFor(Function(org) org.OrganizationBudgets))
</div>
OrganizationBudget.vbhtml:
#ModelType CharityMVC.OrganizationBudget
#Html.EditorFor(Function(ob) ob.Budget)
Budget.vbhtml:
#ModelType CharityMVC.Budget
#Model.Year #Html.EditorFor(Function(b) b.Amount)
UPDATE:
It sounds like I should be doing this in my controller when I populate my Model object, but how do I sort the children or children-of-children in a linq query? This is my current code:
Function Edit(ByVal id As Integer) As ActionResult
Dim o As Organization
Dim ovm As OrganizationViewModel
'Load the organization from the database
o = (From org In _db.Organizations _
Where org.Id = id _
Select org).FirstOrDefault()
'Map it to the ViewModel
ovm = AutoMapper.Mapper.Map(Of Organization, OrganizationViewModel)(o)
Return View(ovm)
End Function
My best answer so far:
Multple LINQ queries populating the child properties like so:
Function Edit(ByVal id As Integer) As ActionResult
Dim o As Organization
Dim ovm As OrganizationViewModel
'Load the organization from the database
o = (From org In _db.Organizations _
Where org.Id = id _
Select org).FirstOrDefault()
o.OrganizationBudgets = (From ob In _db.OrganizationBudgets _
Where ob.OrganizationId = o.Id _
Order By ob.Budget.Year Descending _
Select ob).ToList()
'Map it to the ViewModel
ovm = AutoMapper.Mapper.Map(Of Organization, OrganizationViewModel)(o)
Return View(ovm)
End Function
Hey, can I ask you something? I'm using VB6.0 and I have some problem with the connection of my Database through DataControl. I have a table named tblEmployee and the other one is tblPosition then I passed the value of the two tables to two DataControls respectively. How can I then get the value of a certain row of Position field. With my code, my Position field returns only the first row. Here's my code
Private Sub cmdSearchEmployee_Click()
With datEmployee.Recordset
datEmployee.Recordset.Index = "idxid"
datEmployee.Recordset.Seek "=", txtIDNumber.Text
If .NoMatch = True Then
MsgBox ("No Record Found!")
Else
Me.txtLastName.Text = .Fields("lname")
Me.txtFirstName.Text = .Fields("fname")
Me.txtMiddleName.Text = .Fields("mi")
With datPosition.Recordset
Me.txtPosition.Text = .Fields("position")
End With
End If
End With
End Sub
I can't see that you have "passed the value" to your DataControl named datPosition. Could this be the problem? e.g. where you have
With datPosition.Recordset
Me.txtPosition.Text = .Fields("position")
End With
...should be more like this:
With datPosition.Recordset
.Index = "some_index??"
.Seek "=", "some_value??"
Me.txtPosition.Text = .Fields("position")
End With
Also consider using the recordsets' Filter to remove the rows that do not match your criteria then RecordCount to loop through the rows that do match your criteria.
Further consider returning a single recordset by creating a join between tblEmployee and tblPosition, either in SQL code or returning a hierarchical recordset using the MsDataShape with its SHAPE syntax.