System.FormatException:无法将参数值从字符串转换为Int32 [英] System.FormatException: Failed to convert parameter value from a String to a Int32

查看:209
本文介绍了System.FormatException:无法将参数值从字符串转换为Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在下面的代码中遇到一个错误.请帮忙.

System.FormatException:无法将参数值从字符串转换为Int32


Hi,



I am getting one error in code below. Please help.

System.FormatException: Failed to convert parameter value from a String to a Int32


Public Sub AddGrid()

        For count As Integer = 0 To GD_Prod.Items.Count - 1

            ''getting the bound fields values 



            Dim str0 As String = GD_Prod.Items(count).Cells(0).Text

            Dim str1 As String = GD_Prod.Items(count).Cells(1).Text

            Dim str2 As String = GD_Prod.Items(count).Cells(2).Text

            Dim str5 As String = GD_Prod.Items(count).Cells(5).Text

            Dim str13 As String = GD_Prod.Items(count).Cells(13).Text

            Dim str14 As String = GD_Prod.Items(count).Cells(14).Text

            ''getting the template fields value   

            Dim tx3 As TextBox = DirectCast(GD_Prod.Items(count).FindControl("TxtSTUPort"), TextBox)

            Dim tx4 As TextBox = DirectCast(GD_Prod.Items(count).FindControl("TxtAAPort"), TextBox)

            Dim tx6 As TextBox = DirectCast(GD_Prod.Items(count).FindControl("TxtFPort"), TextBox)

            Dim tx7 As TextBox = DirectCast(GD_Prod.Items(count).FindControl("TxtQoFUsed"), TextBox)

            Dim tx8 As TextBox = DirectCast(GD_Prod.Items(count).FindControl("TxtPPort"), TextBox)

            Dim tx9 As TextBox = DirectCast(GD_Prod.Items(count).FindControl("TxtLOPort"), TextBox)

            Dim tx10 As TextBox = DirectCast(GD_Prod.Items(count).FindControl("TxtBFPort"), TextBox)

            Dim tx11 As DropDownList = DirectCast(GD_Prod.Items(count).FindControl("ddlLOCode"), DropDownList)

            Dim tx12 As TextBox = DirectCast(GD_Prod.Items(count).FindControl("TxtTSPort"), TextBox)



            ''Dim tx15 As Label = DirectCast(FindControl("lblPKey"), Label)

            ''Dim str15 As String = (lblPKey.Text)

            Dim strkey As String = Session("key").ToString



            ''inserting the template values 

            Dim str3 As String = (tx3.Text)

            Dim str4 As String = (tx4.Text)

            Dim str6 As String = (tx6.Text)

            Dim str7 As String = (tx7.Text)

            Dim str8 As String = (tx8.Text)

            Dim str9 As String = (tx9.Text)

            Dim str10 As String = (tx10.Text)

            Dim str11 As String = (tx11.SelectedValue.ToString)

            Dim str12 As String = (tx12.Text)



            Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("FNConnectionString").ConnectionString)

            Dim cmd As New SqlCommand("P_FN_PR_InsertGridData", con)

            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.Add("@Pkey", SqlDbType.Char).Value = strkey

            cmd.Parameters.Add("@PL_Recipe_Number", SqlDbType.Char).Value = str0

            cmd.Parameters.Add("@PL_Recipe_Name", SqlDbType.Char).Value = str1

            cmd.Parameters.Add("@PL_Portion_Size", SqlDbType.Char).Value = str2

            cmd.Parameters.Add("@PL_Student_Portions_Planned", SqlDbType.Int).Value = str3

            cmd.Parameters.Add("@PL_Adults_ALC__Portions_Planned", SqlDbType.Int).Value = str4

            cmd.Parameters.Add("@PL_Total__Portions_Planned", SqlDbType.Int).Value = str5

            cmd.Parameters.Add("@PL_Factor_Portions", SqlDbType.Char).Value = str6

            cmd.Parameters.Add("@DOC_Quantity_Of_Food_Used", SqlDbType.Char).Value = str7

            cmd.Parameters.Add("@DOC_Portions_Prepared ", SqlDbType.Int).Value = str8

            cmd.Parameters.Add("@DOC_Portions_Brought_Forward ", SqlDbType.Int).Value = str9

            cmd.Parameters.Add("@DOC_Portions_Leftover", SqlDbType.Int).Value = str10

            cmd.Parameters.Add("@DOC_Leftover_Code", SqlDbType.Char).Value = str11

            cmd.Parameters.Add("@DOC_Total_Portions_Served", SqlDbType.Int).Value = str12

            cmd.Parameters.Add("@DOC_Portions_Served_Students", SqlDbType.Int).Value = str13

            cmd.Parameters.Add("@DOC_Portions_Served_Adults_ALC", SqlDbType.Int).Value = str14

            con.Open()

            ''cmd.ExecuteNonQuery()

            Dim rows As Integer = cmd.ExecuteNonQuery

            con.Close()

            If rows = 1 Then

                MsgBox("SUCCESS!")

            Else

                MsgBox("FAILED!")

            End If

        Next

        

    End Sub




我的存储过程为:




My Stored Proc is :

CREATE PROCEDURE [DBO].P_FN_PR_InsertGridData

	

	(

	@Pkey char(15),

	@PL_Recipe_Number char(4),

	@PL_Recipe_Name char(20),

	@PL_Portion_Size char(6),

	@PL_Student_Portions_Planned int,

	@PL_Adults_ALC__Portions_Planned int,

	@PL_Total__Portions_Planned int,

	@PL_Factor_Portions char(10),

	@DOC_Quantity_Of_Food_Used char(10),

	@DOC_Portions_Prepared int,

	@DOC_Portions_Brought_Forward int,

	@DOC_Portions_Leftover int,

	@DOC_Leftover_Code char(3),

	@DOC_Total_Portions_Served int,

	@DOC_Portions_Served_Students int,

	@DOC_Portions_Served_Adults_ALC int

	)

	

AS

INSERT INTO FNProdRecDetails (Date_Loc_Type, PL_Recipe_Number, PL_Recipe_Name, PL_Portion_Size, PL_Student_Portions_Planned, PL_Adults_ALC__Portions_Planned, 

PL_Total__Portions_Planned, PL_Factor_Portions, DOC_Quantity_Of_Food_Used, DOC_Portions_Prepared, DOC_Portions_Brought_Forward, DOC_Portions_Leftover,

DOC_Leftover_Code, DOC_Total_Portions_Served, DOC_Portions_Served_Students, DOC_Portions_Served_Adults_ALC)



VALUES (@Pkey , @PL_Recipe_Number, @PL_Recipe_Name, @PL_Portion_Size, @PL_Student_Portions_Planned, @PL_Adults_ALC__Portions_Planned,

@PL_Total__Portions_Planned, @PL_Factor_Portions, @DOC_Quantity_Of_Food_Used, @DOC_Portions_Prepared, @DOC_Portions_Brought_Forward, @DOC_Portions_Leftover,

@DOC_Leftover_Code, @DOC_Total_Portions_Served, @DOC_Portions_Served_Students, @DOC_Portions_Served_Adults_ALC)

推荐答案

您始终可以使用各种TryParse方法来确保字符串在转换期间与期望的格式匹配.

问候
Espen Harlinn
You can always use the various TryParse methods to ensure that the strings matches the expected format during conversion.

Regards
Espen Harlinn


出现此错误的原因与未正确格式化为Integer的字符串的转换有关.如果这些文本框之一中包含非数字字符,则会出现此错误.您还可以得到错误消息,即文本框为空白.

这就是为什么在尝试使用其中的内容之前必须先验证所有用户输入的原因.现在,如果用户在文本框中键入错误的字符,则可以破坏您的应用程序.

最重要的是,您不要将字符串(即使它看起来像数字)也不会传递给声明为Int的参数.

您已将要传递给参数对象的所有值声明为字符串(str ##).您不能使用数字参数类型(SqlDbType.Int)做到这一点.
The reason you''re getting that error has to do with the conversion of a string that is not properly formatted into an Integer. If you have non-numeric characters in one of those textboxes, you''ll get this error. You can also get the error is the textbox is blank.

This is why you have to VALIDATE ALL USER INPUT before you try and use whats there. Right now, the user has the ability to break your app if he/she types the wrong character in a textbox.

Bottom line, you don''t pass a string (even if it LOOKS like a number) to a parameter that is declared Int.

You''ve declared all of the values you are passing to your parameter objects as strings (str##). You cannot do that with number parameter types (SqlDbType.Int).


这篇关于System.FormatException:无法将参数值从字符串转换为Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆