Click here to Skip to main content
15,938,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Could you be so kind and HELP me:

I need to concatenate two strings, columns Nome and Apelido, from the same table, ManutencaoFuncionarios, placing the result in a ComboBox, ComboBoxNomeFuncionario

VB
Imports System.Data
Imports System.Data.SqlClient 
Public Class Teste

    Dim con As SqlConnection
    Dim da As SqlDataAdapter
    Dim connectionString As String = ("Data Source=Server;Initial Catalog=DataBaseName;Persist Security Info=True;User ID=sa;Password=MyPassword")
    Dim DataSetManutencaoFuncionarios As DataSet
    Dim comando As SqlCommand



VB
Sub Colocar_Nomes_Funcionarios()
        con = New SqlConnection(connectionString)
        comando = New SqlCommand
        comando.Connection = con
        comando.CommandType = CommandType.Text
        comando.CommandText = "Select Nome+' ' +Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios"
        da = New SqlDataAdapter
        da.SelectCommand = comando
        DataSetManutencaoFuncionarios = New DataSet
        con.Open()
        da.Fill(DataSetManutencaoFuncionarios, "ManutencaoFuncionarios")
        ComboBoxNomeFuncionario.DataSource = DataSetManutencaoFuncionarios.Tables("ManutencaoFuncionarios")
        ComboBoxNomeFuncionario.DisplayMember = ("Nome")
        con.Close()
        ComboBoxNomeFuncionario.SelectedIndex = -1
    End Sub



VB
Private Sub Teste_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              Colocar_Nomes_Funcionarios()
          End Sub
  End Class
Posted
Updated 6-May-10 5:52am
v2
Comments
Sandeep Mewara 6-May-10 12:59pm    
Couldn't get what you want here. I can understand that English is not your first language, yet could you please try to clarify which strings of DB you want to concatenate?

Thank you so much. The final code is:

VB
Sub Colocar_Nomes_Funcionarios()
        con = New SqlConnection(connectionString)
        comando = New SqlCommand
        comando.Connection = con
        comando.CommandType = CommandType.Text
        comando.CommandText = "Select Nome + ' ' + Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios"
        da = New SqlDataAdapter
        da.SelectCommand = comando
        DataSetManutencaoFuncionarios = New DataSet
        con.Open()
        da.Fill(DataSetManutencaoFuncionarios, "ManutencaoFuncionarios")
        ComboBoxNomeFuncionario.DataSource = DataSetManutencaoFuncionarios.Tables("ManutencaoFuncionarios")
        ComboBoxNomeFuncionario.DisplayMember = ("ComboBoxNomeFuncionario")
        con.Close()
        ComboBoxNomeFuncionario.SelectedIndex = -1
    End Sub
 
Share this answer
 
Your error is simple. The SQL you wrote gives you only one column: "ComboBoxNomeFuncionario". When you set the DisplayMember, you are refering to a column you don't have: Nome,

SQL
comando.CommandText = "Select Nome+' ' +Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios"
da = New SqlDataAdapter        
da.SelectCommand = comando DataSetManutencaoFuncionarios = New DataSet        
con.Open()        
da.Fill(DataSetManutencaoFuncionarios, "ManutencaoFuncionarios")    

ComboBoxNomeFuncionario.DataSource = DataSetManutencaoFuncionarios.Tables("ManutencaoFuncionarios") 

ComboBoxNomeFuncionario.DisplayMember = ("Nome")


Change your SQL to this
Select Nome, Nome + ' ' + Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900