Mr.Geeker
Mr.Geeker

Reputation: 445

c sharp return object from function

im facing a problem , when i creat my own class i want to create Mysql connection function and i want to use it inside all my forms i do this

program.cs

using MySql.Data.MySqlClient;
using MySql.Data.Types;
using System.Xml;
using System.IO;
public class testing
    {
        public string fahadt="Hello Class";




        public  void conncting()
        {


            MySqlConnection connection;
            string cs = @"server=localhost;userid=root;password=;database=taxi";
            connection = new MySqlConnection(cs);
            try
            {

                connection.Open();

            }
            catch (MySqlException ex)
            {
               // MessageBox.Show(ex.ToString());
            }

        }

    }

and in my form

private void button7_Click(object sender, EventArgs e)
        {
            testing fahad = new testing();
            try
            {
                dataGridView1.Show();
                fahad.conncting();

                // here is error under fahad.conncting.createcommand();
                MySqlCommand cmd = fahad.conncting.CreateCommand();
                //cmd.CommandText = "SELECT * FROM  ocms_visitors WHERE `id`='"+textBox4.Text+"'";
               // MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
                //textBox4.Text = adap.id;

               // DataSet ds = new DataSet();
               // adap.Fill(ds);

               // dataGridView1.DataSource = ds.Tables[0].DefaultView;
                //MessageBox.Show("Yes Mysql Connection is Working Now  !");
            }
            catch (Exception)
            {
                throw;
            }

        }

i dont know how i can do it im very new = C#

please help me and also i have another q should i use using .... in class and form or Enough in class ?

thanks

Upvotes: 1

Views: 3284

Answers (3)

Slippery Pete
Slippery Pete

Reputation: 3110

I suspect you want to return a connection from your conncting() method?

    public MySqlConnection conncting()
    {
        string cs = @"server=localhost;userid=root;password=;database=taxi";
        MySqlConnection connection = new MySqlConnection(cs);
        connection.Open();
        return connection;
    }

To answer your second question, yes, using using {..} blocks is a good idea for IDisposable instances whenever possible. This includes connections, commands, data adapters, etc. The following might be a reasonable pattern:

    using (MySqlConnection conn = fahad.conncting())
    using (MySqlCommand cmd = new MySqlCommand("select * from table", conn))
    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
    using (DataTable dt = new DataTable())
    {
        da.Fill(dt);
        // do something with datatable
    }

Upvotes: 0

Emmanuel N
Emmanuel N

Reputation: 7449

You need to return MySqlConnection

Modify your function to:

    public  MySqlConnection conncting()
    {


        MySqlConnection connection;
        string cs = @"server=localhost;userid=root;password=;database=taxi";
        connection = new MySqlConnection(cs);
        try
        {

            connection.Open();
            return connection;

        }
        catch (MySqlException ex)
        {
            return null;
           // MessageBox.Show(ex.ToString());
        }

    }

Upvotes: 2

Brian
Brian

Reputation: 5119

To answer your initial question, it looks to me like you have already created the connection you were looking for. Next steps for you would be to learn about the SqlCommand Class.

For reference/edification, try this link, and happy coding!

Upvotes: 0

Related Questions