How to authorize in xamarin android via mysql database - c#

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;
}

Related

'Unable to connect to any of the specified mysql hosts.' error

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.

Error when run WPF Application in other Computer

First of all, I am new to WPF application.
I have create WPF application login form which require input username and password from the users. This application then will open new form if the username and password matching with the one on sql server database.
private void Login_bt_Click(object sender, RoutedEventArgs e)
{
try
{
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\\isdsvr04\LED\LDEMPDB\MPDB.mdf;Integrated Security=True;Connect Timeout=30");
SqlDataAdapter sda = new SqlDataAdapter("Select * From [Table]
where Username='" + username_tx.Text + "' and Password='" +
password_tx.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
Globals.username = username_tx.Text;
Globals.FullName = dt.Rows[0]["FullName"].ToString();
Globals.userImage = dt.Rows[0]["UserImage"].ToString();
Globals.NickName = dt.Rows[0]["NickName"].ToString();
this.Hide();
Window1 f = new Window1();
f.Show();
}
else
{
MessageBox.Show("Please check your username and password");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException == null ? ex.Message : ex.InnerException.Message);
}
}
'''
This work fine on my PC. However when I run this on other PC, the exception generate error "The systems cannot find the file specified". The sql server database is located on the public server in which all computer can access through it.

How to make a connection with local host database through visual studio 2008 using c#?

I have created a sample database in MySql workbench. I am working on a smart device windows application and i am trying to connect my local host where i have my sample database to the login button i have created on the login form. My coding
private void Login_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("server=localhost;User Id= ;database= ");
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM login where Username = '" + textBox1.Text+ "' and Password = '" + textBox2.Text + "'", conn);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows[0][0].ToString()== "1")
{
MainMenuform ps = new MainMenuform();
ps.Show();
this.Hide();
}
else
{
MessageBox.Show("Please check Username and Password");
}
}
So, when i try the above coding, its giving me an error "Cannot connect to the specified MySql host you specified". My code seems alright but i am still getting this error. Please help.

C# Asp.net can't connect to phpmyadmin

I am building an appliciation that connects with a database. Now the problem is that i get an error when i try to actually connect to the database. *note, I know its not save, its for testing purposes only.
Also, when i go to the url given in string server = "http://185.13.226.246:2222/CMD_DB/"; directly it will give me an error as in the picture below. But if I go to that URL indirectly (so via navigating) its fine.
It might be a very small problem but I can't find why it does not work.
edit: in the picture below there should be a list of available databases.
public class DataConnection
{
private static SqlConnection conn;
SqlCommand cmd = new SqlCommand();
public DataConnection() { }
public static SqlConnection Connection
{
get { return conn; }
}
public void ReadyConnection()
{
string server = "http://185.13.226.246:2222/CMD_DB/";
string database = "nickbbl114_atop";
string uid = "bbl114";
string password = "password010101";
string port = "2222";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PWD=" + password + ";";
conn = new SqlConnection(connectionString);
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
}
catch (Exception ex)
{
throw ex;
}
}
public string Login(string email, string password)
{
string naam;
cmd.CommandText = "Select Voornaam From persoon where email = '" + email + "' and password = '" + password + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
ReadyConnection();
naam = cmd.ExecuteScalar().ToString();
return naam;
}
}
you can create same database for any type of application either it's in JAVA or Dotnet or PHP.It doesn't matter.
First try to create a service layer in asp.net for Database CRUD operations. 1) EmployeeService - API service. 1.Insert a new record in the table. 2.Delete the record. 3.Update the record.
Sample to create service layer using WCF service - http://www.codeproject.com/Articles/254714/Implement-CRUD-operations-using-RESTful-WCF-Servic
The above layer can be used for both asp.net and PHP.
2)Asp.Net application - Call the above service using Jquery from asp.net application
3)PHP application - Call the above service using jquery from asp.net application

Login form for website using web service in asp.net

I am trying to log in to a web service from a website. I have an access database with table USERS (id, user, pass, int admin(1 if it is, 0 if it isn't).
In the web service I have this webmethod:
[WebMethod]
public DataSet login(string u, string p)
{
OleDbConnection CNN = null;
OleDbCommand CMD = null;
string sql = "select * from users where username ='" + u + "' and pass='" + p + "' ";
CNN = new OleDbConnection(conn);
CMD = new OleDbCommand(sql, CNN);
CMD.Connection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(CMD);
DataSet ds = new DataSet();
adapter.Fill(ds, "logged");
CNN.Close();
return ds;
}
And, in the web site I have this code:
protected void Button1_Click(object sender, EventArgs e)
{
db.Service Login = new db.Service();
Login.login(lUser.Text, lPass.Text);
}
So my question is how can I see if the logged user is admin or no ?
I was thinking somehow to read it from the DataSet ds - since it is filled with all the information that I need, but how to do that ?
Thanks,
dnisko
First of all please avoid passing user typed values to the database directly using sql strings. You are open to SQL Injection attacks and it is error prone as well
//Parametrize your following query.
string sql = "select * from users where username ='" + u + "' and pass='" + p + "' ";
Here is an example on how to parametrize OleDbCommand.
Answer to your question:
Your login() method returns a DataSet object, so you need to assign the return vale of login() method to a DataSet.
db.Service Login = new db.Service();
DataSet ds = Login.login(lUser.Text, lPass.Text);
bool isAdmin = false;
//Check if there is a record for the username and password
if(ds.Tables[0].Rows.Count == 1)
{
//now check if user is an admin or not
isAdmin = Convert.ToBoolean(ds.Tables[0].Rows[0]["admin"]);
if(isAdmin)
{
//User is an admin
}
}else{
//User does not exist in the database
}

Categories