PC Review


Reply
Thread Tools Rate Thread

LinqDataSource Adding Row

 
 
Chuck P
Guest
Posts: n/a
 
      6th May 2008
I have a gridview which I extended to allow adding rows. The extended
gridview hooks into the ObjectDataSource's Selected event. In the event I
add a new row to the List<T> returned by the ObjectDataSource select method.
Thus when the grid gets loaded it now has a empty row, which can be edited.

OnObjectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
Type typeList = e.ReturnValue.GetType(); //List<T> for a select statement
Type typeObj = e.ReturnValue.GetType().GetGenericArguments()[0]; //<T>

object ojb = Activator.CreateInstance(typeObj); //new T

// insert the new T into the list by using InvokeMember on the List<T>
object result = null;
object[] arguments = { 0, ojb };

result = typeList.InvokeMember("Insert", BindingFlags.InvokeMethod,
null, e.ReturnValue, arguments);
}

I went to modify the code to work with a LinqDataSource. Unfortunately,
their is no default constructor for the Objects created by LINQ, so the
CreateInstance method fails. Is their a way I can insert a new row with the
LinqDataSource?

thanks,

 
Reply With Quote
 
 
 
 
Wen Yuan Wang [MSFT]
Guest
Posts: n/a
 
      7th May 2008
Hello Chuck,

According to your description, you want to extent GridView (ASP.net 2.0)
control to support Insert feature, correct? If I misunderstood anything
here, please don't hesitate to correct me.

This is a common issue when developing asp.net application with GridView
control. Due to some reason, GridView doesn't naturally support Insert
feature. It seems your current solution is to insert a new row at the top
of result in DataSource Selected event by Reflection namespace. This should
work fine. But, I'm afraid there may have some performance issue with
Reflection. In my opinion, I'd like to suggest you use a common way by Foot
Template solution if it's possible for you. If you are interested in this
method, please check out the following link.
http://www.aspdotnetfaq.com/Faq/How-...with-SqlDataSo
urce.aspx
[How to easily insert row in GridView with SqlDataSource?]

Anyway, regard to your current solution, could you please let me how do you
modify the code snippet (which you posted in this thread) to work with
LinqDataSource. As far as I know, LinqDataSource's selected event should
return a list collection of TableRow. Each TableRow generated by LINQ has a
default constructor without argument. Thereby, the code snippet you posted
in thread should also works in LinqDatasource without modification. I have
tested the following code on my side. It works fine.
protected void LinqDataSource1_Selected(object sender,
LinqDataSourceStatusEventArgs e)
{
Type typeList = e.Result.GetType(); //List<T> for a select
statement
Type typeObj = e.Result.GetType().GetGenericArguments()[0];
//<T>
object ojb = Activator.CreateInstance(typeObj); //new T
// insert the new T into the list by using InvokeMember on the
List<T>
object result = null;
object[] arguments = { 0, ojb };
result = typeList.InvokeMember("Insert",
BindingFlags.InvokeMethod, null, e.Result, arguments);
}

If the issue still persists on your side, is it possible for you to let me
know the Type of "TypeObj" variable? Please also check your
LinqDataContext.designer.cs file generated by VS IDE in your project if
there is a default Constructor defined in that file.

Please try the above method and let me know the result. Please also feel
free to let me know if there is anything unclear. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
Chuck P
Guest
Posts: n/a
 
      7th May 2008
My object type is dynamically created with the select statement.
select new {a.FirstName, b.LastName}. So it does not have a default
constructor.

LinqDataSource1_Selecting()
{e.Result = linq query returning list<anonymous type>}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
asp:LinqDataSource Ralph Microsoft ASP .NET 3 16th Nov 2009 08:33 PM
LinqDataSource Victor Rodriguez Microsoft ADO .NET 0 14th Mar 2009 02:56 AM
LinqDataSource. Get ID shapper Microsoft ASP .NET 1 23rd Dec 2007 04:35 AM
LinqDataSource. Please, could someone help me out? shapper Microsoft C# .NET 0 10th Nov 2007 12:37 AM
LinqDataSource shapper Microsoft ASP .NET 0 9th Nov 2007 09:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:14 PM.