Showing posts with label databind. Show all posts
Showing posts with label databind. Show all posts

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... ?

Databind for default value in INSERT

Hey guys, how can I databind a default value for my textbox in the <InsertItemTemplate>
I tried an Eval but it obviously didnt't work. The data source has a Selectcommand with ID that I thought I could use in the insertitem, but i guess not.

<asp:FormView ID="FormView2" DataSourceID="SqlDataSource1" runat="server">
<InsertItemTemplate>
Test<br />
<asp:TextBox ID="abc" runat="server" Text='<%# Eval("ID") %>' />
</InsertItemTemplate>
</asp:FormView
How can I do this?
thanks

Hi,

first try Bind instead of Eval in this case. If it's really a default value that you don't want the clients to see it's better to use theInserting eventhandler of the SqlDataSource control.

Grz, Kris.

|||

Actually what I want in this case is to have a default value (like PREFIX in this case) + a databind. I could do it in codebind with .select() and assign find the value from the database and assign "thisIDtextbox" that value"


however, since I've already used the value in the ItemTemplate I thought maybe there is a way to just apply that value in a simple way.

As I said, now I do a sqldatasource.select() procedure and find the value to assign the textbox with. Can I do this in a better way??

<asp:FormViewID="FormView1"runat="server"DataSourceID="SqlDataSource1">

<ItemTemplate>

ThisID:

<asp:LabelID="ReceiverIDLabel"runat="server"Text='<%# Bind("ThisID") %>'></asp:Label><br/></ItemTemplate>

<InsertItemTemplate>

ThisID:

<asp:TextBoxID="ThisIDTextBox"runat="server"Text='PREFIX<%# Bind("ThisID") %>'></asp:TextBox><br/><asp:LinkButtonID="InsertButton"runat="server"CausesValidation="True"CommandName="Insert"Text="Insert"/></InsertItemTemplate>

</asp:FormView>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:database %>"SelectCommand="SELECT [ThisID] FROM [db1]"/>