Please let someone help me out here, i want to insert record to the database using VS2010, when i input data to textboxes and hit button, the program will run and tell me data inserted succesffuly. But when i check my dbs table i won't see any thing. here is the code:

If (CheckBox1.Checked = True) Then

            'Try
            'To configure or set our connection string for now
            Dim conn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TaxDatabase.mdf;Integrated Security=True;User Instance=True"
            connection = New SqlConnection(conn)
            'To open our connection to database as late as possible and keep opened for short period of time
            connection.Open()
            'To query our database
            Dim sql As String = "INSERT INTO AdminStaffdata(FullName,StaffID,Password,Sex,Date) VALUES(@FullName,@StaffID,@Password,@Sex,@Date)"
            'Create an instance of command object.command object helps us to communicate with datasource e.g database
            'Here command object will help us to insert the values for the parameters we have.
            Dim cmd As SqlCommand = New SqlCommand(sql, connection)
            cmd.Parameters.AddWithValue("@FullName", TextBox1.Text)
            cmd.Parameters.AddWithValue("@StaffID", TextBox2.Text)
            cmd.Parameters.AddWithValue("@Password", TextBox3.Text)
            cmd.Parameters.AddWithValue("@Sex", TextBox4.Text)
            cmd.Parameters.AddWithValue("@Date", TextBox5.Text)
            'To executescaler means to execute the above instructions i.e to insert only one row of data
            Dim Id As Integer = Convert.ToInt32(cmd.ExecuteScalar())
            'Change color of label4 to green
            Label10.ForeColor = Drawing.Color.Green
            'label4 should write or display :"Success! Your CV has been uploaded."
            Label10.Text = "Successfully Registered!"

            ''Catch
            ''MessageBox.Show(ErrorToString())
            'Label4.ForeColor = Drawing.Color.DarkRed
            'Label4.Text = "Data is already existing!"
            'Finally
            connection.Close()

            'End Try
            'End If

Recommended Answers

All 2 Replies

'To executescaler means to execute the above instructions i.e to insert only one row of data
            Dim Id As Integer = Convert.ToInt32(cmd.ExecuteScalar())

To executescalar means to query the db and return only the first result.
Try cmd.ExecuteNonQuery

Also your program will always return the message for success, even after an error, as you are not checking if something went wrong, you just show the message after the command execution.

'To executescaler means to execute the above instructions i.e to insert only one row of data
Dim Id As Integer = Convert.ToInt32(cmd.ExecuteScalar())

Thats not to insert, use this:

connection.Open()
     cmd.ExecuteNonQuery()
connection.Close()

and try to do some error handling on your codes as suggested by Adam_K

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.