Question

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
Was it helpful?

Solution 3

The question in fact had a wrong assumption and that was that there was an error in the code.

The syntax of the SQL query was correct and the parameter was being inserted correctly. However the test data contained errors and therefore no result was being returned by a correctly formatted query.

Thanks all for input.

OTHER TIPS

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)

What you have looks correct with the "?" as the parameter place-holder for the parameters added to the command. You currently have the parameter identified as OleDbType.VarWChar. Is that intended? Are you dealing with unicode data? I suspect not in this case. Try changing to OleDbType.Char which also is represented to handle System.String values.

You could also make sure you are getting a string by using

AccountNo.ToString()

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top