Dialog Box when I delete a recipe

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
filou
Posts: 22
Joined: Thu May 03, 2012 11:06 am

Dialog Box when I delete a recipe

Post by filou »

Hi,

When I want to delete a recipe I use this script:
Globals.ProductRecipe.DeleteRecipe(this.ListBox1.SelectedItem.ToString());

Whenever a DialogBox appears automatically to confirm the deletion:
"Delete Recipe, Recipe "xxxx" will be deleted. Do you want to continue? Yes or No
Then it's necessary to confirm.

I would like to know if it is possible to force the confirmation (Yes) by a script?

Thanks
Philippe

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Dialog Box when I delete a recipe

Post by mark.monroe »

The recipes are stored in the database. You should be able to delete the recipes directly out of the database using a script and some SQL. Then you would not get that confirmation message. There is no way to automatically select 'Yes' when it asks you if you want to delete that recipe.
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

filou
Posts: 22
Joined: Thu May 03, 2012 11:06 am

Re: Dialog Box when I delete a recipe

Post by filou »

Hi,

Thanks for the information.
Unfortunately I am not an expert with SQL.
Can you give me an example how to delete a recipe with SQL?
Where can I found more information about SQL and the command?

Thanks
Philippe

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: Dialog Box when I delete a recipe

Post by Edmund »

Hi,

This is not a complete and fully tested function!

1.

If you are going to communicate with the database in the project you will need to add a library for database connection (System.Data.SqlServerCe.dll),
you can download it from here:

http://ftc.beijer.se/files/C125728B003A ... rverCe.zip

Extract the zipfile to a location that you can find.

Go to your iX project and the tab called Project.

Here you have "Referenced Assemblies", click on it and a new window pops up. Hit add and locate the dll file you extracted and add it.


2.

Create a Scripe module that you name Database_Functions.

Paste the following code inside of it.

Code: Select all

//--------------------------------------------------------------
// Press F1 to get help about using script.
// To access an object that is not located in the current class, start the call with Globals.
// When using events and timers be cautious not to generate memoryleaks,
// please see the help for more information.
//---------------------------------------------------------------

namespace Neo.ApplicationFramework.Generated
{
    using System.Windows.Forms;
    using System;
    using System.Drawing;
    using Neo.ApplicationFramework.Tools;
    using Neo.ApplicationFramework.Common.Graphics.Logic;
    using Neo.ApplicationFramework.Controls;
    using Neo.ApplicationFramework.Interfaces;
	using System.Data;
	using System.Data.SqlServerCe;
	using System.IO;
	using System.Reflection;
    
    public partial class Database_Functions
    {
		/// <summary>
		/// Member variable to hold a sql connection.
		/// </summary>
		private SqlCeConnection m_SqlConnection;
		private SqlCeCommand m_Sql_Command;
		
		/// <summary>
		/// Tries to delete the recipe from the database
		/// </summary>
		/// <param name="recipe_handler">The name of the recipe handler</param>
		/// <returns>The name of the recipe</returns>
		public void DeleteRecipe (string recipe_handler, string recipe_name)
		{
			if (ConnectToDataBase())
			{
				// Using try/catch to prevent a crash, should anything go wrong.
				try
				{
					m_Sql_Command = new SqlCeCommand("DELETE FROM "+recipe_handler+" WHERE FieldName = '"+recipe_name+"'", m_SqlConnection);
					m_Sql_Command.ExecuteNonQuery();
				}
				catch (SqlCeException)
				{
					// Catches any exceptions from the database and prevents the 
					// program from terminating unexpectedly.
				}
			}
			
			// Disconnects from the database.
			// This is very important.
			// If you leave connections open, there can be unexpected behaviour,
			// like locks and crashes. There is also a memory cost for every open
			// connection.
			DisconnectFromDataBase();
		}
		

		/// <summary>
		/// Tries to connect to the database.
		/// </summary>
		/// <returns>True if successful, otherwise false.</returns>
		private bool ConnectToDataBase()
		{
			// Checks if there already is an active connection. 
			// If so, it returns true right away.
			if (m_SqlConnection != null && m_SqlConnection.State == ConnectionState.Open)
				return true;

			// Create a new sql connection object.
			m_SqlConnection = new SqlCeConnection();
			// Specify the connectionstring, with the full path to the database.
			m_SqlConnection.ConnectionString = string.Format("data source={0}", Path.Combine(ExcecutingPath, "Database.sdf"));

			// Using a try/catch in case something goes wrong.
			// Should the full path to the database be wrong, it will throw an exception.
			// This prevents the termination of the program.
			try 
			{
				// Tries to open the connection.
				m_SqlConnection.Open();
			}
			catch (SqlCeException)
			{
				// If it went wrong, terminate the connection completely.
				DisconnectFromDataBase();
				// Return false to indicate that the connection couldn't be made.
				return false;
			}

			// Checks the state of the connection, returning true if it is open.
			return m_SqlConnection.State == ConnectionState.Open;
		}

		/// <summary>
		/// Disconnects from the database and disposes the connection.
		/// </summary>
		private void DisconnectFromDataBase()
		{
			// Makes sure that the connection isn't already disposed.
			if (m_SqlConnection != null)
			{
				// Checks to see if the connection is open.
				if(m_SqlConnection.State == ConnectionState.Open)
				{
					// Close the connection.
					m_SqlConnection.Close();
				}
				// Dispose the connection object.
				m_SqlConnection.Dispose();
				// Set it to null, so that it is easy to find out
				// if it is in use or not.
				m_SqlConnection = null;
			}
		}
		
		/// <summary>
		/// Get the path to where the project is excecuting.
		/// This differs if you run the project from the pc
		/// or in the terminal.
		/// </summary>
		private string ExcecutingPath
		{
			get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName); }
		}
    }
}


3.

Create a button on a screen and create a click event for it.
In the event you now call the function Globals.Database_Functions.Delete_Recipe and passes the recipe handler name and recipe name.

Code: Select all


void Button1_Click(System.Object sender, System.EventArgs e)
{
	Globals.Database_Functions.DeleteRecipe("Recipe1", "Full Speed");
}

Hope this will help you!

Best Regards Edmund
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

filou
Posts: 22
Joined: Thu May 03, 2012 11:06 am

Re: Dialog Box when I delete a recipe

Post by filou »

Hi Edmund,

I tried your script and it works perfectly.
Thank you!!!!

Philippe

Ankit
Posts: 5
Joined: Tue Oct 06, 2015 6:19 am

Re: Dialog Box when I delete a recipe

Post by Ankit »

hi edmund,,

i also tried your script and it works perfectly but in this i need to write name of the recipe that i want to delete...

my requirement is to delete all created recipes with help of one button.

can i do that??

help me with this....

thanks.
Ankit
Applicatipn Engineer
Spark Automation

Post Reply