Tjazz Tjazz - 1 month ago 19
MySQL Question

Database connection not workinh

I am not able to connect to my database. db info: dbname: users, dbtable; user.

I keep getting an error, and dont know how to fix it.

The error I am getting is:


I will enter it here with an edit.


It should be working since i added all the deeden refrences ( i think?) and had a working connection when i configured the database.

here's my code:

(beginners code)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

namespace hpjct
{

public partial class _Default : Page
{
private string conn;
MySqlConnection connect;


protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click1(object sender, EventArgs e)
{
string user = username.Text;
string pass = password.Text;

if (user == "" || pass == "")
{
lbmsg.Text = "Please fill in all information";
lbmsg.ForeColor = System.Drawing.Color.Red;
return;
}
bool r = validate_login(user, pass);

if (r)
{
lbmsg.Text = "Correct";
}
else
{
lbmsg.Text = "False";
}
}

private void db_connection()
{
try
{
conn = "Server=localost;Database=users;Uid=root;Pwd=;";
connect = new MySqlConnection(conn);
connect.Open();
}
catch (MySqlException e)
{
throw;
}
}

private bool validate_login(string user, string pass)
{
db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "SELECT * FROM `user` WHERE username=@username AND password=@password ";
cmd.Parameters.AddWithValue("@username", user);
cmd.Parameters.AddWithValue("@password", pass);
cmd.Connection = connect;
MySqlDataReader login = cmd.ExecuteReader();
if (login.Read())
{
connect.Close();
return true;
}
else
{
connect.Close();
return false;
}
}

private void submit_Click(object sender, EventArgs e)
{

}
}
}


Thx in advance!

Answer

Fixed it! Just use this:

"Server=myServerAddress;Database=myDataBase;Uid=myUsername;P‌​wd=myPassword;"
Comments