Reputation: 74
using System;
using Xamarin.Forms;
using UIKit;
using MySql.Data.MySqlClient;
namespace test.project
{
public partial class ViewController : UIViewController
{
partial void UIButton197_TouchUpInside(UIButton sender)
{
if (textBoxName.Text == "" || textBoxpasswd.Text == "")
{
var alert = UIAlertController.Create("Fehler", "Bitte geben sie Benutzername und Password ein", UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
PresentViewController(alert, true, null);
// Fehlerüberprüfung wenn leer ist
}
else
{
try
{
MySqlConnection con = new MySqlConnection("Server=localhost;Port=8889;database=app;User Id=root;Password=;charset=utf8");
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
MySqlCommand cmd = new MySqlCommand("INSERT INTO users(id, username, passwd, rank) VALUES(@user, @pass, @rank)", con);
cmd.Parameters.AddWithValue("@user", textBoxName.Text);
cmd.Parameters.AddWithValue("@pass", textBoxpasswd.Text);
cmd.Parameters.AddWithValue("@rank", 0);
cmd.ExecuteNonQuery();
errorLabel.Text = "ausgeführt";
}
else
{
errorLabel.Text = "no connection";
}
}
catch
{
errorLabel.Text = "catch";
}
}
}
protected ViewController(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
}
I try to. connect to the the Database but afte the line con.open the Programm crashes
Upvotes: 0
Views: 60
Reputation: 28629
There is obviously an issue with establishing a connection to the database.
You have defined the server as: Server=localhost
Is this absolutely correct? I highly doubt that you are running a full MySQL server on the device itself? If you are running the server on the device, is the server process running?
What is the execption that the application closes with?
CONNECTION_TIMEOUT
? Probably a firewall issue
CONNECTION_REFUSED
? The MySQL server process is not running.
You should also design your application to be hardened against database failures, maybe wrapping the connection with a try:
try {
connection.open();
} catch (Exception e) {
// TODO handle the connection error properly
System.err.println("An error has occurred.");
e.printStackTrace();
}
Upvotes: 1