Insert null from TextBox to integer Field

Discussion in 'ASP .Net' started by Fraggle, May 7, 2004.

  1. Fraggle

    Fraggle Guest

    I have an asp.net page, I have a text box where people can enter an
    integer (age). I wish to store this in a SQLServer Database. If the
    person enters no age then I would store a NULL.

    However if the textbox is left blank, I get an error
    "[InvalidCastException: Cast from string "" to type 'Integer' is not
    valid.]"

    How can I work around this, and get the null into the db?

    Cheers

    Fragg
    Fraggle, May 7, 2004
    #1
    1. Advertising

  2. You have to use Convert.DBNul

    For example (using a stored procedure)

    int intValueFromTextBox = Convert.ToInt32(txtTextBox.Text)
    if(intValueFromTextBox ==null

    cmdInsertRecord.SelectCommand.Parameters["@nAge"].Value = Convert.DBNull

    els

    cmdInsertRecord.SelectCommand.Parameters["@nAge"].Value = intValueFromTextBox
    }
    =?Utf-8?B?RCBTaGVsZG9u?=, May 7, 2004
    #2
    1. Advertising

  3. Fraggle

    avnrao Guest

    how are you accessing db? with stored procedures? when setting the parameter
    value check if the text box value is "" and set the value to null.
    Av.

    "Fraggle" <> wrote in message
    news:...
    >I have an asp.net page, I have a text box where people can enter an
    > integer (age). I wish to store this in a SQLServer Database. If the
    > person enters no age then I would store a NULL.
    >
    > However if the textbox is left blank, I get an error
    > "[InvalidCastException: Cast from string "" to type 'Integer' is not
    > valid.]"
    >
    > How can I work around this, and get the null into the db?
    >
    > Cheers
    >
    > Fragg
    avnrao, May 7, 2004
    #3
  4. If you're using a SQL Statment, you can simply omit the column from the
    INSERT SQL statement.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "Fraggle" <> wrote in message
    news:...
    > I have an asp.net page, I have a text box where people can enter an
    > integer (age). I wish to store this in a SQLServer Database. If the
    > person enters no age then I would store a NULL.
    >
    > However if the textbox is left blank, I get an error
    > "[InvalidCastException: Cast from string "" to type 'Integer' is not
    > valid.]"
    >
    > How can I work around this, and get the null into the db?
    >
    > Cheers
    >
    > Fragg
    Kevin Spencer, May 7, 2004
    #4
  5. What DB are you using? if you're using SQL, What type did you give to your
    Age Column (int, string, etc?)make sure it matches the cast type you are
    getting back from the DB.
    Go to the Age COlumn in you table and select Allow Nulls.

    let me know

    Patrick

    "avnrao" <> wrote in message
    news:...
    > how are you accessing db? with stored procedures? when setting the

    parameter
    > value check if the text box value is "" and set the value to null.
    > Av.
    >
    > "Fraggle" <> wrote in message
    > news:...
    > >I have an asp.net page, I have a text box where people can enter an
    > > integer (age). I wish to store this in a SQLServer Database. If the
    > > person enters no age then I would store a NULL.
    > >
    > > However if the textbox is left blank, I get an error
    > > "[InvalidCastException: Cast from string "" to type 'Integer' is not
    > > valid.]"
    > >
    > > How can I work around this, and get the null into the db?
    > >
    > > Cheers
    > >
    > > Fragg

    >
    >
    Patrick Delifer, May 7, 2004
    #5
  6. Fraggle

    ak_ambi

    Joined:
    Oct 13, 2008
    Messages:
    1
    In WindowsForms I was able to do that this way:

    txtNummer.DataBindings["Text"].NullValue = "";
    ak_ambi, Oct 13, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FatboyCanteen

    About Insert Null to DateTime Field

    FatboyCanteen, Nov 11, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    21,299
    Prashant Kondle
    Nov 11, 2003
  2. Replies:
    2
    Views:
    439
    =?Utf-8?B?Q2FsbCBKYXZhc2NyaXAgZnJvbSB0aGUgc2VydmVy
    Sep 26, 2005
  3. Replies:
    5
    Views:
    26,498
    Mike Schilling
    Mar 29, 2006
  4. tanmaypatankar
    Replies:
    0
    Views:
    1,073
    tanmaypatankar
    Dec 18, 2007
  5. Keith

    Insert NULL if Form Field Blank

    Keith, Jun 9, 2004, in forum: ASP General
    Replies:
    1
    Views:
    143
Loading...

Share This Page