0

I'm using freesqldatabase.com to get a database for a college project, so having a remotely accessible database is needed. Unfortunately it does not connect to the database. It connects to their myphpadmin just fine but any other connection fails.

private void Submit_registration(object sender, RoutedEventArgs e)
{
    SqlConnection sqlCon = new SqlConnection(@"Data Source= sql2.freesqldatabase.com; Initial Catalog=dbname; User ID=id;Password=no, Integrated Security= True;");
    sqlCon.Open();

    String query = "INSERT INTO tbluser (ID, username, password) VALUES (@ID, @username, @password)";
    String query2 = "SELECT MAX(ID) FROM tbluser";

    SqlCommand cmd2 = new SqlCommand(query2, sqlCon);
    SqlCommand cmd = new SqlCommand(query, sqlCon);

    int maxId = Convert.ToInt32(cmd2.ExecuteScalar());
    int newmaxid = maxId + 1;

    if (String.Equals(text_password.Password,text_password_confirm.Password))
    {
        cmd.Parameters.AddWithValue("@ID", newmaxid.ToString());
        cmd.Parameters.AddWithValue("@username", textbox_username.Text);
        cmd.Parameters.AddWithValue("@password", text_password.Password);

        cmd.ExecuteNonQuery();
            
        sqlCon.Close();

        MainWindow mainWindow = new MainWindow();
        mainWindow.Show();
        Close();
    }
    else
    {
        MessageBox messageBox = new MessageBox();
        messageBox.Show();

        sqlCon.Close();
    }
}
derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • 3
    Does freesqldatabase.com provide SQL Server databases? It looks like it only provides MySQL databases. You're using SQL Server objects and connection string. – madreflection Oct 20 '20 at 20:03
  • this is very bad, you should use using statement, to ensure that connections are always closed, open the connection inside of a using block, – Nonik Oct 20 '20 at 20:05
  • 1
    What do you mean by "fails"? Do you get a specific error? You need to provide as much relevant information as possible when requesting help. – Daniel Mann Oct 20 '20 at 20:05
  • @Nonik While broadly speaking true, it's also not relevant to the question at hand. There are a lot of issues with this code (such as not using auto-incrementing identity columns), but none of that relates to issues connecting to the database, which is the scope of the question. – Daniel Mann Oct 20 '20 at 20:06
  • @DanielMann true, but i can't help it, also madreflection already provided an answer – Nonik Oct 20 '20 at 20:10
  • 1
    @madreflection pointed out that I'm making an sql connection to a MySQL database... That was the main problem, I misread that freesqldatabase provides an SQL database instead of MySQL. I never believed I was this blind. The error basically says that the connection could not be established. – karolis dedele Oct 20 '20 at 20:14

0 Answers0