Create A New Class

Creating a new class Project > Add Class In the new .cs file add the following: The namespace can be a distinct name, or for classes that are part of a main ap you can just use the ap’s namespace name.

Read More

INSERT Queries

INSERT Example Command1.CommandText = @"INSERT INTO myTable ( SomeValueColumn, SomeStringColumn ) Values ( @SomeValue, @SomeString )"; //Strings should be added as parameters to avoid sanatisation risks unless you know they are safe. //Other values can be added as parameters too or inline in the INSERT text. Command1.Parameters.AddWithValue("@SomeValue", 1234); Command1.Parameters.AddWithValue("@SomeString", "Hello"); Command1.ExecuteNonQuery(); INSERT and get Auto […]

Read More

Using Message Boxes

Typical Info Message Box MessageBox.Show("Message Text", "Message Box Title", MessageBoxButtons.OK, MessageBoxIcon.Information); Are you Sure Message Box if ( MessageBox.Show( "Are you sure you want to do this?", "Continue?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation ) == System.Windows.Forms.DialogResult.Yes) {  

Read More

Working With Files

using System.IO; Does file exist? if (File.Exists("c:\\temp.txt")) Delete File try { if (File.Exists("c:\\temp.txt")) File.Delete("c:\\temp1.txt"); } catch (IOException ) { System.Diagnostics.Debug.WriteLine("This file is in use by another application – please close it first"); } or try { if (File.Exists("c:\\temp.txt")) { //If file has read only attribute set clear it so that delete can occur if ((File.GetAttributes("c:\\temp1.txt") […]

Read More

Working With Directories

using System.IO; Does directory exist? (Note paths can include a filename if you wish) if (!Directory.Exists(Path.GetDirectoryName(MyFileName))) Directory.CreateDirectory(Path.GetDirectoryName(MyFileName)); //or String path; path = "C:\\Atest\\"; // '\\'='\' if (!Directory.Exists(path)) //or if (!Directory.Exists("C:\\Atest\\")) // '\\'='\' Create Directory (Note paths can include a filename if you wish) Directory.CreateDirectory(Path.GetDirectoryName(MyFileName)); //or String path; path = "C:\\Atest\\"; // '\\'='\' Directory.CreateDirectory(path); //or Directory.CreateDirectory("C:\\Atest\\"); […]

Read More

Global Classes

You simply use a static class to provide global functionality. Static classes contain only static members and are not instantiated – essentially they are global functions and variables accessed via their class name (and namespace). Issues: There is no constructor. Any class you want to include must be static also An example: public static class MyGlobalClassName { […]

Read More

Methods In Classes

  public void MyMethodName(int MyVariable1) { } <Access Specifier> <Return Type> <Method Name>(Parameter List) { Method Body } Access Specifier Determines the visibility of a variable or a method from another class. 'public' to allow it to be called outside of the class  

Read More

Save File As

using System.IO; Save As Dialog Box //—– NEW FILE DIALOG —– const string MY_FILE_EXTENSION = "txt"; String SaveAsFilename; //If last directory is not valid then default to My Documents if (!Directory.Exists(Path.GetDirectoryName(ApMain.LastFileDirectory))) ApMain.LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //—– SAVE FILE DIALOG BOX —– SaveFileDialog SelectFileDialog = new SaveFileDialog(); SelectFileDialog.Filter = "Comma Separated Values (*." + MY_FILE_EXTENSION + ")|*." […]

Read More

Creating Tables

An Example //***************************************** //***************************************** //********** INITIALISE DATABASE ********** //***************************************** //***************************************** bool InitialiseDatabase() { System.Data.SQLite.SQLiteConnection Connection1 = null; String sTemp; try { //—– OPEN THE DATABASE CONNECTION —– Connection1 = new System.Data.SQLite.SQLiteConnection(“data source=” + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + “\\” + Application.CompanyName + “\\” + Application.ProductName + “\\mydatabasefile.db;Password=” + SQLITE_DATABASE_FILE_PASSWORD); Connection1.Open(); System.Data.SQLite.SQLiteCommand Command1 = new System.Data.SQLite.SQLiteCommand(Connection1); //—– CREATE MY […]

Read More