Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
Posted
Updated 14-Jan-11 5:36am
v2
Comments
Estys 14-Jan-11 11:37am    
added <pre> tags. please use them if you post.

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

Regards
Espen Harlinn
 
Share this answer
 
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).
 
Share this answer
 
Comments
Rohit.Net100 18-Jan-11 12:39pm    
How to validate user input and allow textbox with empty values.
Dave Kreskowiak 18-Jan-11 19:06pm    
By testing all the textbox values for a correctly formatted number within the ranges allowed. For example, what would a blank textbox value mean? 0, Nothing, or some other value??

This isn't rocket science. It's just testing values for validty before you use them.
Rohit.Net100 18-Jan-11 20:26pm    
thanks dave

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900