Results 1 to 11 of 11

Thread: DBNull to Textbox Value

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    10

    Question DBNull to Textbox Value

    Hi there,
    Can anyone help me out with DBNull Value in c# programming. I want to set textbox value =1 if there is no value in sql database.
    I am using datareader here to get values from sql DB.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,805

    Re: DBNull to Textbox Value

    If only there was a way to check IF the value equals DBNULL then you could set it to 1.... if only there was some sort of construct that allowed you to do that..... hmmmm...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,490

    Re: DBNull to Textbox Value

    Hmmmm... or maybe a way in the SQL to return 1 if NULL....
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,963

    Re: DBNull to Textbox Value

    To GaryMazzone's point, you can use COALESCE on the field. This is what the syntax looks like:
    Code:
    COALESCE(Table.Column, 1)
    Here is an example: http://sqlfiddle.com/#!9/e5a45c/1

    To techgnome's point, you can use a ternary if on the condition of if DBNull.Value equals the column value. This is what the Syntax would look like:
    Code:
    int value = (DBNull.Value.equals(reader.GetInt32(0)) ? 1 : reader.GetInt32(0))
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,509

    Re: DBNull to Textbox Value

    Quote Originally Posted by dday9 View Post
    To techgnome's point, you can use a ternary if on the condition of if DBNull.Value equals the column value. This is what the Syntax would look like:
    Code:
    int value = (DBNull.Value.equals(reader.GetInt32(0)) ? 1 : reader.GetInt32(0))
    I'm fairly sure that that would fail because GetInt32 can only get an Int32. The data reader has its own method for testing for NULL:
    Code:
    int value = (reader.IsDBNull(0) ? 1 : reader.GetInt32(0))

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,509

    Re: DBNull to Textbox Value

    For the record, the term "sql database" is basically meaningless. SQL is the language used by virtually all databases for queries and data definition so pretty much every database is a SQL database in that it is a database that uses the SQL language. Many people rather lazily refer to Microsoft SQL Server as just SQL but plenty of people use the same term to describe many other databases too, many of which have "SQL" in the name, e.g. MySQL, PostgreSQL, SQLite and many others. If you really do mean SQL Server then you should say SQL Server or at least MS SQL so that there is no confusion. If you don't mean SQL Server then you'll likely get invalid advice because the T-SQL variant used by SQL Server has differences to the SQL flavours used by other database systems, e.g. I'm not sure off the top of my head but I believe that that COALESCE function is specific to SQL Server.

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,490

    Re: DBNull to Textbox Value

    The ISNULL(column,value) (or NLV in Oracle) is part of the ANSI standard and would work with any database that is ANSI compliant.

    COALESCE is defined by the ISO/ANSI SQL standards
    Last edited by GaryMazzone; Dec 12th, 2020 at 01:24 PM.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  8. #8
    Member
    Join Date
    Jul 2019
    Location
    Ahmedabad
    Posts
    57

    Re: DBNull to Textbox Value

    Hello,@arvinddx
    Please try this code, To DBNull to Textbox Value

    Code:
    CREATE TABLE dbo.doc_exz (column_a INT, column_b INT);
    GO
    INSERT INTO dbo.doc_exz (column_a) VALUES (7);
    GO
    ALTER TABLE dbo.doc_exz
      ADD CONSTRAINT DF_Doc_Exz_Column_B
      DEFAULT 50 FOR column_b;
    GO
    I hope this code will be useful to you.
    Thank you.
    < advertising removed by moderator >

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,509

    Re: DBNull to Textbox Value

    Quote Originally Posted by Sherin View Post
    Hello,@arvinddx
    Please try this code, To DBNull to Textbox Value

    Code:
    CREATE TABLE dbo.doc_exz (column_a INT, column_b INT);
    GO
    INSERT INTO dbo.doc_exz (column_a) VALUES (7);
    GO
    ALTER TABLE dbo.doc_exz
      ADD CONSTRAINT DF_Doc_Exz_Column_B
      DEFAULT 50 FOR column_b;
    GO
    I hope this code will be useful to you.
    Thank you.
    Can you explain exactly how that should be useful because I'm not seeing it. There's nothing there about DBNull or NULL values at all and nothing about TextBoxes. Is this another ploy to get your advertising seen that the mods have already removed from your signature?

  10. #10
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: DBNull to Textbox Value

    You can use the string.IsNullOrEmpty or string.IsNullOrWhiteSpace methods to check your TextBox value.

    Code:
    string yourValueToPutIntoDatabase = (string.IsNullOrEmpty(yourTextBox.Text)) ? yourTextBox.Text : null;
    You could also use it in an if-statement if you are more comfortable :

    Code:
    string valueToPutInDatabase;
    
    if(string.IsNullOrEmpty(yourTextBox.Text))
    {
         //Your textbox was empty
         valueToPutInDatabase = null;
    }
    else
    {
         valueToPutInDatabase = yourTextBox.Text;
    }


    Reference: Link

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,509

    Re: DBNull to Textbox Value

    Quote Originally Posted by Prahlad View Post
    You can use the string.IsNullOrEmpty or string.IsNullOrWhiteSpace methods to check your TextBox value.

    Code:
    string yourValueToPutIntoDatabase = (string.IsNullOrEmpty(yourTextBox.Text)) ? yourTextBox.Text : null;
    You could also use it in an if-statement if you are more comfortable :

    Code:
    string valueToPutInDatabase;
    
    if(string.IsNullOrEmpty(yourTextBox.Text))
    {
         //Your textbox was empty
         valueToPutInDatabase = null;
    }
    else
    {
         valueToPutInDatabase = yourTextBox.Text;
    }


    Reference: Link
    The OP said:
    I want to set textbox value =1 if there is no value in sql database.
    How does what you have provided help with that?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width