Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind an ASP.NET FormView to a single record?

Tags:

The whole purpose of the ASP.NET FormView control is to display a single record at a time. So how do I bind it to a single record? It complains that

Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

I can wrap my object in a list if that's the only solution. In this application, the FormView is inside a user control and I'm retrieving the object in a public method, i.e. I'm not using a data source control. I'm not doing any 2-way data binding here either, I'm only using the FormView in this case to maintain a consistent look.

If I went by the book and bound a FormView to a data source that returned a list of records, would it actually retrieve all the records and just display only the selected record?

Here's what I ended up implementing in a utility class:

public static IEnumerable<T> WrapInEnumerable<T>(T item) {     return new T[] {item}; } 
like image 879
Jamie Ide Avatar asked Jul 13 '09 14:07

Jamie Ide


People also ask

Which are two types of ASP.NET data binding?

There are two types of data-binding in ASP.NET namely, simple data-binding and declarative data binding.

Is FormView data control allows you to add new record within the database?

Definition. Displays the values of a single record from a data source using user-defined templates. The FormView control allows you to edit, delete, and insert records.

What is data binding write a program to bind any one ASP.NET control to database?

This is known as simple data binding or inline data binding. Simple data binding involves attaching any collection (item collection) which implements the IEnumerable interface, or the DataSet and DataTable classes to the DataSource property of the control.

What is an ASP FormView?

The FormView control is used to display a single record at a time from a data source. When you use the FormView control, you create templates to display and edit data-bound values. The templates contain controls, binding expressions, and formatting that define the look and functionality of the form.


1 Answers

As the error message says, the DataSource object must implement IListSource, IEnumerable or IDataSource to work.

If you have an object av type A which do not implement one of the mentioned interfaces then you could as you say wrap your object in a list:

C#

var myDataSource = new List<A> {myObject}; 

VB.NET

Dim myDataSource As List(Of A)(myObject) 
like image 156
Sani Singh Huttunen Avatar answered Oct 05 '22 00:10

Sani Singh Huttunen