Page 1 of 1

Insert/Update/Delete

Posted: Tue 24 Dec 2013 09:50
by Ekki
Hi,

I want to extend my table control to insert, update and delete objects.

To do so I did enable the LinqDataSource flags and added specific event handlers. While the event handler is called for OnSelecting(), their're not called for Inserting, Updating, Deleting.

In the classes created by EntityDeveloper I see partial methods for Insert, Update, Delete. If I provide an implementation for one of them, it's not called either.

Instead I always get the error "The data context used by LinqDataSource must extend DataContext when the Delete, Insert or Update operations are enabled."

Code: Select all

<asp:Content ID="Table" ContentPlaceHolderID="ContentHolder" runat="Server">
    <dx:ASPxGridView ID="LayerView" runat="server" DataSourceID="Layers" AutoGenerateColumns="False" DataSourceForceStandardPaging="True" Width="100%" Theme="Office2010Black" KeyFieldName="Name" OnHtmlRowPrepared="Prepare">
        <Columns>
            <dx:GridViewCommandColumn ShowEditButton="True" ShowNewButtonInHeader="True" VisibleIndex="3">
            </dx:GridViewCommandColumn>
            <dx:GridViewDataTextColumn Caption="Origin" FieldName="Origin" Name="Origin" Visible="False" VisibleIndex="0">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Name" FieldName="Name" Name="Name" VisibleIndex="1" Width="200px">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Description" FieldName="Description" Name="Description" VisibleIndex="2" Width="100%">
            </dx:GridViewDataTextColumn>
        </Columns>
        <SettingsEditing EditFormColumnCount="4" Mode="EditForm">
        </SettingsEditing>
        <SettingsDataSecurity AllowDelete="true" />
    </dx:ASPxGridView>
  <asp:LinqDataSource ID="Layers" runat="server"
      ContextTypeName="IG.Data.GFX.Context" TableName="Layers" EntityTypeName="" OnSelecting="Layer_Selecting"  
      EnableInsert="True" OnInserting="Layer_Inserting"
      EnableDelete="true" OnDeleting="Layer_Deleting"
      EnableUpdate="true" OnUpdating="Layer_Updating">
  </asp:LinqDataSource>
Can you help please?

Thx and Merry X-Mas!

Ekki

Re: Insert/Update/Delete

Posted: Wed 25 Dec 2013 11:19
by MariiaI
LinqDataSource is Microsoft's component and it uses the System.Data.Linq.DataContext class, while LinqConnect (LINQ to PostgreSQL) uses only its own classes (Devart.Data.Linq.DataContext class).
Please use our component - DbLinqDataSource - instead of LinqDataSource. The DbLinqDataSource component can perform deletes/updates/inserts automatically, without extending any methods. Please try using this functionality and inform us about the results.

For more information please refer to http://www.devart.com/linqconnect/docs/ ... nding.html

Re: Insert/Update/Delete

Posted: Wed 25 Dec 2013 15:30
by Ekki
Thanx a lot! I will check it out ASAP. For the time being I'm using an ObjectDataSource. This works and also implements a specific behavior I have to handle: The control will display entries from different databases - a read-only database (Support DB) and a r/w database (Project DB). As the background of these data are different contexts, I believe that are regular Linq DS cannot be used here.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using IG.Data.GFX;

namespace IG.Creator
{
	public class LayerDataSource : ObjectDataSource
	{
		public IEnumerable<Layer> SelectEntities ()
		{
			var tLayers = new List<Layer>();

			foreach (var tLayer in this.Context.GetSupportDatabase().GetLayers())
			{
				tLayer.Origin = DataOrigin.SupportDB;
				tLayers.Add(tLayer);
			}

			foreach (var tLayer in this.Context.GetProjectDatabase().GetLayers())
			{
				tLayer.Origin = DataOrigin.ProjectDB;
				tLayers.Add(tLayer);
			}

			return tLayers.AsEnumerable<Layer>();
		}

		public void InsertEntity (string name, string description)
		{
			var tContext = this.Context.GetProjectDatabase().Context;

			var tQuery = from layer in tContext.Layers
						 where layer.Name.Equals(name)
						 select layer;

			if (tQuery.Count<Layer>() > 0)
				return;

			tContext.Layers.InsertOnSubmit(new Layer() 
			{
				Name = name,
				Description = description
			});

            tContext.SubmitChanges();
		}

		public void UpdateEntity (string name, string description)
		{			
			var tContext = this.Context.GetProjectDatabase().Context;
	
			var tDone = false;

			// Too bad we don't have the previous state...

			var tQuery = from layer in tContext.Layers
				where layer.Name.Equals (name)
                select layer;

			Layer[] tLayers;

			tLayers = tQuery.ToArray<Layer>();
			if (tLayers.Length == 1)
			{
				tLayers[0].Description = description;
				tDone = true;
			}

			if (tDone == false)
			{
				tQuery = from layer in tContext.Layers
						 where layer.Description.Equals(description)
						 select layer;

				tLayers = tQuery.ToArray<Layer>();
				if (tLayers.Length == 1)
				{
					tLayers[0].Name = name;
					tDone = true;
				}
			}

			if (tDone == true)
				tContext.SubmitChanges();
			else
				this.InsertEntity(name, description);
		}

		public void DeleteEntity (string name)
		{
			var tContext = this.Context.GetProjectDatabase().Context;

			var tQuery = from layer in tContext.Layers
				where layer.Name.Equals (name)
                select layer;	

			var tLayers = tQuery.ToArray<Layer>();
			if (tLayers.Length == 1)
			{
				tContext.Layers.DeleteOnSubmit(tLayers[0]);
				tContext.SubmitChanges();
			}
		}
	}
}

Re: Insert/Update/Delete

Posted: Thu 26 Dec 2013 11:28
by Ekki
A short and maybe stupid question... what packages I have to include in my web.config in order to see the Devart components such as this Linq datasource in my VS Toolbox?

Ekki

Re: Insert/Update/Delete

Posted: Thu 26 Dec 2013 14:15
by MariiaI
dotConnect for PostgreSQL components are automatically registered in Toolbox (the PostgreSQL Data tab) when you start the Visual Studio first time after successful installation of dotConnect for PostgreSQL. If automatic registration fails, please do the following steps to install the components manually:
- In Visual Studio open the Toolbox window;
- Add 'PostgreSQL Data' tab by right-clicking the Toolbox window and selecting Add Tab from the shortcut menu;
- Right-click the added tab and select Choose Items... from the shortcut menu;
- In the opened Choose Toolbox Items dialog box switch to the .NET Framework Components tab;
- Select the necessary components the Namespace column of which starts with "Devart";
- Click OK button.

When you add the DbLinqDataSource component to your application, the necessary entries for Devart assemblies are automatically added to the Web.config file.

Please tell us if this helps.

Re: Insert/Update/Delete

Posted: Thu 26 Dec 2013 15:43
by Ekki
Hi MariiaI,

things work as you did write. I'm now getting deeper into Devart's Linq DS. It's very likely that I'll come back with another request for help... Thanks a lot for your support during the X-Mas days which are free--at least here in Germany. CU

Ekki