Configuring A MySQL Database Host IP Address In A Textbox Using Window Form

The purpose of this programming tutorial is to provide the flexibility in configuring the IP Address on a textbox outside the interface instead of configuring it inside the source code. This could make it much more easier when running a setup and later change the IP address from the host database workstation if the host IP change in time .

To start,

Create a database named configdb and the database table as testcon (you can change your own database name and table name if you want too).

To begin, first of all,
  1. Install WAMP or XAMPP Server on your computer (I used WampServer).
  2. Open the WAMP Server to access phpMyAdmin and create the database and the table name above.
  3. Name the fields or tuples in the table testcon (testconId for PK (Primary Key ) and testconnection as the first row in the table)

Configure MySQL Connection in Visual Studio 2010 (I used 2012)

  1. Download MySQL Connector 6.05 or 6.07 version and Install it in your computer.
  2. Set your connection in the Studio on the left where it says Data Source and add a New Data Source.
  3. Create a new project and name whatever you like. I have named it Configure_IPAdd_On_TxtBox.
  4. On the Solution Explorer, right-click and add a reference. Go to .NET and look for MySQL.Data DLL files and add it to the system program library.

Now, create a new project from the above step three 3, and add forms to this. So you can see from below are the two form of windows describing the configuration of any IP Addresses for MySQL Database Host depending on your database workstation IP Address.

  1. window GUI below show when the IP address enter is invalid


  2. The GUI below show when the IP address enter is valid


  3. After the successful connection was made, a new pop up window will show. This will indicates that your MySQL database is valid and open for any data transaction. Therefore, we are going to confirm the valid connection by testing an insert query into the MySQL Database.


  4. After the successful testing, below is the result shown in the database.

     
Below are all the codes for the above-displayed window interface.
 
Interface 1 (Form1)
  1. using System.Windows.Forms;  
  2. using MySql.Data.MySqlClient;  
  3. //Using this internal class to access all derive or inheritence class by passing the configure IP Address  
  4. internal class GetConnectionString {  
  5.     public static string InTextBoxSetting {  
  6.         get;  
  7.         set;  
  8.     }  
  9. }  
  10. //Making Connection using the Localhost connection of the MySQL Database  
  11. public string MakingConnectionString(string connection) {  
  12.     connection = "datasource='" + txtIPAddress.Text + "'; database=configdb; username=root; password=;";  
  13.     GetConnectionString.InTextBoxSetting = connection;  
  14.     MySqlConnection conn = new MySqlConnection(GetConnectionString.InTextBoxSetting);  
  15.     conn.Open();  
  16.     //This return the Store IP Address in this internal class as strings and passes it to any classes in this application program  
  17.     return GetConnectionString.InTextBoxSetting;  
  18. }  
  19. //When execute and the connection is valid then the next confirmation GUI will pop up showing that the Host IP is correct otherwise invalid  
  20. private void btnConnect_Click(object sender, EventArgs e) {  
  21.     try {  
  22.         if (this.txtIPAddress.Text == "127.0.0.1") {  
  23.             MakingConnectionString(GetConnectionString.InTextBoxSetting);  
  24.             MessageBox.Show("Database Host Connection succesfully ..");  
  25.             Form2 f2 = new Form2();  
  26.             f2.Show();  
  27.         } else {  
  28.             MessageBox.Show("Connection String Error");  
  29.         }  
  30.     } catch (Exception ex) {  
  31.         MessageBox.Show(ex.Message);  
  32.     }  
  33. }  
Interface 2 (Form2) 
  1. private void btnInsert_Click(object sender, EventArgs e) {  
  2.     //This is to check if the string connection is true and is valid for access the MySQL Database  
  3.     string InsertQuery = "insert into testcon(testconnection)values('" + txtData.Text + "')";  
  4.     MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(Form1.GetConnectionString.InTextBoxSetting);  
  5.     MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(InsertQuery, conn);  
  6.     conn.Open();  
  7.     MySql.Data.MySqlClient.MySqlDataReader dr = cmd.ExecuteReader();  
  8.     dr.Read();  
  9.     MessageBox.Show("Host Connected succesfully to MySql Database");  
  10.     conn.Close();  
  11. }