Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

seeking correct approach to combine features of linq datasource vs datasourceID

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jrl - 02 Feb 2008 23:06 GMT
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)?
Steven Cheng[MSFT] - 04 Feb 2008 07:29 GMT
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.

                                                       

--------------------
>From: "jrl" <jrl@newsgroup.nospam>
>Newsgroups: microsoft.public.dotnet.languages.csharp
>Subject: seeking correct approach to combine features of linq datasource
vs datasourceID

>I'm beginning with linq using C#, and I'm looking for the correct approach
>to drawing data into a gridview.
[quoted text clipped - 40 lines]
>of DataSourceID (parameter control displays) as well as DataSource (select
>where filtering)?
jrl - 05 Feb 2008 05:33 GMT
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?
Steven Cheng[MSFT] - 07 Feb 2008 07:03 GMT
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" <jrl@newsgroup.nospam>
>Newsgroups: microsoft.public.dotnet.languages.csharp
>Subject: Re: seeking correct approach to combine features of linq
datasource vs datasourceID

>Steven,
>
[quoted text clipped - 39 lines]
>working properly. How would you recommend that I do this so that the
>DataSourceID becomes, truly and temporarily, undefined?
jrl - 07 Feb 2008 15:50 GMT
Thanks very much. Your example was very helpful in getting it to work now.

Jrl

> Hi Jrl,
>
[quoted text clipped - 94 lines]
>>working properly. How would you recommend that I do this so that the
>>DataSourceID becomes, truly and temporarily, undefined?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.