seeking correct approach to combine features of linq datasource vs datasourceID

J

jrl

I'm beginning with linq using C#, and I'm looking for the correct approach
to drawing data into a gridview.

In one case, the gridview is using a datasourceID because I want it to
respond to where parameters from a drop down list.

<asp:GridView ID="GridView1" DataSourceID="LinqDataSource1"> (edited for
brevity)

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="PagevisitDataContext" EnableDelete="True"
EnableInsert="True"
EnableUpdate="True" TableName="pagevisits" Where="ip == @ip">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="ip"
PropertyName="SelectedValue" Type="String" />
</WhereParameters>
</asp:LinqDataSource>

This part works fine, and now I want to use buttons to delete the group that
is selected above, or to show all the database entries (rather than filtered
as in the first case). The button to show all looked like the following:

protected void Button1_Click(object sender, EventArgs e)
{
PagevisitDataContext db = new PagevisitDataContext();
var visits = from visit in db.pagevisits
select visit;
GridView1.DataSource = visits;
GridView1.DataBind();
}

This uses the DataSource. But I find I have to choose one or the other
(DatasourceID or DataSource). So, how then could I manage to provide these
two functions? I don't think I can convert the functions under the
Button1_Click so that it uses DatasourceID. And the gridview has to use
DatasourceID in order to link up with the drop down list. So I'm stuck.

I suppose it would be possible to use two gridviews, one useing datasourceID
and the other using DataSource, but I think it would be much better if I
could learn an approach that allows me to operate on the database with one
gridview. What is the recommended approach when one wants both the benefits
of DataSourceID (parameter control displays) as well as DataSource (select
where filtering)?
 
S

Steven Cheng[MSFT]

Hi,

As for the GridView control, its "DataBind" method will always use call the
GetDataSource function which will determine how to obtain the datasource
for bindcing. Here is the code from reflector:

===================
protected virtual IDataSource GetDataSource()
{
if ((!base.DesignMode && this._currentDataSourceValid) &&
(this._currentDataSource != null))
{
return this._currentDataSource;
}
IDataSource source = null;
string dataSourceID = this.DataSourceID;
if (dataSourceID.Length != 0)
{
Control control = DataBoundControlHelper.FindControl(this,
dataSourceID);
if (control == null)
{
throw new
HttpException(SR.GetString("DataControl_DataSourceDoesntExist", new
object[] { this.ID, dataSourceID }));
}
source = control as IDataSource;
if (source == null)
{
throw new
HttpException(SR.GetString("DataControl_DataSourceIDMustBeDataControl", new
object[] { this.ID, dataSourceID }));
}
}
return source;
}
===================

As you can see, as long as the DataSourceID is not an empty string, it will
use it. For your scenario, I suggest you consider programmatically set the
DataSourceID to empty string when you want not to use the DataSource(use
the manually queried record sets). How do you think?

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.







--------------------
 
J

jrl

Steven,

the key part you wrote was:

as long as the DataSourceID is not an empty string, it (the gridview) will
use it (the DataSourceID). For your scenario, I suggest you consider
programmatically set the
DataSourceID to empty string when you want not to use the DataSource


So to summarize my goal again: I want to use the DatasourceID sometimes
(when I bind it to parameters of a drop down list) and to a Datasource at
other times, when I use linq queries.

You recommend that I set DataSourceID to an empty string when I want to
substitute the Datasource as the source for the Gridview.Databind()
function. (I had some trouble understanding you at first, so I hope my
interpretation is what you intended).

Here's my attempt to do what you reccommend:

protected void Button1_Click(object sender, EventArgs e)
{
string temp = GridView1.DataSourceID; // so that I can reset the
name
GridView1.DataSourceID = ""; // set to an empty string as
recommended, now gridview should use datasource instead
PagevisitDataContext db = new PagevisitDataContext();
var visits = from visit in db.pagevisits
select visit;
GridView1.DataSource = visits; // now datasource is set
GridView1.DataBind(); // refresh with new data
GridView1.DataSourceID = temp; // restore the DataSourceID so I can
easily sort and filter
}

When I run my test application, it loads the Gridview normally, using
DataSourceID. As soon as I click button1, (above), I get this error:
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one
definition.

It seems my method of setting the DataSourceID to an empty string, is not
working properly. How would you recommend that I do this so that the
DataSourceID becomes, truly and temporarily, undefined?
 
S

Steven Cheng[MSFT]

Hi Jrl,

I think the exception you mentioned is due to you set the DataSourceID
property back after you call "DataBind" method. At that time, GridView has
a certain flag marked and indicate that a DataSource object is used, if you
set a non empty value to DataSourceID, the runtime will think it illegal.

So far my suggestion is as below:

You can always let the Gridview be set to default DatasourceID in Page's
Load event(this is the one you'll use normally) and in the Button's click
event, just change the DataSourceID to the custom one, do not set it back
after yo call databind. e.g.

-======================
public partial class GridViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSourceID = SqlDataSource1.ID;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable("items");
dt.Columns.Add("id", typeof(long));
dt.Columns.Add("name", typeof(string));

dt.Rows.Add(11, "item11");


GridView1.DataSourceID = string.Empty;
GridView1.DataSource = dt;
GridView1.DataBind();

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

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

--------------------
X-Trace-PostClient-IP: 70.67.40.71
From: "jrl" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: Re: seeking correct approach to combine features of linq
datasource vs datasourceID
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top