I have a Xamarin app where you connect to a database. There are no errors in the error list but when I run the code, I get this error: Unable to connect to any of the specified MySQL hosts. I use Xamarin.MySql.Data.
I use the same connection string in a windows forms app and it works totally fine. I wrote it yesterday, it is not an old build.
Here is my code:
MySqlConnection conn = new MySqlConnection("Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;");
await conn.OpenAsync();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE USERNAME = '" + username.Text + "' AND PASSWORD = '" + password.Text + "'", conn);
cmd.Connection = conn;
MySqlDataReader rdr = cmd.ExecuteReader();
if(rdr.HasRows)
{
Toast.MakeText(this, "Successfully Logged In", ToastLength.Short).Show();
}
else
{
Toast.MakeText(this, "Invalid Username/Password", ToastLength.Short).Show();
}
I solved the issue by replacing Xamarin.MySql.Data with MySqlConnector.
It seems Xamarin.Mysql.Data is outdated or something.
Related
I do not really know how to ask this well, but will try to describe.
I have created a mysql command, that should insert data into a table and it does exactly what I want, but if statement reader.Read() jumps into an error saying it failed to create the user, even though the user was created.
Why is this happening?
private void iconButton1_Click(object sender, EventArgs e)
{
//create user in mysql
MySqlConnection conn = new MySqlConnection(#"SERVER=localhost;DATABASE=sklad;UID=root;PASSWORD=;");
conn.Open();
MySqlCommand cmd = new MySqlCommand("INSERT INTO users (username, password, role) VALUES ('" + username.Text + "', '" + password.Text + "', '" + role.Text + "') ", conn);
MySqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
MessageBox.Show("Uživatel vytvořen!", "E-Skladovka.cz - Uživatel vytvořen", MessageBoxButtons.OK);
}
else
{
MessageBox.Show("Failed to create user");
}
reader.Close();
cmd.Dispose();
conn.Close();
}
I tried to search, but as long as I do not know how to ask properly, I can not search properly.
How to authorize in xamarin android via mysql database?
Here code:
MysqlCommand com = new MysqlCommand("SELECT test_log, test_pass FROM login WHERE test_log =#log AND test_pass =#pass", mycon);
mycon.Open();
com.Parameters.AddWithValue("#log", login1.Text);
com.Parameters.AddWithValue("#pass", password1.Text);
MySqlReader dr = com.ExcecuteReader();
download all links and connected MysqlClient. please need help!
How to authorize in xamarin android via mysql database?
I don't know what library are you currently using, but you can have a try of MySQL Plugin. You can add it to your project through Component->Edit Components-> Get More Components->Search MySQL Plugin on the search bar.
And then you can access MySQL using following codes:
public bool loginFromMySQL()
{
MySqlConnection sqlconn=null;
bool loginSuccess=false;
try
{
string connsqlstring = "Server=serverAddress;Port=3306;database=database Name;User Id=userName;Password=user password;charset=utf8";
sqlconn = new MySqlConnection(connsqlstring);
sqlconn.Open();
DataSet tickets = new DataSet();
string userName = "userName";
string password = "password";
string queryString = "select * from login where userName='" + userName + "' and password='" + password + "'";
MySqlDataAdapter adapter = new MySqlDataAdapter(queryString, sqlconn);
adapter.Fill(tickets, "login");
if (tickets.Tables["login"].Rows.Count > 0)
{
//login success
loginSuccess=true;
}
else
{
loginSuccess=false;
}
}
catch (Exception e)
{
Console.Write(e.Message);
}
sqlconn.Close();
return loginSuccess;
}
I'm stuck here again and i don't know where the problem come from !
Well, i have a delete button in c# i wrote the code and everytime i execute it they show a message.box" well deleted" but when i restarted my app i find that nothing has deleted even in my database in sql server ..please tell me what's wrong with my code:
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter reader = new SqlDataAdapter();
sqlCon.Open();
string requete = "DELETE FROM [RECAP] where code_reseau='" + textBox1.Text + "' and gch_dep='" + comboBox2.SelectedText + "' and typ_port='" + comboBox3.SelectedText + "' and mois = " +Form3.mois+" and annee = "+Form3.annee+" ";
cmd = new SqlCommand(requete, sqlCon);
cmd.ExecuteNonQuery();
MessageBox.Show("Supprimé !");
sqlCon.Close();
Are you really sure that the DELETE operation succeeded? you are not checking for that and showing the success message. Rather show the message upon checking like
int recordEffected = cmd.ExecuteNonQuery();
if(recordEffected > 0)
MessageBox.Show("Supprimé !");
else
MessageBox.Show("Maximus Prime !");
Not sure why I get an error every single time i try to connect. I tried so many things. When i type the wrong password while connecting manually it says access not granted or whatever so I know it connects but when it connects I get this weird error.
An unhandled exception of type
'System.Collections.Generic.KeyNotFoundException' occurred in
mscorlib.dll Additional information: The given key was not present in
the dictionary.
For localhost it works when I have this connection string.
server=localhost; port=3306; USER ID=root; password=***; database=database;
but when I change the server user id and password it decides to not work.
The exception is thrown at cn.Open();
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection cn = new MySqlConnection();
cn.ConnectionString = "server=db4free.net ;port=3306;
user=goof; password=pwd; database=s;";
cn.Open();
MySqlCommand cmd = new MySqlCommand("select * from users where
username = '"
+ textBox1.Text + "',
and password = '"
+ textBox2.Text + "'", cn);
MySqlDataReader dr;
dr = cmd.ExecuteReader();
int count = 0;
while(dr.Read())
{
count += 1;
}
if(count == 1)
{
MessageBox.Show("success");
}
else
{
MessageBox.Show("error");
}
}
The problem is the user=goof field should be user id=goof.
Change your connection string into this:
cn.ConnectionString = "server=db4free.net ;port=3306; USER ID=goof; password=pwd; database=s;";
try using "user id" or "uid" instead of just "user" and let us know if its does the job!
I am using Microsoft Visual Studio Community version 2015. There have been many questions on this issue, but my problem was not covered in them completely. Basically i am working in Windows Form Application (C#) with MySql(phpmyadmin - wamp) database. I am creating a simple windows form application to get Input, Update/Delete the data, Display the elements in Data Grid View and Search any record. I am facing problem in Updating the record. Compiler continuously gives me the same error, that is; "Specified Cast is Invalid". My Code is given bellow:
.
private void button_update(object sender, EventArgs e)
{
try
{
MySqlConnection cn = new MySqlConnection("server=localhost;port=3306;database=hyder;uid=root;Encrypt=false;AllowUserVariables=True;Usecompression=True;");
int id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
cn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update record set r_name='" + textBox1.Text + "',r_address='" + textBox2.Text + "',r_phone='" + textBox3.Text + "',r_comment='" + textBox4.Text + "' where r_id='" + id;
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated!");
cn.Close();
}
catch (Exception x) { MessageBox.Show("Error:" + x.Message); }
}
What is the problem with this code ?
Try replacing
int id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
with
int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);