Parameters.Add failing in Dictionary loops
I have written a copy & paste Class to be used by WebMethods.
INSERT and DELETE versions have been written successfully with WHEREs;
however, I'm getting a strange error now that I'm trying to do a UPDATE
version.
The error is The parameterized query '(@NotesComments
nvarchar(4000),@StoreNumber nvarchar(4000),@PONu' expects the parameter
'@NotesComments', which was not supplied.
The strange part about this error is
The source of data that created the @NotesComments portion of query is
used to create the SQLParameter in nearly the same way
Outputting the data the server claims is missing as Strings gives no
indication of a problem
This code simply takes an Array of Arrays ((,) doesn't work with
WebMethods) called Inputs. Previous error checking Functions gives
ValidRowsImport which is a List of the valid first dimension indexes for
Inputs. ColumnTypes is a good idea gone wrong that is used to type check
Inputs.
It builds the query, checks the Types, Adds the SQLParameters, INSERTs,
and finally outputs a List of the successfully INSERTed rows taken from
Inputs. Similar code without Arrays of Dictionarys works just fine.
Is there some scope or ordering issue when it comes to SQLParameters? How
can this error be resolved?
class
Public Shared Function UpdateRowsWithDateColumnsWheredByInputs(ByVal
Inputs() As Object, ByVal ColumnTypes As Dictionary(Of Integer, Type),
ByVal ValueAndWhereColumns() As Dictionary(Of Integer, String), ByVal
ValidRowsImport As List(Of Integer), ByVal TableName As String, ByVal
connString As Object) As List(Of Integer)
Dim InsertedRows As New List(Of Integer)
Dim conn As New SqlConnection
conn.ConnectionString = connString
Dim query As New StringBuilder
query.Append("UPDATE " & TableName & " SET ")
For Each Pair In ValueAndWhereColumns(0)
query.Append(Pair.Value & " = @" & Pair.Value)
If Pair.Key <> ValueAndWhereColumns(0).Last.Key Then
query.Append(",")
End If
Next
query.Append(" WHERE")
For Each Pair In ValueAndWhereColumns(1)
If Pair.Key <> ValueAndWhereColumns(1).First.Key Then
query.Append(" AND")
End If
query.Append(" " & Pair.Value & " = @" & Pair.Value)
Next
For Each i As Integer In ValidRowsImport
Dim sqlCommand = New SqlCommand(query.ToString, conn)
For t As Integer = 0 To 1
For Each Pair In ValueAndWhereColumns(t)
If ColumnTypes.ContainsKey(Pair.Key) Then
Dim InsertValue
Select Case ColumnTypes(Pair.Key).Name
Case "Decimal"
Decimal.TryParse(Inputs(i)(Pair.Key).ToString,
InsertValue)
Case "Int32"
Integer.TryParse(Inputs(i)(Pair.Key).ToString,
InsertValue)
Case "DateTime"
If Inputs(i)(Pair.Key).ToString.Length > 0 Then
Date.TryParse(Inputs(i)(Pair.Key),
InsertValue)
End If
End Select
sqlCommand.Parameters.Add(New SqlParameter("@" &
Pair.Value, InsertValue))
Else
sqlCommand.Parameters.Add(New SqlParameter("@" &
Pair.Value, Inputs(i)(Pair.Key)))
End If
Next
Next
conn.Open()
If sqlCommand.ExecuteNonQuery() > 0 Then
InsertedRows.Add(i)
End If
conn.Close()
Next
Return InsertedRows
End Function
test execution
Dim stuff = CopyAndPaste.UpdateRowsWithDateColumnsWheredByInputs(
New Object() {
New Object() {
"2231",
"6610056165",
"100016764",
"DudeBrahWoah"
}
},
New Dictionary(Of Integer, Type) From {
{0, GetType(String)},
{1, GetType(String)},
{2, GetType(String)},
{3, GetType(String)}
},
{
New Dictionary(Of Integer, String) From
{
{3, "NotesComments"}
},
New Dictionary(Of Integer, String) From
{
{0, "StoreNumber"},
{1, "PONumber"},
{2, "ItemNumber"}
}
},
New List(Of Integer) From {0},
"SCM_SupplierManagementReport",
Module1.DBConn
)
No comments:
Post a Comment