Link to home
Start Free TrialLog in
Avatar of somorain
somorain

asked on

Cast from string to type Double not valid

I've got a drop down list on an asp.net form and I'm inserting the
selected text to a an SQL Server database but i'm encountering the
error 'Cast from string "On Hold" to type 'Double' is not valid.'

This is the code:

 Sub btnSubmit_Click(Sender As Object, E As EventArgs)
 'If Not IsPostBack Then
 If Page.IsValid Then

               Dim strSQL As String

               strSQL = "UPDATE tbl_THA SET " & _
                               "Line_No = @Line, " & _
                               "Zone = @Zone, " & _
                               "Product = @Product, " & _
                               "Process_Fail_Mode = @PFM, " & _
                               "CPM = @CPM, " & _
                               "Lot_Id = @LotID, " & _
                               "No_of_Reels_on_Hold = @NumReels, " & _
                               "Qty_On_Hold = @Qty, " & _
                               "Reel_Id = @ReelID, " & _
                               "Fallout = @Fallout, " & _
                               "Status = @Status, " & _
                               "Operations_Comments = @Comments " & _
                               "WHERE THA_on_hold_No = "&txtTHA_No.Text

               Dim strConnection As String =
ConfigurationSettings.AppSettings("msle02")
               Dim objConnection As New SqlConnection(strConnection)

               Dim dbComm As New SqlCommand(strSQL, objConnection)

               dbComm.Parameters.Add("@Line", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@Zone", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@Product", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@Shift", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@PFM", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@Originator", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@CPM", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@LotID", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@NumReels", SqlDbType.int, 4)
               dbComm.Parameters.Add("@Qty", SqlDbType.int, 4)
               dbComm.Parameters.Add("@ReelID", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@Fallout", SqlDbType.int, 4)
               dbComm.Parameters.Add("@Status", SqlDbType.NVarChar, 50)
               dbComm.Parameters.Add("@Comments", SqlDbType.NVarChar, 255)

               dbComm.Parameters("@Line").Value = cmbLine.SelectedItem.Text
               dbComm.Parameters("@Zone").Value =
cmbZone.SelectedItem.Text
               dbComm.Parameters("@Product").Value = cmbProduct.SelectedItem.Text
               dbComm.Parameters("@PFM").Value =
cmbProcessFailMode.SelectedItem.Text
               dbComm.Parameters("@Shift").Value = cmbShift.SelectedItem.Text
               dbComm.Parameters("@Originator").Value = txtOriginator.Text
               dbComm.Parameters("@CPM").Value = cmbCPM.SelectedItem.Text
               dbComm.Parameters("@LotID").Value = txtLotID.Text
               dbComm.Parameters("@NumReels").Value = txtNumReels.Text
               dbComm.Parameters("@Qty").Value = txtQty.Text
               dbComm.Parameters("@ReelID").Value = txtReelID.Text
               If txtFallout.Text = "" Then
                       dbComm.Parameters("@Fallout").Value = 0
               Else
                       dbComm.Parameters("@Fallout").Value = txtFallout.Text
               End If
               dbComm.Parameters("@Status").Value = cmbStatus.SelectedItem.Text
               dbComm.Parameters("@Comments").Value = txtComments.Text

               'Response.Write(strSQL)


       Dim iID as Integer

   Try
     objConnection.Open()
     iID = dbComm.ExecuteNonQuery()
         'Response.Write(iID)
         If (iID = 1) And (cmbStatus.SelectedItem.Value = 2) Then
               lblStatus.Text = "Record Updated. Please modify barcode."
               lblStatus3.Text = "="
           btnSubmit.Enabled = false
         Else If (iID = 1) Then
               lblStatus.Text = "Record Updated."
           btnSubmit.Enabled = false
         End If
   Catch ex As Exception
         'If ex.Message =
         Response.Write("An error has occurred: ")
     Response.Write(ex.Message)
         Response.Write("<br>RECORD NOT UPDATED. PLEASE TRY AGAIN OR REPORT
ERROR")
         Response.Write(cmbStatus.SelectedItem.Text)
     Response.End
   Finally
     If objConnection.State = ConnectionState.Open Then
       objConnection.Close()
     End If
   End Try

       If ((cmbStatus.SelectedItem.Value = 4) or
(cmbStatus.SelectedItem.Value = 5) or (cmbStatus.SelectedItem.Value =
6)) Then
                btnClose.Enabled = True
       End If


  End If

 End Sub

The drop down list is populated with 5 text values in the page load
sub. The strange thing is if I change the value in the list, I don't
get the error. It only occurs if I leave the selected item as it is
when the page loads.

I can't understand why its trying to change to type Double in the
first place. I don't have any Cast statements and the data type in the
db is nvarchar(50).

Any help is greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Trace/Debug the value of strSQL, and run it in query analyser, you will find the point of error.

Avatar of somorain
somorain

ASKER

On closer inspection, there was a - between on and hold in my drop down list.

Thanks for the advice, it helped a lot.