0

Im new to C# and I just create a database connection using mysql. Then I need to define a database connection class here this is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace restsurant_pos
{
    class mysqlDbConnect
    {

          public static MySqlConnection GetConnection(){
            string MyConnectionString = "Server=Localhost;Database=pos;Uid=root;Pwd='';";
            MySqlConnection connection = new MySqlConnection(MyConnectionString);
            MySqlCommand cmd;
            connection.Open();

            return connection;

          }

hope it is correct..

then how can I create a object using this class. I just created a as below, but it is getting an error.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace restsurant_pos
{
    public partial class AddCategory : Form
    {

        public AddCategory()
        {
            InitializeComponent();
        }

        private void btn_insertCategory_Click(object sender, EventArgs e)
        {
            string categoryName = txtCategoryName.Text;

            mysqlDbConnect connection = new mysqlDbConnect();

            try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = "INSERT INTO categories(name) VALUES(@categoryName)";
                cmd.Parameters.AddWithValue("categoryName", txtCategoryName.Text);
                cmd.ExecuteNonQuery();
                this.Close();
            }
            catch (Exception)
            {
                throw;
            }

            finally 
            {
                if (connection.State == ConnectionState.Open) {
                    connection.Close();
                }
            }
        }
    }
}

enter image description here

enter image description here

what is the wrong with my code.. pls help

1
  • It looks like you haven't defined cmd. Try putting var or MySqlConnection in front of it Commented Nov 17, 2016 at 12:11

1 Answer 1

2

You haven't providing a type for cmd.

Solve it by declaring it (lazily) as var cmd = connection.CreateCommand() or use a hard coded type.

Sign up to request clarification or add additional context in comments.

7 Comments

yes cmd solved... but connection.CreateCommand(); ,connection.State and connection.Close(); still remaining...
you need to use connection.Open(). Right now you've created a connection, but not opened it.
also, "still remaining" isn't exactly descriptive. What are the errors..?
Error 2 'restsurant_pos.mysqlDbConnect' does not contain a definition for 'CreateCommand' and no extension method 'CreateCommand' accepting a first argument of type 'restsurant_pos.mysqlDbConnect' could be found (are you missing a using directive or an assembly reference?) C:\vb\restsurant_pos\restsurant_pos\AddCategory.cs 29 38 restsurant_pos
Error 3 'restsurant_pos.mysqlDbConnect' does not contain a definition for 'State' and no extension method 'State' accepting a first argument of type 'restsurant_pos.mysqlDbConnect' could be found (are you missing a using directive or an assembly reference?) C:\vb\restsurant_pos\restsurant_pos\AddCategory.cs 42 32 restsurant_pos
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.