Sunday, February 26, 2012

DataBind/DataSource problem

I have this Code:

myConnection.Open()
CommandText = "SELECT * FROM Students ORDER BY Students.firsName"
myCommand = New SqlCommand(CommandText, myConnection)

chooseStudent.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

'Here is the problem
chooseStudent.DataTextField = "firsName" + "|" + "familyName"

chooseStudent.DataValueField = "firsName"

chooseStudent.DataBind()

In this code "chooseStudent" is a DropDownList which contain all students name.

I want that the value of each option (ListItem) will be the first name, But I want that the text will be "firstName" + "|" + "familyName".
(Like "Brad|Pit", "Gorge|Bush" and so.....)

How to do it?(1) never do a "select * from".. always write down the list of columns you need xplicitly. in your case do a "SELECT students.firstname,students.lastname FROM Students ORDER BY Students.Firstname"

(2)

chooseStudent.DataTextField = "firsName"& "|"& "familyName"

"&" is the concatenation string in vb.net ( i assumed you are using vb.net since you dont have semicolons at the end of tour strings..and if you are using C# then you have even bigger issues to worry about..)

hth|||I'm using VB.NET but your solution not work

I get this Error message:

Exception Details: System.Web.HttpException: DataBinder.Eval: 'System.Data.Common.DbDataRecord' does not contain a property with the name firsName|familyName|||can you post some code... ?