Input string was not in a correct format

th3gman

Member
Joined
Nov 17, 2010
Messages
16
Programming Experience
Beginner
Hello all,

I'm getting the following error... problem is I know that the ContactCatId is an Integer, but I'm not sure how to update the database correctly once someone has chosen something from the DropDown box that this is tied to.

I'll attach the code below.

Thanks!

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

Line 33: UpdateCMD.Parameters.Add("@ContactFax", SqlDbType.Text).Value = ContactFax.Text
Line 34: UpdateCMD.Parameters.Add("@ContactCatId", SqlDbType.Int).Value = DropDownCategory.SelectedValue
Line 35: UpdateCMD.ExecuteNonQuery()
Line 36:
Line 37: myConnection.Close()

VB.NET:
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Partial Class secure_contacts_contact_edit
    Inherits System.Web.UI.Page
    Dim connString As String = _
    ConfigurationManager.ConnectionStrings("teal_data").ConnectionString()
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using myConnection As New SqlConnection(connString)
            Dim UpdateSql As String
            UpdateSql = "UPDATE teal_Contacts Set ContactFirstName = @ContactFirstName, " & _
            "ContactLastName = @ContactLastName, " & _
            "ContactPhone = @ContactPhone, " & _
            "ContactEmail = @ContactEmail, " & _
            "ContactNotes = @ContactNotes, " & _
            "ContactMobile = @ContactMobile, " & _
            "ContactFax = @ContactFax, " & _
            "JobTitle = @JobTitle, " & _
            "EmergencyPriority = @EmergencyPriority, " & _
            "ContactCatId = @ContactCatId " & _
            "WHERE ContactId = @ContactId"
            Dim UpdateCMD As New SqlCommand(UpdateSql, myConnection)
            myConnection.Open()
            UpdateCMD.Parameters.Add("@ContactFirstName", SqlDbType.VarChar).Value = ContactFirstName.Text
            UpdateCMD.Parameters.Add("@ContactLastName", SqlDbType.VarChar).Value = ContactLastName.Text
            UpdateCMD.Parameters.Add("@ContactPhone", SqlDbType.Text).Value = ContactPhone.Text
            UpdateCMD.Parameters.Add("@ContactEmail", SqlDbType.Text).Value = ContactEmail.Text
            UpdateCMD.Parameters.Add("@ContactNotes", SqlDbType.Text).Value = ContactNotes.Text
            UpdateCMD.Parameters.Add("@ContactMobile", SqlDbType.Text).Value = ContactMobile.Text
            UpdateCMD.Parameters.Add("@JobTitle", SqlDbType.VarChar).Value = JobTitle.Text
            UpdateCMD.Parameters.Add("@ContactId", SqlDbType.Int).Value = Request.QueryString("cid")
            UpdateCMD.Parameters.Add("@EmergencyPriority", SqlDbType.Text).Value = EmergencyPriority.Text
            UpdateCMD.Parameters.Add("@ContactFax", SqlDbType.Text).Value = ContactFax.Text
            UpdateCMD.Parameters.Add("@ContactCatId", SqlDbType.Int).Value = DropDownCategory.SelectedValue
            UpdateCMD.ExecuteNonQuery()

            myConnection.Close()
            If (Request.QueryString("project") > 0) Then
                Response.Redirect("../projects/project-central.aspx?p=" & Request.QueryString("project") & "")
            Else
                Response.Redirect("contact-display.aspx?cid=" & Request.QueryString("cid") & "")
            End If

        End Using
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Using myConnection As New SqlConnection(connString)
            If Not Page.IsPostBack Then
                If (Request.QueryString("p") = 1) Then
                    'Process to delete a tag
                    Dim DelSql As String
                    DelSql = "DELETE FROM teal_ContactTags WHERE TagId = @TagId"
                    Dim DelCMD As New SqlCommand(DelSql, myConnection)
                    myConnection.Open()
                    DelCMD.Parameters.Add("@TagId", SqlDbType.Int).Value = Request.QueryString("pid")
                    DelCMD.ExecuteNonQuery()
                    myConnection.Close()

                    Response.Redirect("contact-edit.aspx?cid=" & Request.QueryString("cid") & "")

                Else
                    Dim SuperCatVar As Integer
                    Dim NameCMD As New SqlCommand("ContactInfoSelect", myConnection)
                    NameCMD.CommandType = CommandType.StoredProcedure
                    myConnection.Open()
                    NameCMD.Parameters.Add("@ContactId", SqlDbType.Int).Value = Request.QueryString("cid")
                    Dim NameDR As SqlDataReader = NameCMD.ExecuteReader

                    While NameDR.Read
                        ContactNameHeader.Text = "<h1>" & NameDR("ContactFirstName") & " " & NameDR("ContactLastName") & "</h1>"
                        ContactFirstName.Text = NameDR("ContactFirstName")
                        ContactLastName.Text = NameDR("ContactLastName")
                        EmergencyPriority.Text = NameDR("EmergencyPriority")
                        ContactPhone.Text = NameDR("ContactPhone")
                        ContactMobile.Text = NameDR("ContactMobile")
                        ContactEmail.Text = NameDR("ContactEmail")
                        ContactNotes.Text = NameDR("ContactNotes")
                        JobTitle.Text = NameDR("JobTitle")
                        ContactFax.Text = NameDR("ContactFax")
                        SuperCatVar = NameDR("SuperCatId")
                    End While

                    NameDR.Close()
                    myConnection.Close()

                    Dim CatCMD As New SqlCommand("SELECT CategoryName FROM teal_ContactsCategories WHERE SuperId = @SuperCatVar ORDER BY CategoryName", myConnection)
                    Dim CatDR As SqlDataReader
                    myConnection.Open()
                    CatCMD.Parameters.Add("@SuperCatVar", SqlDbType.Int).Value = SuperCatVar
                    CatDR = CatCMD.ExecuteReader(CommandBehavior.CloseConnection)
                    While CatDR.Read()
                        DropDownCategory.Items.Add(CatDR.Item("CategoryName"))
                    End While
                    CatDR.Close()
                    myConnection.Close()

                    Dim LabelOptionsSB As New StringBuilder

                    If (Request.QueryString("project") > 0) Then
                        LabelOptionsSB.Append("<a href=""../projects/project-central.aspx?p=" & Request.QueryString("project") & """>Back to the Project Details</a><br />")
                        LabelOptionsSB.Append("<a href=""contact-add-tags.aspx?cid=" & Request.QueryString("cid") & "&project=" & Request.QueryString("project") & """>Add tagged projects</a><br />")
                        LabelOptionsSB.Append("<a href=""contact-edit-tags.aspx?cid=" & Request.QueryString("cid") & "&project=" & Request.QueryString("project") & """>Remove tagged projects</a>")
                        '   LabelOptionsSB.Append("")
                    Else
                        LabelOptionsSB.Append("<a href=""contact-display.aspx?cid=" & Request.QueryString("cid") & """>Back to the Contact Details</a><br />")
                        LabelOptionsSB.Append("<a href=""contact-add-tags.aspx?cid=" & Request.QueryString("cid") & """>Add tagged projects</a><br />")
                        LabelOptionsSB.Append("<a href=""contact-edit-tags.aspx?cid=" & Request.QueryString("cid") & """>Remove tagged projects</a>")

                    End If

                    LabelOptions.Text = LabelOptionsSB.ToString()

                End If
            End If
        End Using
    End Sub
End Class
 
Back
Top