Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have a employee table(empID, empLname, empFname, empMname) and I want to concatenate two fields in a LnameComboBox. The displayed text in the combobox is empId not the fullname. What's wrong with the code.
 

Here is my code:

LnameComboBox.DataSource = EmployeeBindingSource
Dim fname As String = "empfname"
            Dim lname As String = "emplname"
            Dim full As String
            full = String.Concat(fname & "," & lname)
            LnameComboBox.DisplayMember = full
            LnameComboBox.ValueMember = "empid"
            EmployeeTableAdapter.Fill(Point_of_saleDataSet11.employee)
 
Thanks...
Posted 11-Dec-12 22:47pm
Comments
Krunal Rohit at 12-Dec-12 4:50am
   
whats the error ?
ianshack at 12-Dec-12 4:54am
   
the displayed text in the combobox is still the empID not the fullname.
choudhary.sumit at 12-Dec-12 5:05am
   
post your query which you are using to fetch data and make the EmployeeBindingSource.
VitorHugoGarcia at 12-Dec-12 5:34am
   
Why don't you use add methos from items like this :
Dim fname As String = "empfname"
Dim lname As String = "emplname"
Dim full As String
full = String.Concat(fname & "," & lname)
 
LnameComboBox.Items.Add(full)
ianshack at 12-Dec-12 19:04pm
   
I got an error if I add an Item to a control that Bind to a datasource.
ianshack at 12-Dec-12 19:10pm
   
SELECT EmpID, EmpLname, EmpFname, EmpMname, CONCAT(EmpLname, ', ', EmpFname, ', ', EmpMname) AS fullname, `E.CatID`
FROM employee
ianshack at 12-Dec-12 19:16pm
   
Sir I made a new Data Table(fullname) in DataSet Designer and have a TableAdapter queries above.
And I bind LnameComboBox to Data Table(fullname) and it displays fullname. Sir is it okey?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this one:
 
Dim ds As New DataSet
ds =  someDatasource
If ds.Tables(0).Rows.Count > 0 Then
  For index As Integer = 0 To ds.Tables(0).Rows.Count - 1
   LnameComboBox.Items.Add(ds.Tables(0).Rows(index).Item("empfname").ToString() _
   & "," & ds.Tables(0).Rows(index).Item("emplname"))
   LnameComboBox.Items(LnameComboBox.Items.Count - 1).Value = _ ds.Tables(0).Rows(index).Item("empid")
                Next
            End If
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this one
 
LnameComboBox.DataSource = EmployeeBindingSource
Dim fname As String = "empfname"
Dim lname As String = "emplname"
Dim full As String = fname & "," & lname 
LnameComboBox.DataTextField= full
LnameComboBox.DataValueField= "empid"
LnameComboBox.DataBind()
  Permalink  
v2
Comments
ianshack at 12-Dec-12 19:03pm
   
Sir, It still display the empid not the fullname.
ujju.1 at 14-Dec-12 4:59am
   
Try this one:
Dim ds As New DataSet
ds = EmployeeBindingSource
If ds.Tables(0).Rows.Count > 0 Then
For index As Integer = 0 To ds.Tables(0).Rows.Count - 1
LnameComboBox.Items.Add(ds.Tables(0).Rows(index).Item("empfname").ToString() & ds.Tables(0).Rows(index).Item("emplname"))
LnameComboBox.Items(LnameComboBox.Items.Count - 1).Value = ds.Tables(0).Rows(index).Item("empid")
Next
End If
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The DisplayMember should be a public property of the object.
E.g:
// In employee.cs
public string DisplayName 
{
  get { return this.LastName + ", " + this.FirstName; }
}
Then, in your view class:
LnameComboBox.DisplayMember = DisplayName; // pseudo code, won't compile.
You can set this in the Properties (using the designer).
 
Hope this helps,
Pablo.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 365
1 Sergey Alexandrovich Kryukov 252
2 OriginalGriff 165
3 George Jonsson 159
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,860
2 CPallini 5,135
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100