Hi Guys,

I am new to Vb.net please help me sort out this problem. Formatexceptionunhandled:

i am trying to save data from unbound datagridview to sql database.

Failed to convert parameter value from a String to a Int32. 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 REQUISITIONS (RequsitionID,Date,DepartmentID,Department,ItemID, Item Name,CategoryID,Category,UnitID, Units, Unit Price,Quantity, Amount,User Name)" & _
" VALUES(@RequsitionID,@Date,@DepartmentID,@Department,@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").Precision = 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.ToString
nonqueryCommand.Parameters("@Date").Value = row.Cells(1).Value.ToString
nonqueryCommand.Parameters("@DepartmentID").Value = row.Cells(2).Value.ToString
nonqueryCommand.Parameters("@Department").Value = row.Cells(3).Value.ToString
nonqueryCommand.Parameters("@ItemID").Value = row.Cells(4).Value.ToString
nonqueryCommand.Parameters("@Item_Name").Value = row.Cells(5).Value.ToString
nonqueryCommand.Parameters("@CategoryID").Value = row.Cells(6).Value.ToString
nonqueryCommand.Parameters("@Category").Value = row.Cells(7).Value.ToString
nonqueryCommand.Parameters("@UnitID").Value = row.Cells(8).Value.ToString
nonqueryCommand.Parameters("@Units").Value = row.Cells(9).Value.ToString
nonqueryCommand.Parameters("@Unit_Price").Value = row.Cells(10).Value.ToString
nonqueryCommand.Parameters("@Quantity").Value = row.Cells(11).Value.ToString
nonqueryCommand.Parameters("@Amount").Value = row.Cells(12).Value.ToString
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