Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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 14-Jan-11 6:34am
Edited 14-Jan-11 6:36am
Estys12.5K
v2
Comments
Estys at 14-Jan-11 11:37am
   
added <pre> tags. please use them if you post.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can always use the various TryParse methods to ensure that the strings matches the expected format during conversion.
 
Regards
Espen Harlinn
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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).
  Permalink  
Comments
Rohit.Net100 at 18-Jan-11 12:39pm
   
How to validate user input and allow textbox with empty values.
Dave Kreskowiak at 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 at 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)

  Print Answers RSS
0 Kornfeld Eliyahu Peter 265
1 Sergey Alexandrovich Kryukov 202
2 George Jonsson 185
3 King Fisher 185
4 CPallini 155
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 14 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100