Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Unanswered: Failed to convert parameter value from a String to a Int32.

    Hi guys,

    i am trying to save data from unbound datagridview but i get the error " format exception unhandled- failed to convert parameter value to a int32"

    Please help

    the following is my code:

    Private Sub btnPlace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlace.Click
    ' Modify the following code to correctly connect to your SQL Server.
    Dim Connection As New SqlConnection("Server=.\SQLEXPRESS;Initial Catalog=Store Management System;Integrated Security=SSPI ")

    'Create Command object
    Dim nonqueryCommand As SqlCommand = Connection.CreateCommand()





    Try
    ' Open Connection
    Connection.Open()
    MsgBox("Connection Opened")

    ' Create INSERT statement with named parameters
    nonqueryCommand.CommandText = _
    "INSERT INTO Requisitions (RequsitionID,Date,DepartmentID,Department,ItemID,[Item Name],CategoryID,Category,UnitID,Units,[Unit Price],Quantity,Amount,[User Name])" & _
    " VALUES(@RequsitionID,@Date,@DepartmentID,@Departme nt,@ItemID,@Item Name,@CategoryID,@Category,@UnitID,@Units,@Unit Price,@Quantity,@Amount,@User Name)"

    'Add Parameters to Command Parameters collection
    nonqueryCommand.Parameters.Add("@RequisitionID", SqlDbType.Int)
    nonqueryCommand.Parameters.Add("@Date", SqlDbType.SmallDateTime)
    nonqueryCommand.Parameters.Add("@DepartmentID", SqlDbType.Int)
    nonqueryCommand.Parameters.Add("@Department", SqlDbType.VarChar, 50)
    nonqueryCommand.Parameters.Add("@ItemID", SqlDbType.Int)
    nonqueryCommand.Parameters.Add("@Item_Name", SqlDbType.VarChar, 50)
    nonqueryCommand.Parameters.Add("@CategoryID", SqlDbType.Int)
    nonqueryCommand.Parameters.Add("@Category", SqlDbType.VarChar, 80)
    nonqueryCommand.Parameters.Add("@UnitID", SqlDbType.Int)
    nonqueryCommand.Parameters.Add("@Units", SqlDbType.NVarChar, 20)
    nonqueryCommand.Parameters.Add("@Unit_Price", SqlDbType.Decimal, 9)
    nonqueryCommand.Parameters("@Unit_Price").Precisio n = 18
    nonqueryCommand.Parameters("@Unit_Price").Scale = 2
    nonqueryCommand.Parameters.Add("@Quantity", SqlDbType.NChar, 10)
    nonqueryCommand.Parameters.Add("@Amount", SqlDbType.Money)
    nonqueryCommand.Parameters.Add("@User_Name", SqlDbType.VarChar, 20)

    ' Prepare command for repeated execution
    nonqueryCommand.Prepare()

    ' Data to be inserted

    For Each row As DataGridViewRow In RequisitionsDGV.Rows

    If Not row.IsNewRow Then
    nonqueryCommand.Parameters("@RequisitionID").Value = row.Cells(0).Value
    nonqueryCommand.Parameters("@Date").Value = row.Cells(1).Value
    nonqueryCommand.Parameters("@DepartmentID").Value = row.Cells(2).Value
    nonqueryCommand.Parameters("@Department").Value = row.Cells(3).Value.ToString
    nonqueryCommand.Parameters("@ItemID").Value = row.Cells(4).Value
    nonqueryCommand.Parameters("@Item_Name").Value = row.Cells(5).Value.ToString
    nonqueryCommand.Parameters("@CategoryID").Value = row.Cells(6).Value
    nonqueryCommand.Parameters("@Category").Value = row.Cells(7).Value.ToString
    nonqueryCommand.Parameters("@UnitID").Value = row.Cells(8).Value
    nonqueryCommand.Parameters("@Units").Value = row.Cells(9).Value.ToString
    nonqueryCommand.Parameters("@Unit_Price").Value = row.Cells(10).Value
    nonqueryCommand.Parameters("@Quantity").Value = row.Cells(11).Value
    nonqueryCommand.Parameters("@Amount").Value = row.Cells(12).Value
    nonqueryCommand.Parameters("@User_Name").Value = row.Cells(13).Value.ToString
    nonqueryCommand.ExecuteNonQuery()

    End If
    Next row



    Catch ex As SqlException
    ' Display error
    MsgBox("Error: " & ex.ToString())
    Finally
    ' Close(Connection)
    Connection.Close()
    MsgBox("Connection Closed")



    End Try
    End Sub

  2. #2
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    Have you narrowed down the origin of the error and verified that the value is indeed a number?
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •