SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    INSERT on button click - error VB.Net SQL 2008

    Hello,

    I am working through the "Build Your Own ASP.NET4 Website" book and am at the part where you build the AdmimTools.apsx page. I have it all working fine and wanted to venture out on my own and add a little additional functionality.

    Essentially I have a button at the top (newEmpButton) that when clicked will clear the form of any values and enable the visibility of a new button (addEmpButton). A label also appears that tell the user to enter the informatoin and click (addEmpButton) to submit. Then the user is redirected back to the same page where the list of employees is now refreshed with the employee that was just added.

    The data is not entered and I receive the "dbErrorLabel" message. I have searched the code for hours trying different things to no avail. The error resides in the addEmpButton_Click Sub.

    Can anyone help me figure out what the issue is?

    *Note: the @EmployeeID and @DepartmentID are assigned a value as they are not part of the form layout and cannot be Null. These values are the next incremental value in my database in this table.


    Code:
    Protected Sub newEmpButton_Click(sender As Object, e As System.EventArgs) Handles newEmpButton.Click
    
            addEmpLabel.Text = "Please enter new employee info below and click ADD EMPLOYEE<br />"
    
            nameTextBox.Text = ""
            userNameTextBox.Text = ""
            addressTextBox.Text = ""
            cityTextBox.Text = ""
            stateTextBox.Text = ""
            zipTextBox.Text = ""
            homePhoneTextBox.Text = ""
            extensionTextBox.Text = ""
            mobilePhoneTextBox.Text = ""
    
            updateButton.Enabled = False
            deleteButton.Enabled = False
            updateButton.Enabled = False
            addEmpButton.Visible = True
            addEmpButton.Enabled = True
    
        End Sub
    
        Protected Sub addEmpButton_Click(sender As Object, e As System.EventArgs) Handles addEmpButton.Click
    
            If Page.IsValid Then
                Dim conn As SqlConnection
                Dim comm As SqlCommand
                Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dorknozzle").ConnectionString
    
                conn = New SqlConnection(connectionString)
                comm = New SqlCommand( "INSERT INTO Employees (EmployeeID, DepartmentID, Name, " & _
            "Username, Password, Address, City, State, Zip, HomePhone, Extension, MobilePhone) " & _
            "VALUES (@EmployeeID, @DepartmentID, @Name, @Username, @Password, @Address, @City, @State, @Zip, @HomePhone " & _
            "@Extension, @MobilePhone)", conn)
    
                comm.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int)
                comm.Parameters("@EmployeeID").Value = 14
                comm.Parameters.Add("@DepartmentID", System.Data.SqlDbType.Int)
                comm.Parameters("@DepartmentID").Value = 8
                comm.Parameters.Add("@Name", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@Name").Value = nameTextBox.Text
                comm.Parameters.Add("@Username", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@Username").Value = userNameTextBox.Text
                comm.Parameters.Add("@Password", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@Password").Value = "heather"
                comm.Parameters.Add("@Address", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@Address").Value = addressTextBox.Text
                comm.Parameters.Add("@City", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@City").Value = cityTextBox.Text
                comm.Parameters.Add("@State", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@State").Value = stateTextBox.Text
                comm.Parameters.Add("@Zip", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@Zip").Value = zipTextBox.Text
                comm.Parameters.Add("@HomePhone", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@HomePhone").Value = homePhoneTextBox.Text
                comm.Parameters.Add("@Extension", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@Extension").Value = extensionTextBox.Text
                comm.Parameters.Add("@MobilePhone", System.Data.SqlDbType.NVarChar, 50)
                comm.Parameters("@MobilePhone").Value = mobilePhoneTextBox.Text
    
                Try
                    conn.Open()
                    comm.ExecuteNonQuery()
    
                    Response.Redirect("AdminTools.aspx")
    
                Catch
                    dberrorlabel.Text = "Error adding new employee!"
    
                Finally
                    ' Close the connection
                    conn.Close()
    
                End Try
    
            End If
    
            LoadEmployeesList()
        End Sub

  2. #2
    SitePoint Member
    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I am getting error: Incorrect syntax near '@Extension' at the CATCH Block when I debug. It steps through everything else just fine but hangs up there.

    Thanks for any help you can give.

  3. #3
    SitePoint Member
    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found the issue. Had a missing comma in my insert command.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •