1

I've been messing around trying to make a signup page in Visual Studio, and I can't seem to get it to work.

  • Database name = User
  • Columns which data to be inserted into = Username & Password

When I execute the code, it returns the message "Data Saved" which would lead me to believe that it had worked, but when I look at the table data, nothing has changed..

Public Class SignUp
Dim mysqlConn As Data.SqlClient.SqlConnection
Dim command As Data.SqlClient.SqlCommand

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    mysqlConn = New Data.SqlClient.SqlConnection
    mysqlConn.ConnectionString = ("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\NewFolder1\Members.mdf;Integrated Security=True")
    Dim RowsAffected As Integer
    Try
        mysqlConn.Open()
        Dim query As String = "INSERT INTO [User] (username, password) VALUES (@Username, @Password)"
        command = New SqlClient.SqlCommand(query, mysqlConn)
        Dim paramUsername As New SqlClient.SqlParameter() With {.ParameterName = "@Username", .Value = TextBox1.Text, .Size = 50, .SqlDbType = SqlDbType.VarChar}
        Dim paramPassword As New SqlClient.SqlParameter() With {.ParameterName = "@Password", .Value = TextBox2.Text, .Size = 50, .SqlDbType = SqlDbType.VarChar}

        command.Parameters.Add(paramUsername)
        command.Parameters.Add(paramPassword)

        RowsAffected = command.ExecuteNonQuery

        mysqlConn.Close()

        If 0 < RowsAffected 
           Then MessageBox.Show("Data Saved")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        mysqlConn.Dispose() 'closes connection  
    End Try 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Drax
  • 141
  • 8
  • 1
    Probably a dupe of [Why saving changes to a database fails?](http://stackoverflow.com/q/17147249/1070452) You never ever store passwords as plain text though – Ňɏssa Pøngjǣrdenlarp Oct 17 '16 at 00:15
  • This is not a public form and will never be, so i dont want to hash passwords, not yet atleast :) – Drax Oct 17 '16 at 00:16
  • If it displays that message then the data is saved, because at least one row in the database was affected. You're just looking in the wrong place or at the wrong time for the data. The link @Plutonix provided is undoubtedly relevant to the situation. In short, you two databases and the one containing your changes is overwritten each time you build. Either you're looking in the one that doesn't contain your changes or you're looking in the one that does but after it's been overwritten. – jmcilhinney Oct 17 '16 at 01:17

1 Answers1

0

The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the mySqlConn.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459