I will try to be as explicit as possible.
I am trying to input data to two tables in SQL Server. The composition of the tables which are connected by the NumeroDeOrden
are as follows.
The first table OrdenesDeCompraCON
enters the general data and total amounts of the set of products that it contains in a single line of the table.
The second table OrdenesDeCompraEXION
enters the data of each and one of the products that contains a purchase order, separating them in a different line of the table.
To identify product 3 that corresponds to purchase order number 1000 I do it this way1000-3
As you can see there are some rows that are not filled like FinalizadoPor
, FechaDeFinalizacion
, EditadoPor
and FechaDeEdicion
. This happens because these Orders enter the Pending state and when the material they buy is received, they can go to finished, so I must save the state they are in and those cells will be completed once the circuit is finished.
So I have the tables working correctly, the input of information to those tables also does it perfectly since all the items that I previously load in a DataGridView are loaded successfully in the database I have a message excepcion
that is launched when it finishes loading absolutely everything before does not happen. for more quantity of items charge simultaneously.
Image with the message
And finally this is my codeExcepcion
La leyenda de la excepcion es "The parameterized Query (@numerodeorden nvarchar(1), @item nvarchar(3), @producto nvarchar expect the parameter @producto, which was not supplied.
Sub AgregarOrden()
Dim conn As New SqlConnection
conn.ConnectionString = conexion
ruta = New SqlCommand("INSERT INTO OrdenesDeCompraCON (numerodeorden, fecha, estado, proveedor,iva, importetotalconiva, importetotalsiniva, lugardeentrega, fechadeentrega, formadepago, acreditacion, NotaGeneral, creadopor, fechadecreacion, FinalizadoPor, FechaDeFinalizacion) values (@numerodeorden, @fecha, @estado, @proveedor,@iva, @importetotalconiva, @importetotalsiniva, @lugardeentrega, @fechadeentrega, @formadepago, @acreditacion, @NotaGeneral, @creadopor, @fechadecreacion, @FinalizadoPor, @FechaDeFinalizacion)", Conn)
Dim filas As DataGridViewRowCollection = aodcdt.Rows
Try
Conn.Open()
'For Each Row In filas
ruta.Parameters.Clear()
ruta.Parameters.AddWithValue("@numerodeorden", Convert.ToString(aodcordendecompra))
ruta.Parameters.AddWithValue("@fecha", Convert.ToString(aodcfechahoy))
ruta.Parameters.AddWithValue("@estado", Convert.ToString("PENDIENTE"))
ruta.Parameters.AddWithValue("@proveedor", Convert.ToString(aodcrazonsocial))
ruta.Parameters.AddWithValue("@iva", Convert.ToString(aodciva))
ruta.Parameters.AddWithValue("@importetotalconiva", Convert.ToString(aodcimportesiniva))
ruta.Parameters.AddWithValue("@importetotalsiniva", Convert.ToString(aodcimporteconiva))
ruta.Parameters.AddWithValue("@lugardeentrega", Convert.ToString(aodclugardeentrega))
ruta.Parameters.AddWithValue("@fechadeentrega", Convert.ToString(aodcfechadeentrega))
ruta.Parameters.AddWithValue("@formadepago", Convert.ToString(aodcformadepago))
ruta.Parameters.AddWithValue("@acreditacion", Convert.ToString(aodcacreditacion))
ruta.Parameters.AddWithValue("@NotaGeneral", Convert.ToString(aodcobservacionesgrales))
ruta.Parameters.AddWithValue("@creadopor", Convert.ToString("Martin")) ' debe ir el usuario
ruta.Parameters.AddWithValue("@fechadecreacion", Convert.ToString(Format(DateTime.Now, "dd/MM/yyyy")))
ruta.Parameters.AddWithValue("@FinalizadoPor", Convert.ToString(""))
ruta.Parameters.AddWithValue("@FechaDeFinalizacion", Convert.ToString(""))
ruta.ExecuteNonQuery()
MsgBox("Datos ingresados correctamente")
' Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
If Conn.State = ConnectionState.Open Then
Conn.Close()
Conn.Dispose()
End If
End Sub
Sub agregarproducto()
Dim conn As New SqlConnection
conn.ConnectionString = conexion
ruta = New SqlCommand("INSERT INTO OrdenesDeCompraEXION (NumeroDeOrden, Item, Producto, Cantidad, iva, preciounitariosiniva, preciounitarioconiva, preciototalsiniva, preciototalconiva, creadopor, editadopor, fechadecreacion, fechadeedicion) values (@NumeroDeOrden, @Item, @Producto, @Cantidad, @iva, @preciounitariosiniva, @preciounitarioconiva, @preciototalsiniva, @preciototalconiva, @creadopor, @editadopor, @fechadecreacion, @fechadeedicion)", conn)
Dim filas As DataGridViewRowCollection = aodcdt.Rows
Try
Conn.Open()
For i As Integer = 0 To aodcdt.Rows.Count - 1
ruta.Parameters.Clear()
If String.IsNullOrEmpty(aodcordendecompra) Then
ruta.Parameters.AddWithValue("@numerodeorden", DBNull.Value)
ruta.Parameters.AddWithValue("@item", DBNull.Value)
Else
ruta.Parameters.AddWithValue("@numerodeorden", Convert.ToString(aodcordendecompra))
ruta.Parameters.AddWithValue("@item", aodcordendecompra & "-" & i + 1)
End If
ruta.Parameters.AddWithValue("@producto", aodcdt.Rows(i).Cells(0).Value)
ruta.Parameters.AddWithValue("@cantidad", aodcdt.Rows(i).Cells(1).Value)
ruta.Parameters.AddWithValue("@preciounitariosiniva", aodcdt.Rows(i).Cells(2).Value)
ruta.Parameters.AddWithValue("@preciounitarioconiva", Convert.ToString(aodcdt.Rows(i).Cells(2).Value * (1.0 + (aodciva) / 100)))
ruta.Parameters.AddWithValue("@preciototalsiniva", aodcdt.Rows(i).Cells(3).Value)
ruta.Parameters.AddWithValue("@iva", Convert.ToString(aodciva))
ruta.Parameters.AddWithValue("@preciototalconiva", aodcdt.Rows(i).Cells(4).Value)
ruta.Parameters.AddWithValue("@creadopor", Convert.ToString("Martin")) ' debe ir el nombre del usuario
ruta.Parameters.AddWithValue("@fechadecreacion", Convert.ToString(Format(DateTime.Now, "dd/MM/yyyy")))
ruta.Parameters.AddWithValue("@EditadoPor", Convert.ToString(""))
ruta.Parameters.AddWithValue("@FechaDeEdicion", Convert.ToString(""))
ruta.ExecuteNonQuery()
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
If Conn.State = ConnectionState.Open Then
Conn.Close()
Conn.Dispose()
End If
End Sub
Searching the internet I came across this post.
Link to StackOverflow post with similar problem
I adapted your case to my possible inconvenience but nevertheless the message Exception
continues to appear. I hope I have been clear enough with my problem. In case of any doubt, I will try to answer it as soon as possible.
Thank you very much in advance
The passing of parameters in this case is correct.
The problem is that the grid is being traversed, and not all the grid fields are filled.
In this case, the cause of the error is that the grid has turned on the property that allows adding new rows directly on it. However, that row exists and is counted, even if it is added later and the same grid does it.
So when doing:
Actually, two rows are being traversed (guiding us by the example), the row that has data, and one that does not have data, which is the newrow.
To avoid these problems, what you have to do is check if the row has a value in any field, and if not skip it.
a possible solution is this:
And another way would be:
Continue It is used to transfer control back to the For statement in this case.