0

I'm still new here and even in Visual Studio so I don't fully know all the function in Visual Studio. I'm trying to make the system to check the database first for any existing staID, staName, depID, and telNo so that the system will skip the process if the data already exist.

But if there aren't any, the system will record the user input to the database.

Here's my code:

Imports System.Data.SqlClient
Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick

        Dim con As New SqlConnection("Data Source=.\SQLEXPRESS; AttachDbFilename= C:\Users\ammaramli\Documents\Visual Studio 2010\WebSites\Project\App_Data\Database.mdf; Integrated Security=True;User Instance=True")
        Dim strinsert As String
        Dim cmd As SqlCommand

        strinsert = "INSERT INTO Staff (staID, staName, depID, telNo) values (@staID, @staName, @depID, @telNo)"
        cmd = New SqlCommand(strinsert, con)
        con.Open()
        cmd.Parameters.AddWithValue("@staID", staID.Text)
        cmd.Parameters.AddWithValue("@staName", staName.Text)
        cmd.Parameters.AddWithValue("@depID", depID.Text)
        cmd.Parameters.AddWithValue("@telNo", telNo.Text)
        cmd.ExecuteNonQuery()
        cmd.Parameters.Clear()
        con.Close()
    End Sub
End Class

So my question will be, how can I make it like that? It's been 2 days I'm stuck here.

bummi
  • 27,123
  • 14
  • 62
  • 101
Ammar Ramli
  • 1
  • 1
  • 3
  • Don't use the `AttachDbFileName=` and `UserInstance=true` features anymore - they cause a lot of confusion and grief, and will be removed from SQL Server in a future version anyway - better stay away from it today already! See Aaron Bertrand's excellent blog post [Bad habits : Using AttachDBFileName](http://blogs.sqlsentry.com/aaronbertrand/bad-habits-attachdbfilename/) for more background info on this. – marc_s Oct 13 '14 at 20:28
  • possible duplicate of [SQL Insert into ... values ( SELECT ... FROM ... )](http://stackoverflow.com/questions/25969/sql-insert-into-values-select-from) – Isaac G Sivaa Oct 13 '14 at 20:31
  • I'm still a student though and the lecture asked me to follow what is in the book so I don't really know much about it. But still I'll keep it in mind about it @marc_s . Just that I want to know how to compare the user input and the data in the database for the staID especially. – Ammar Ramli Oct 13 '14 at 20:44

0 Answers0