I'm testing out mysql
and c#
stuff, I am only using a console application and will adapt it to a windows form in the future. There has been an error that has occurred and I can't seem to fix it.
Method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace databaseTest
{
class Program
{
private MySqlConnection connection; //connection property
private string server;
private string database;
private string uid;
private string password;
static void Main(string[] args)
{
while (true)
{
Console.Clear();
Program myProg = new Program();
myProg.Initialize();
Console.WriteLine("Choose item;");
Console.WriteLine("<A> Add Record");
Console.WriteLine("<B> Delete Record");
Console.WriteLine("<C> Update Record");
Console.WriteLine("<D> Show Record");
Console.WriteLine("<E> Count Records");
Console.WriteLine("<F> Search Records");
Console.WriteLine("<X> Exit");
ConsoleKeyInfo myKey = Console.ReadKey();
if (myKey.Key == ConsoleKey.A)
{
myProg.Insert();
}
}
}
private void Initialize()
{
server = "localhost"; //local host (WAMP)
database = "my_db"; //database name
uid = "root"; //database username
password = ""; //database password
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
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:
Console.WriteLine("Cannot connect to server. Contact administrator");
break;
case 1045:
Console.WriteLine("Invalid username/password, please try again");
break;
}
return false;
}
}
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public void Insert()
{
List<databaseOperations.User> user = new List<databaseOperations.User>();
databaseOperations.User x = new databaseOperations.User();
Console.Clear();
Console.WriteLine("---> Insert Record\n");
Console.WriteLine("Enter numeric ID: (***)");
int ID = int.Parse(Console.ReadLine());
Console.WriteLine("Enter first name:");
String firstName = Console.ReadLine();
Console.WriteLine("Enter last name:");
String lastName = Console.ReadLine();
Console.WriteLine("Enter telephone:");
String telephone = Console.ReadLine();
x.ID = ID;
x.firstName = firstName;
x.lastName = lastName;
x.telephone = telephone;
user.Add(x);
databaseOperations.InsertToDatabase(x);
}
}
}
Dll method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace databaseTest
{
class databaseOperations
{
private MySqlConnection connection; //connection property
private string server;
private string database;
private string uid;
private string password;
public class User
{
public int ID { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string telephone { get; set; }
}
private void Initialize()
{
server = "localhost"; //local host (WAMP)
database = "my_db"; //database name
uid = "root"; //database username
password = ""; //database password
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
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:
Console.WriteLine("Cannot connect to server. Contact administrator");
break;
case 1045:
Console.WriteLine("Invalid username/password, please try again");
break;
}
return false;
}
}
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public void InsertToDatabase(int ID, string firstName, string lastName, string telephone)
{
string query = "INSERT INTO tbl_user (ID,First_Name,Last_Name,Telephone) VALUES('" + ID + "', '" + firstName + "','" + lastName + "','" + telephone + "')";
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
this.CloseConnection();
Console.WriteLine("\n -->Record Added - Press enter to continue...");
Console.ReadLine();
}
}
}
}
I'm trying to use the dll method for InsertToDatabase
The error I get is:
Error CS7036 There is no argument given that corresponds to the required formal parameter 'firstName' of 'databaseOperations.InsertToDatabase(int, string, string, string)
If I change
databaseOperations.InsertToDatabase(ID, firstName, lastName, telephone);
I get this error instead:
Severity Code Description Project File Line Error CS0120 An object reference is required for the non-static field, method, or property 'databaseOperations.InsertToDatabase(int, string, string, string)
You haven't created an instance of databaseOperations
. The method databaseOperations.InsertToDatabase
is an instance method.
Also, as you mentioned, you're new to c# - you should name classes with capital first letters (Pascal case) , and instances with lower case first letters (camel case): https://msdn.microsoft.com/en-us/library/x2dbyw72.aspx
I have written the example below assuming you've renamed your databaseOperations
class to DatabaseOperations
:
DatabaseOperations databaseOperations = new DatabaseOperations();
databaseOperations.InsertToDatabase(x.ID, x.firstName, x.lastName, x.telephone);
Note also that your DatabaseOperations
class doesn't currently call Initialize
. You probably need to call this from your constructor, e.g:
class DatabaseOperations
{
public DatabaseOperations()
{
Initialize();
}
// etc.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With