Application.Exit();
Monday, May 21, 2018
How To Terminate or Exit Program in C#
When your application opens another form and you want that the user can terminate the program without going back to the main form, this 1-line code will help you.
Sunday, May 20, 2018
How To Make a Borderless Form Movable in C#
If you made your form borderless, you might be wondering how to move it to different location of the screen, then you are at the right blog. Here's how you do it.
First, make a MouseDown event in your form.
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void LoginForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
First, make a MouseDown event in your form.
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void LoginForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
How To Make an Event when Enter Key is Pressed in a TextBox in C#
First, you have to add a KeyPress event just like the picture show below.
private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32(e.KeyChar) == 13)
{
//This is where you will put your code if Enter key is pressed
}
}
How To Check if a String or TextBox is Empty or Null in C#
To check if a string or textbox is empty, simply follow the code snippet below.
if (!string.IsNullOrEmpty(textboxName.Text)
{
//This will if a textbox is not empty
}
if (!string.IsNullOrEmpty(stringName)
{
//This will if a string variable is not empty
}
if (!string.IsNullOrEmpty(textboxName.Text)
{
//This will if a textbox is not empty
}
if (!string.IsNullOrEmpty(stringName)
{
//This will if a string variable is not empty
}
Friday, April 27, 2018
How to Insert Data into Microsoft Access Database Using C#
To insert data to the Microsoft Access database, the code snippet below will do the job.
string fileName = "YourDatabaseFilename";
string password = "TheDatabasePassword";
string tableName = "TableName";
string columnName = "NewColumn";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + fileName +
".accdb; Jet OLEDB:Database Password =" + password + "; Mode=12";
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string query =
"INSERT INTO " + tableName + "(" + columnName +
") VALUES('" + info + "')";
OleDbCommand Command = new OleDbCommand(query, con);
Command.ExecuteNonQuery();
con.Close();
How to Add Column in Microsoft Access Database File using C#
In adding column in Microsoft Access database file using C#, the code snippet below might help you.
using System.Data.OleDb;
string fileName = "YourDatabaseFilename";
string password = "TheDatabasePassword";
string tableName = "TableName";
string columnName = "NewColumn";
string dataType = "Number";
// it could be "LongText", "ShortText", kindly refer to the data types accepted by MS Access
string connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + fileName +
".accdb; Jet OLEDB:Database Password =" + password + "; Mode=12";
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string query = "ALTER TABLE NewTable ADD COLUMN " + columnName + " " + dataType;
OleDbCommand Command = new OleDbCommand(query, con);
Command.ExecuteNonQuery();
con.Close();
using System.Data.OleDb;
string fileName = "YourDatabaseFilename";
string password = "TheDatabasePassword";
string tableName = "TableName";
string columnName = "NewColumn";
string dataType = "Number";
// it could be "LongText", "ShortText", kindly refer to the data types accepted by MS Access
string connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + fileName +
".accdb; Jet OLEDB:Database Password =" + password + "; Mode=12";
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string query = "ALTER TABLE NewTable ADD COLUMN " + columnName + " " + dataType;
OleDbCommand Command = new OleDbCommand(query, con);
Command.ExecuteNonQuery();
con.Close();
How to Add a New Table with Primay Key on a Microsoft Access Database File Using C#
Presented below is the code snippet for adding a new table on a Microsoft Access database file. It will also automatically generate a column named ID as the primary key with autoincrementing integer value.
Pages you may also want to visit:
Creating a Microsoft Access Database File
using System.Data.OleDb;
string fileName = "YourDatabaseFilename";
string password = "TheDatabasePassword";
string tableName = "NewTableName";
string connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + fileName +
".accdb; Jet OLEDB:Database Password =" + password + "; Mode=12";
OleDbConnection con = new OleDbConnection(connectionString);
string connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + fileName +
".accdb; Jet OLEDB:Database Password =" + password + "; Mode=12";
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string query = "CREATE TABLE " + tableName+
"(ID int identity, " + "CONSTRAINT AutoIncrementTest_PrimaryKey PRIMARY KEY (ID))"
"(ID int identity, " + "CONSTRAINT AutoIncrementTest_PrimaryKey PRIMARY KEY (ID))"
OleDbCommand command = new OleDbCommand(query, con);
command.ExecuteNonQuery();
con.Close();
Creating a Microsoft Access Database File
How to Add or Change the Password on a Microsoft Access Database File Using C#
Code snippet for adding or changing the password on a Microsoft Access database file:
using System.Data.OleDb;
string fileName = "YourDatabaseFilename";
string newPassword = "ThisIsTheNewPassword";
string oldPassword = null;
string connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + fileName +
".accdb; Jet OLEDB:Database Password =" + oldpassword + "; Mode=12";
OleDbConnection con = new OleDbConnection(connectionString);
string connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + fileName +
".accdb; Jet OLEDB:Database Password =" + oldpassword + "; Mode=12";
OleDbConnection con = new OleDbConnection(connectionString);
string query = string.Format("ALTER DATABASE PASSWORD [{0}] [{1}]", newPassword, oldPassword);
OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Pages you may also want to visit:
How to Programmatically Create a Microsoft Access Database File Using C#
You might be wondering what to do when you want to create a database but you do not want manually setting it up and you want just the code to do it for you. Well, you are in the right blog. Without further ado, let me share to you this code snippet.
First, you muss add Microsoft ADO Ext. 6.0 (or whatever version you have) for DDL and Security as a reference.
stringfileName = "YourDatabaseFilename";
ADOX.Catalog cat = new ADOX.Catalog();
ADOX.Table table = new ADOX.Table();
try
{
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ".accdb; Jet OLEDB:Engine Type=5");
}
catch (Exception ex)
{
//For Windows Forms Applications
//MessageBox.Show("Database file is not created.");
//MessageBox.Show("Database file is not created.");
//For Console Applications
// Console.WriteLine("Database file is not created.");
}
cat = null;
Pages you may also want to visit:
Creating a Microsoft Access Database File
Adding a Table on a Microsoft Access Database File (with primary key)
Pages you may also want to visit:
Creating a Microsoft Access Database File
Adding a Table on a Microsoft Access Database File (with primary key)
Subscribe to:
Posts (Atom)