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.
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
SubRtn
Member
6 Points
29 Posts
Help inserting data on button click - VB.Net/SQL 2008
May 05, 2012 02:36 PM|LINK
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.
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 SubKen Tucker
All-Star
16797 Points
2608 Posts
MVP
Re: Help inserting data on button click - VB.Net/SQL 2008
May 05, 2012 02:42 PM|LINK
Try changing the catch block to output the error to the label
Catch Ex as Exception
dberrorlabel.Text = ex.Message
Once you know the exact error we can help you fix it
Space Coast .Net User Group
SubRtn
Member
6 Points
29 Posts
Re: Help inserting data on button click - VB.Net/SQL 2008
May 05, 2012 07:41 PM|LINK
Here is the error I am getting: Incorrect syntax near '@Extension'.
When I debug it shows the error on the Catch block.
Hope this helps.
imobsuz
Participant
1278 Points
195 Posts
Re: Help inserting data on button click - VB.Net/SQL 2008
May 05, 2012 08:40 PM|LINK
I believe you're missing a comma between @HomePhone and @Extension:
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)Hope this helps.
SubRtn
Member
6 Points
29 Posts
Re: Help inserting data on button click - VB.Net/SQL 2008
May 05, 2012 09:04 PM|LINK
That fixed it!! I also had a few other bugs that popped up after fixing this one. I resolved those myself.
Thank you both for the help.