Reputation: 6741
It has been so long since I've worked with webforms I've forgot how to gain access to the underlying data to get the column value so that I can set the current request for a specific product to the page title.
I've got a detailsview on a webforms page and I need to get the value of a column named Title.
How should I go about doing this?
I'm sure it is because I'm in the Page_Load event.
Any suggestions?
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = GetDS(this.ObjectDataSourceItem);
if (ds != null)
{
string title = ds.Tables[0].Columns["Title"].ToString();
this.Page.Title = title;
}
}
private DataSet GetDS(ObjectDataSource ods)
{
DataSet ds = new DataSet();
DataView dv = (DataView)ods.Select();
if (dv != null && dv.Count > 0)
{
DataTable dt = dv.ToTable();
ds.Tables.Add(dt);
}
return ds;
}
My error is Invalid cast exception.
Here is a snapshot of my error:
The correct way to gain access to the underlying object, my custom object name Item.
private Item GetItem(ObjectDataSource ods)
{
Item itm = null;
object[] iObjs = (object[])ods.Select();
for (int i = 0; i < iObjs.Length; i++)
{
itm = (Item)iObjs[i];
}
return itm;
}
Upvotes: 2
Views: 3031
Reputation: 1591
From MSDN:
ObjectDataSource.Select
doesn't have to return a DataView
type. From the method signature, it returns an IEnumerable
. From your debugger screenshot, you can see that it is an array of objects. You just need to iterate over the array to get to the value buried underneath.
Upvotes: 1