1

I am copying the code of my program (VS 2008) . I am a beginner , I have created a database test , with 1 table emp in it having 3 fields e_name,e_id,age. After debugging the .cs file I get the error as - does not contain a static 'main' method for suitable entry point. Please could you help me out ?

code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ConnectCsharppToMySQL
{
    public  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 = "test";
            uid = "root";
            password = "";
            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;
            }
        }

        //Insert statement
        public void Insert()
        {
            string query = "INSERT INTO emp (e_name, age) VALUES('Pooja R', '21')";

            //open connection
            if (this.OpenConnection() == true)
            {
                //create command and assign the query and connection from the constructor
                MySqlCommand cmd = new MySqlCommand(query, connection);

                //Execute command
                cmd.ExecuteNonQuery();

                //close connection
                this.CloseConnection();
            }
        }

        //Update statement
        public void Update()
        {
            string query = "UPDATE emp SET e_name='Peachy', age='22' WHERE name='Pooja R'";

            //Open connection
            if (this.OpenConnection() == true)
            {
                //create mysql command
                MySqlCommand cmd = new MySqlCommand();
                //Assign the query using CommandText
                cmd.CommandText = query;
                //Assign the connection using Connection
                cmd.Connection = connection;

                //Execute query
                cmd.ExecuteNonQuery();

                //close connection
                this.CloseConnection();
            }
        }

        /*     //Delete statement
             public void Delete()
             {
                 string query = "DELETE FROM tableinfo WHERE name='John Smith'";

                 if (this.OpenConnection() == true)
                 {
                     MySqlCommand cmd = new MySqlCommand(query, connection);
                     cmd.ExecuteNonQuery();
                     this.CloseConnection();
                 }
             }
     */


        //Select statement
        public List<string>[] Select()
        {
            string query = "SELECT * FROM emp";

            //Create a list to store the result
            List<string>[] list = new List<string>[3];
            list[0] = new List<string>();
            list[1] = new List<string>();
            list[2] = new List<string>();

            //Open connection
            if (this.OpenConnection() == true)
            {
                //Create Command
                MySqlCommand cmd = new MySqlCommand(query, connection);
                //Create a data reader and Execute the command
                MySqlDataReader dataReader = cmd.ExecuteReader();

                //Read the data and store them in the list
                while (dataReader.Read())
                {
                    list[0].Add(dataReader["e_id"] + "");
                    list[1].Add(dataReader["e_name"] + "");
                    list[2].Add(dataReader["age"] + "");
                }

                //close Data Reader
                dataReader.Close();

                //close Connection
                this.CloseConnection();

                //return list to be displayed
                return list;
            }
            else
            {
                return list;
            }
        }


        public static void main(String[] args)
        {
            DBConnect db1 = new DBConnect();
            Console.WriteLine("Initializing"); 
            db1.Initialize();
            Console.WriteLine("Inserting a record into table");
            db1.Insert();
            Console.WriteLine("Updating the record");
            db1.Update();
            Console.WriteLine("Displaying the table :");
            db1.Select();
        }
    }

}

3 Answers 3

2

C# is case sensitive!

main <--- bad

Main <--- fine

Check your main method identifier and just correct it! ;)

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

7 Comments

@PoojaBalkundi Still debugging? This solved your problem? What's going on? :)
On runnin this program , I am not able to see any output, could you please help ?
@PoojaBalkundi i believe you should mark Matías Fidemraizer as an answer. Your program was not running now it is. So you got what you need
@PoojaBalkundi Actually you need to ask answers one by one. If you need further assistance, ask another question.
OKAY , actually on executing the select * from emp command in mySQl cmd , the table has 5 records and all the e_name are Peachy, why is it so ? only 1 record i have inserted , then why 5 are seen with same name ?
|
1

Change:

public static void main(String[] args)

To:

public static void Main(String[] args)

Comments

0

Change the basic code to

public static void Main(String[] args)

The later part defines why you are not able to see anything on your screen

You have written

Console.WriteLine("Inserting a record into table");

which runs on CONSOLE format of DOS format to make it easy for your to understand

Where as I guess you are running GUI program which is a graphic program which runs with forms and mouse

Thank You.

Convert the

 Console.WriteLine("Inserting a record into table");

to

  MessageBox.Show("Inserting a record into table");

Comments

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.