Hello Experts,
I have down loaded http://www.codeproject.com/Tips/423233/How-to-Connect-to-MySQL-Using-Csharp this example .
of C sharp connection to mysql . It works fine . But My problem is this example explains the connection from form button click.
I want to use class for connection purpose . So while loading a form it will check that connection is made successfull for not .
I am Vfp9 programmer this same i am using in vfp9/mysql from last 5 years . But here on c Sharp I stucked up. Thanks in advance .
regards
anand kulkarni

Dhirendra MisraPosted Jan 7, 2014, 4:15 AM
What I understood from your question that you want to call OpenConnection method from your form load event. If it is so, change access modifier of your method OpenConnection() to internal from private and same for CloseConnection method.
like this : internal bool OpenConnection() { ..... }
Form load event as
Hope this answer your question. If not, please show your code what have you written in button click event.
Thanks
anand kulkarniPosted Jan 8, 2014, 10:30 AM
To the above code I have tried to get some table values . As for the same condition means while loading the from and through the class . Means when I get the successful connection while load the form same time I want pull out mysql table values to text box . I Have tried but after spending some hours I posted it here . Connection class is same as above
Regards
Aanand S. Kulkarni
anand kulkarniPosted Jan 7, 2014, 10:50 AM
Thank you very Much . For your help .
anand kulkarniPosted Jan 7, 2014, 2:49 AM
Thanks for reply , Still I am in confussion . I want to call a class of dbconnection string from form load just take a look at it .
I have created a class which connect the db , Insert , Select , Edit and delete . Every thing is going fine but the problem is Every time I have to click on the butn to then it checks the connection is done or not . upto this it is ok . But What i want is to Call dbconnection at form load event . Please see the class code . It working fine .
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using MySql.Data.MySqlClient;
namespace ConnectCsharpToMysql
{
class DBConnect
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
//Constructor
public DBConnect()
{
Initialize();
}
//Initialize values
private void Initialize()
{ server = "localhost";
database = "mydb";
uid = "myuid";
password = "mypwd";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
Dhirendra MisraPosted Jan 6, 2014, 3:47 AM
You can reuse that code in page load events as well.
For details refer link.
http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C