Welcome Guest Search | Active Topics | Members | Log In | Register

AdRotator Problem Options · View
MC
Posted: Saturday, October 06, 2007 11:40:22 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 06/10/2007 11:40:22
Date: Sat, 06 Oct 2007 11:40:22 GMT

I have coded an AdRotator to use a Database for it's data source. If I
use a SqlDataSource control it functions as expected, however if I
manually retreive the data using an OleDBDataReader It always omits the
first item in the result set? Code for each solution as below.

The reason I'm don't want to use the Declarative solution is that I
intend to Create a custom control that inherits from AdRotator and I
would then need to retrieve the Advert set programtically.

Regards


MC

------------------- Solution 1 - Declarative -------------------

<asp:Content ID="Content2" ContentPlaceHolderID="cphMainSite"
Runat="Server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AdsDatabase %>"
ProviderName="<%$ ConnectionStrings:AdsDatabase.ProviderName %>"
SelectCommand="SELECT CompanyName AS ImageUrl, CompanyName AS
AlternateText, URL AS NavigateUrl, Impressions, Clicks FROM Sponsors
WHERE BannerAdvert=True" />
<asp:AdRotator ID="AdRotator1" runat="server"
DataSourceID="SqlDataSource1" />
</asp:Content>

------------------- Solution 2 - OleDBReader -------------------

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
using (System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings["AdsDatabase"].ConnectionString))
{
conn.Open();
using (System.Data.OleDb.OleDbCommand comm = new
System.Data.OleDb.OleDbCommand("SELECT CompanyName AS ImageUrl,
CompanyName AS AlternateText, URL AS NavigateUrl, Impressions, Clicks
FROM Sponsors WHERE BannerAdvert=True", conn))
{
AdRotator1.DataSource = comm.ExecuteReader();
AdRotator1.DataBind();
}
}
}
</script>
<asp:Content ID="Content2" ContentPlaceHolderID="cphMainSite"
Runat="Server">
<asp:AdRotator ID="AdRotator1" runat="server" />
</asp:Content>
Steven Cheng[MSFT]
Posted: Monday, October 08, 2007 7:32:33 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 08/10/2007 07:32:33
Date: Mon, 08 Oct 2007 07:32:33 GMT

Hi MC,

From your description, you're using databinding to populate the Adrotator
control but found that if you manually bind DataReader to it, it will
always miss the first record, correct?

I've performed some test and repro this behavior. After some further
research, I found out the cause of the problem. It is because the AdRotator
will try dynamically inspect the datasource item's property list. It will
use the following code(get from reflector).

You can see that for those datasource objects which do not support
"ITypedList.GetItemProperties", it will read a row(the first row) so as to
get all the fields(columns) info. And for datareader, this is exact the
case and the first row is used for getting fields info and not used at the
databinding later.

>>>>>>>>>>>>>>>>
private ArrayList CreateAutoGeneratedFields(IEnumerable dataSource)
{
if (dataSource == null)
{
return null;
}
ArrayList list = new ArrayList();
PropertyDescriptorCollection itemProperties = null;
if (dataSource is ITypedList)
{
itemProperties = ((ITypedList) dataSource).GetItemProperties(new
PropertyDescriptor[0]);
}
if (itemProperties == null)
{
IEnumerator enumerator = dataSource.GetEnumerator();
if (enumerator.MoveNext())
{
object current = enumerator.Current;
if (this.IsBindableType(current.GetType()))
{
throw new
HttpException(SR.GetString("AdRotator_expect_records_with_advertisement_prop
erties", new object[] { this.ID, current.GetType() }));
}
itemProperties = TypeDescriptor.GetProperties(current);
}
}
if ((itemProperties != null) && (itemProperties.Count > 0))
{
foreach (PropertyDescriptor descriptor in itemProperties)
{
if (this.IsBindableType(descriptor.PropertyType))
{
list.Add(descriptor.Name);
}
}
}
return list;
}


<<<<<<<<<<<<<<<<<<<<<<<<<<

I've also found a means to resolve this. If you do not want to directly
return DataSet from ADO.NET query. You can manualy create a DataTable
object to wrapper the records(from DataReader). Here is a sample function
show this:

========================
protected void BindRorator2()
{
string sql = "SELECT [id], [name], [description] FROM [itemtable]";

using (SqlConnection conn = new
SqlConnection(WebConfigurationManager.ConnectionStrings["DatabaseConnectionS
tring"].ConnectionString))
{
conn.Open();

using(SqlCommand comm = new SqlCommand(sql, conn))
{
SqlDataReader reader = comm.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);

this.AdRotator2.DataSource = dt;
this.AdRotator2.DataBind();

reader.Close();

}

}
}
===============================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



>From: MC <mc@community.nospam>
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>Subject: AdRotator Problem
>Date: Sat, 06 Oct 2007 11:40:22 GMT

>
>I have coded an AdRotator to use a Database for it's data source. If I
>use a SqlDataSource control it functions as expected, however if I
>manually retreive the data using an OleDBDataReader It always omits the
>first item in the result set? Code for each solution as below.
>
>The reason I'm don't want to use the Declarative solution is that I
>intend to Create a custom control that inherits from AdRotator and I
>would then need to retrieve the Advert set programtically.
>
>Regards
>
>
>MC
>
>------------------- Solution 1 - Declarative -------------------
>
><asp:Content ID="Content2" ContentPlaceHolderID="cphMainSite"
>Runat="Server">
> <asp:SqlDataSource ID="SqlDataSource1" runat="server"
>ConnectionString="<%$ ConnectionStrings:AdsDatabase %>"
>ProviderName="<%$ ConnectionStrings:AdsDatabase.ProviderName %>"
>SelectCommand="SELECT CompanyName AS ImageUrl, CompanyName AS
>AlternateText, URL AS NavigateUrl, Impressions, Clicks FROM Sponsors
>WHERE BannerAdvert=True" />
> <asp:AdRotator ID="AdRotator1" runat="server"
>DataSourceID="SqlDataSource1" />
></asp:Content>
>
>------------------- Solution 2 - OleDBReader -------------------
>
><script runat="server">
> protected void Page_Load(object sender, EventArgs e)
> {
> using (System.Data.OleDb.OleDbConnection conn = new
>System.Data.OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings["A
dsDatabase"].ConnectionString))
> {
> conn.Open();
> using (System.Data.OleDb.OleDbCommand comm = new
>System.Data.OleDb.OleDbCommand("SELECT CompanyName AS ImageUrl,
>CompanyName AS AlternateText, URL AS NavigateUrl, Impressions, Clicks
>FROM Sponsors WHERE BannerAdvert=True", conn))
> {
> AdRotator1.DataSource = comm.ExecuteReader();
> AdRotator1.DataBind();
> }
> }
> }
></script>
><asp:Content ID="Content2" ContentPlaceHolderID="cphMainSite"
>Runat="Server">
> <asp:AdRotator ID="AdRotator1" runat="server" />
></asp:Content>
>

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.075 seconds.