PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET LinqDataSource Adding Row

Reply

LinqDataSource Adding Row

 
Thread Tools Rate Thread
Old 06-05-2008, 10:08 PM   #1
Chuck P
Guest
 
Posts: n/a
Default LinqDataSource Adding Row


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
Old 07-05-2008, 11:18 AM   #2
Wen Yuan Wang [MSFT]
Guest
 
Posts: n/a
Default RE: LinqDataSource Adding Row

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:
msdnmg@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.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/subscript...rt/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

  Reply With Quote
Old 07-05-2008, 02:53 PM   #3
Chuck P
Guest
 
Posts: n/a
Default RE: LinqDataSource Adding Row

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off