locked
how to insert null value RRS feed

  • Question

  • hi, All

     

    hear is my statement

    i am trying to insert null value through statement

    if (txtBankAcNo.Text == "") { sSqlDbCommand.Parameters["@BankAcNo"].Value = null; }
                            else { sSqlDbCommand.Parameters["@BankAcNo"].Value = txtBankAcNo.Text; }

    but is throwing Error: expects the parameter '@BankAcNo' which is not supplied

     

    Regards!

     

    Hitesh

     

     

    Tuesday, August 31, 2010 7:23 AM

Answers

  • Hi Hitesh

    You could also try passing in DBNull.Value.

    Regards

    James


    James Sales MCP, MCTS - Senior Software Developer - Solihull, England
    • Proposed as answer by Naomi N Tuesday, August 31, 2010 2:15 PM
    • Marked as answer by Hitesh1508 Thursday, September 2, 2010 6:44 AM
    Tuesday, August 31, 2010 7:57 AM

All replies

  • Hi,

    If parameter is declared as NULL, you don't need to pass it.

     

    IF BankNo.text <> ""

     sSqlDbCommand.Parameters["@BankAcNo"].Value = BankNo.text

     

    Thanks,


    Tejas Shah
    SQL YOGA
    • Edited by Shah Tejas Tuesday, August 31, 2010 7:29 AM typo
    • Proposed as answer by Kalman Toth Tuesday, August 31, 2010 7:39 AM
    Tuesday, August 31, 2010 7:28 AM
  • In stored procedure, the parameter @BankAcNo is not defined as nullable.
    My Blog
    • Proposed as answer by Kalman Toth Tuesday, August 31, 2010 7:38 AM
    Tuesday, August 31, 2010 7:29 AM
  • Hello,

    The stored procedure should be like below

    CREATE PROCEDURE Procedure_Name
    (
     @BankAcNo INT = NULL
    )
    AS
    BEGIN
    // Logic goes over here

    END

    Hope its clear & helpful....


    Pavan Kokkula Infosys Technologies Limited.
    • Proposed as answer by Kalman Toth Tuesday, August 31, 2010 7:38 AM
    Tuesday, August 31, 2010 7:34 AM
  • Hi Hitesh

    You could also try passing in DBNull.Value.

    Regards

    James


    James Sales MCP, MCTS - Senior Software Developer - Solihull, England
    • Proposed as answer by Naomi N Tuesday, August 31, 2010 2:15 PM
    • Marked as answer by Hitesh1508 Thursday, September 2, 2010 6:44 AM
    Tuesday, August 31, 2010 7:57 AM
  • Still, the parameter in stored procedure should be nullable otherwise it will generate error.
    My Blog
    Wednesday, September 1, 2010 10:19 AM