Как обрабатывать преобразование из строки В тип integer недопустимо
I am just wondering if someone could help me with the following. I am using VB. NET and SQL Server and want to include some error handling in my code. I understand this error occurs because the datatype is string.
Что я уже пробовал:
Public Sub addProducts() Try dbConnection() search_query = "SELECT * FROM tblproducts WHERE product_name = @product_name;" command = New SqlCommand With command .Connection = connection .CommandText = search_query .Parameters.Clear() .Parameters.Add(New SqlParameter With {.ParameterName = "@product_name", .SqlDbType = SqlDbType.VarChar, .Value = productsView.txtProductName.Text}) Dim check As String = .ExecuteScalar().ToString() If check > 0 Then formNotify.Show("Product already exists!", formNotify.AlertType.warning) Else insert_query = "INSERT INTO tblproducts(product_barcode, product_name, product_sprice, product_type, product_supplier, product_unit) " & _ "VALUES(@barcode, @name, @sprice, @type, @supplier, @unit);" command = New SqlCommand With command .Connection = connection .CommandText = insert_query .Parameters.Clear() .Parameters.Add(New SqlParameter With {.ParameterName = "@barcode", .SqlDbType = SqlDbType.VarChar, .Value = productsView.txtProductBarcode.Text}) .Parameters.Add(New SqlParameter With {.ParameterName = "@name", .SqlDbType = SqlDbType.VarChar, .Value = productsView.txtProductName.Text}) .Parameters.Add(New SqlParameter With {.ParameterName = "@sprice", .SqlDbType = SqlDbType.Int, .Value = productsView.txtSellingPrice.Text}) .Parameters.Add(New SqlParameter With {.ParameterName = "@type", .SqlDbType = SqlDbType.VarChar, .Value = productsView.cbProductType.SelectedItem}) .Parameters.Add(New SqlParameter With {.ParameterName = "@supplier", .SqlDbType = SqlDbType.VarChar, .Value = productsView.cbSupplierName.SelectedItem}) .Parameters.Add(New SqlParameter With {.ParameterName = "@unit", .SqlDbType = SqlDbType.VarChar, .Value = productsView.cbUnit.SelectedItem}) result = .ExecuteNonQuery() If result = 0 Then formNotify.Show("Error in adding product!", formNotify.AlertType.err0r) Else formNotify.Show("Product added!", formNotify.AlertType.success) End If End With End If End With Catch ex As SqlException MsgBox("Error: " + ex.Message) Finally connection.Close() command.Dispose() End Try End Sub