Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Okay i dont know if im going to be able to explain this correctly but i will try

What im trying to do is, On a Successful mysqlconnection on Form1 have a value of True Returned, then be able to use that True value on Form 2

Somthing like

Form1
isconnected = true


Form2 if(isconnected = true){execute query}

But i have no clue on how to do this...
Any help would be appreciated
Posted
Comments
shakil0304003 19-Oct-10 6:05am    
requirement unclear.
Sandeep Mewara 19-Oct-10 7:48am    
Use 'Add Comment' feature to respond to an answer.

Hey the solution you are going to use is not a way.if you want to achieve it you need to follow the concept of Object-Oriented Programming Methodology in which you create a class for your MySQLConnection and then use as many time as you want.
take a look how you will create it.

Create a class and create a static method
C#
public class myConnection
{
    public static MySqlConnection GetConnection()
    {
        string str = "SERVER=localhost;"+"DATABASE=mydatabase;"+"UID=testuser;" + "PASSWORD=testpassword;";
        // Note this String is a Example you need to write your own connection string
        MySqlConnection con = new MySqlConnection(str);
        con.Open();
        return con;
    }
}



if you want to pass value in connection string at runtime then you can use it

C#
class myConnection
  {
      public static string datasource = "";
      public static string initialcatalog = "";
      public static string uid = "";
      public static string pwd= "";
      public static MySqlConnection GetConnection()
      {
          string str = "SERVER='"+datasource+"';DATABASE='"+initialcatalog+"';UID='"+uid+"';PASSWORD='"+pwd+"'";
           MySqlConnection con = new MySqlConnection(str);
          con.Open();
          return con;
      }
  }


and then go to Form where you are going to Enter data for Connection Strng and then look how you will pass value in myConnection Class.
let this code is place in click event of connect button or any where you want as your requirement
MIDL
// passing value from user to MyConnection Class variables.
MyConnection.datasource = txtdatsource.Text;
            MyConnection.initialcatalog = txtdatabase.Text;
            MyConnection.uid = txtuid.Text;
            MyConnection.pwd = txtpwd.Text;





now when you can use
myConnection.GetConnection(); instead of creating a new connection.
when ever you call it it will give you a connection.

Make Once, use More than Once....
 
Share this answer
 
v3
Comments
ShilpaKumari 19-Oct-10 7:39am    
Great Work
Tunacha 19-Oct-10 7:43am    
i dont know how this would work for me as i am trying to make it so the User can enter ther connection details themselves, and When i try to use that code with my custom Connection string it tells me
"An object refrence is required for the non-static field, method, or propertly textbox1.Text
Tunacha 19-Oct-10 8:42am    
Now i just need to figure out how to use this with a Dataset YAY Thankyou for the help
[no name] 22-Oct-10 4:07am    
nice object
is this Web Application or Windows application?
if its web application - use Session variables or Querystrings

if its windows application you can simply declare a public variable in form 1, and read it from form 2.
i.e

in form 1:

public boolean isconnected;


//inside method {
isconected = true;
}


in form 2:
label1.Text = form1.isconnected.tostring();
 
Share this answer
 
Declare a public static variable in Form 1
public static bool isConnect=false;
Do the operation and change the isConnection value

C#
private void Connect()
        {
             //Your code 
            isConnect = true;
        }



Call the value from the Form2

bool connected=Form1.isConnect;

You will get the variable value.Since the variable is declared as static no need to create an instance of the class to access the variable

Hope this will help you :)
 
Share this answer
 
declare that public bool isconnected

declare this way

means declare that value as public so you can acess
 
Share this answer
 
What i have on for1 is this
C#
public bool isConnected;

private void button1_Click(object sender, EventArgs e)
{
    string constring = "Server='"+ServerIPBox.Text+"';user='"+MysqlUsername.Text+"';password='"+MySqlPassword.Text+"';database='"+DBNAME.Text+"';";
    MySqlConnection conn = new MySqlConnection(constring);
    try
    {
        conn.Open();
        this.isConnected = true;

    }
/// ... rest of code here
}

and on Form2 im trying to Call the public bool isConnected but it doesnt seem to be working? any examples or somthing would help as i dont rly undrstand your awnser

Also if i do not close the mysql Connection that Form1 has created will it keep it open till i close it, even if i hide Form1?
 
Share this answer
 
v4
Comments
Toli Cuturicu 19-Oct-10 6:56am    
Don't post fake answers. Edit your original question or post comments instead.
MDNadeemAkhter 19-Oct-10 6:57am    
if you will use this code you will only access boolean value which is eiter Yes or No doesnot get connection.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900