Click here to Skip to main content
Sign Up to vote bad
good
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:47

Comments
Krunal Rohit - 12 Dec '12 - 4:50
whats the error ?
ianshack - 12 Dec '12 - 4:54
the displayed text in the combobox is still the empID not the fullname.
choudhary.sumit - 12 Dec '12 - 5:05
post your query which you are using to fetch data and make the EmployeeBindingSource.
VitorHugoGarcia - 12 Dec '12 - 5:34
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 - 12 Dec '12 - 19:04
I got an error if I add an Item to a control that Bind to a datasource.
ianshack - 12 Dec '12 - 19:10
SELECT EmpID, EmpLname, EmpFname, EmpMname, CONCAT(EmpLname, ', ', EmpFname, ', ', EmpMname) AS fullname, `E.CatID` FROM employee
ianshack - 12 Dec '12 - 19:16
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?

3 solutions

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  
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  
Comments
ianshack - 12 Dec '12 - 19:03
Sir, It still display the empid not the fullname.
ujju.1 - 14 Dec '12 - 4:59
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
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 208
1 OriginalGriff 203
2 Abhinav S 168
3 Rohan Leuva 150
4 Abhishek Pant 145
0 Sergey Alexandrovich Kryukov 8,474
1 OriginalGriff 6,714
2 CPallini 3,603
3 Rohan Leuva 2,853
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid