本文介绍了如何处理从字符串到类型整数的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我只是想知道是否有人可以帮我解决以下问题。我正在使用VB。 NET和SQL Server,并希望在我的代码中包含一些错误处理。 我理解这个错误的发生是因为数据类型是字符串。 我尝试过: Public Sub addProducts()尝试 dbConnection() search_query = SELECT * FROM tblproducts WHERE product_name = @product_name; command = New SqlCommand 使用命令 .Connection = connection .CommandText = search_query .Parameters.Clear() .Parameters.Add(New SqlParameter使用{.ParameterName =@ product_name,。SqlDbType = SqlDbType.VarChar,.Value = productsView.txtProductName.Text}) Dim check As String = .ExecuteScalar()。ToString() If检查> 0然后 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,@ price,@ type,@ supplier,@ unit); command = New SqlCommand 使用命令 .Connection = connection .CommandText = insert_query .Parameters.Clear() .Parameters.Add(New SqlParameter使用{.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(带有{.ParameterName =@ price的新SqlParameter,.SqlDbType = SqlDbType.Int,.Value = productsView.txtSellingPrice .Text}) .Parameters.Add(New SqlParameter With {.ParameterName =@ type,。SqlDbType = SqlDbType.VarChar,.Value = productsView.cbProductType.SelectedItem}) .Parameters.Add (新的SqlParamete r {.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()如果result = 0那么 formNotify.Show(添加产品时出错! ,formNotify.AlertType.err0r) Else formNotify.Show(Product added!,formNotify.AlertType.success) End if End with End If 以结束Catch ex As SqlException MsgBox(Error:+ ex.Message)最后 connection.Close() command.Dispose ()结束尝试结束子 解决方案 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.What I have tried: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 解决方案 这篇关于如何处理从字符串到类型整数的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 22:02