2

Having problem with the code below in a web service. Have searched for a solution but nothing that I have seen seems different to what I am doing below.

NB: The string variable 'AccountNo' is a passed into a function which includes the code below.

The error is generated on the last line of code - ExecuteReader.

    Dim sConnString As String
    Dim rdr As OleDbDataReader
    Dim orderPaid As Decimal
    Dim fbeused As Decimal

    sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\orders.mdb'"

    Dim conn As New OleDbConnection(sConnString)

    Dim sb As New StringBuilder
    sb.Append("SELECT DISTINCTROW OrderHeaders.Accountno, Sum(([paidqty]*[unitprice])*[orderheaders].[entpercent]/100) AS orderpaid, Sum([freeqty]*[unitprice]) AS fbeused")
    sb.Append(" FROM OrderHeaders INNER JOIN OrderDetails ON OrderHeaders.[OrderNo] = OrderDetails.[OrderNo]")
    sb.Append(" GROUP BY OrderHeaders.Accountno HAVING OrderHeaders.Accountno=?")
    Dim sqlString As String = sb.ToString

    Dim cmd As New OleDbCommand(sqlString, conn)
    cmd.CommandType = CommandType.Text
    'cmd.Parameters.AddWithValue("AccNo", AccountNo)
    cmd.Parameters.Add("AccNo", OleDbType.VarWChar).Value = AccountNo
    conn.Open()

    rdr = cmd.ExecuteReader()

The error I get is (as mentioned above)

Parameter ?_1 has no default value
4

4 回答 4

7

It's a shame that the top two StackOverflow results currently in Google for searches involving

Parameter ?_ has no default value

both have the questioner coming back in and saying there were flaws in their original question or their test data or whatever (though it's great that questioners check back in).

The explanation for this error (as encountered in normal situations) is supplied by Marc Gravell here:

Parameters with a .Value of null are not passed. At all.

You need to pass DBNull.Value instead to pass a semantic null. For example:

com.Parameters.Add("@p7", OleDbType.Char, 255).Value =
         ((object)values7[0]) ?? DBNull.Value; (etc)
于 2015-01-16T16:26:59.857 回答
0

这个问题实际上有一个错误的假设,那就是代码中存在错误。

SQL 查询的语法正确,并且正确插入了参数。但是,测试数据包含错误,因此格式正确的查询没有返回任何结果。

感谢大家的意见。

于 2014-01-26T22:32:21.353 回答
0

你有什么看起来正确的“?” 作为添加到命令的参数的参数占位符。您当前将参数标识为 OleDbType.VarWChar。这是故意的吗?你在处理 unicode 数据吗?我怀疑在这种情况下不会。尝试更改为 OleDbType.Char,它也被表示为处理 System.String 值。

您还可以通过使用确保您得到一个字符串

AccountNo.ToString()

于 2014-01-21T18:20:13.350 回答
-1

Duplicated of: OleDbCommand parameters order and priority

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

// Create SQL with ? for each parameter
String sql = "SELECT Address FROM Adresses WHERE Country = ? AND City = ?";
OleDbCommand command = new OleDbCommand(sql , connection);

// Add to command each paramater VALUE by position. 
// One parameter value for ?
command.Parameters.Add("My City") ;
command.Parameters.Add("My Country") ;

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx

于 2014-01-21T16:31:38.327 回答