databinding - datatextfield questions

E

Eric Layman

Hi

I am attempting to bind data to a dropdownlist. Currently the below code
will bind a persons last name just find using the datatextfield. What I
would like to do though, is also be able to bind there First Name as well
into that datatextfield, so that when the drop down list is displayed, they
see a Last Name, First Name, rather than just a persons last name.

cmdname = New SqlClient.SqlCommand("SELECT id, lname, fname from
app_personal", conn)
dtrname = cmdname.ExecuteReader()
ddlname.DataSource = dtrname
ddlname.DataTextField = "lname"
ddlname.DataValueField = "id"

ddlname.DataBind()
dtrname.Close()

Any help would be much appreciated.

Are there any other ways apart from the sql method?..eg: New
SqlClient.SqlCommand("SELECT id, lname, fname, rtrim(fname) + ' ' +
rtrim(lname) as fullname from app_personal", conn)

Pls advise.

Thanks
 
G

Guest

Howdy,

I quess you use vb.net:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If Not IsPostBack Then
PopulatePeople()
End If

End Sub

Private Sub PopulatePeople()

Dim query As String = "select id, lname, fname from app_personal"
Dim table As New DataTable

Using connection As New SqlConnection(ConnectionString)
Dim adapter As New SqlDataAdapter(query, connection)
connection.Open()
adapter.Fill(table)
End Using

' data is retreived from database,
' concatenation is performed locally
table.Columns.Add("fullName", GetType(String), "fname + ' ' + lname")

people.DataValueField = "id"
people.DataTextField = "fullName"
people.DataSource = table
people.DataBind()

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top