Search code examples
vb.netoracleparameter-passingdbnulloracle-manageddataaccess

Oracle.ManagedDataAccess.Client and add parameter with addwithvalue / DBNull


this i my query and parameter list i got msg "Not All Variable bound"

Dim cmd As New OracleCommand("select POLICY_REFER, ....

from   POLICY_MASTER
where  ISSUE_DATE BETWEEN :DFrom AND :DTo 
                           And    (CLIENT_NAME = :client or :client is null)  
                           and    (POLICY_PRODUCT = :product or :product is null)
                           and DOC_STATUS = 'Parked'", sgcnn)

cmd.Parameters.Add("@DFrom", OracleDbType.Date).Value = DtpFrom.Value.ToShortDateString
            cmd.Parameters.Add("@DTo", OracleDbType.Date).Value = DtpTo.Value.ToShortDateString
            cmd.Parameters.Add("@Product", OracleDbType.Varchar2).Value = CmbProduct.Text
            cmd.Parameters.Add("@Client", OracleDbType.Varchar2).Value = CmbClient.Text

if i used just client_name in query in parameter list then it work but when i used client_name and product i got msg "Not All Variable bound"


Solution

  • You SQL string has six parameters

    **:DFrom** AND **:DTo** 
    And    (CLIENT_NAME = **:client** or **:client** is null)  
    and    (POLICY_PRODUCT = **:product** or **:product** 
    

    Thus you must provide six parameters:

    cmd.Parameters.Add("DFrom", OracleDbType.Date).Value = DtpFrom.Value
    cmd.Parameters.Add("DTo", OracleDbType.Date).Value = DtpTo.Value
    cmd.Parameters.Add("Client", OracleDbType.Varchar2).Value = IIf(String.IsNullOrEmpty(CmbClient.Text), DBNull.Value, CmbClient.Text)
    cmd.Parameters.Add("Client", OracleDbType.Varchar2).Value = IIf(String.IsNullOrEmpty(CmbClient.Text), DBNull.Value, CmbClient.Text)
    cmd.Parameters.Add("Product", OracleDbType.Varchar2).Value = IIf(String.IsNullOrEmpty(CmbProduct.Text), DBNull.Value, CmbProduct.Text)
    cmd.Parameters.Add("Product", OracleDbType.Varchar2).Value = IIf(String.IsNullOrEmpty(CmbProduct.Text), DBNull.Value, CmbProduct.Text)
    

    I don't know whether @ is permitted/required for parameter name. Then you should remove ToShortDateString. Parameter is declared as OracleDbType.Date, so just provide the DateTime value, you don't have to consider any format topics - that's a major benefit of using bind parameters instead of putting the values as strings.