-
Dec 10th, 2020, 08:59 AM
#1
Thread Starter
New Member
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.
-
Dec 10th, 2020, 09:25 AM
#2
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
-
Dec 10th, 2020, 10:24 AM
#3
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
-
Dec 11th, 2020, 03:16 PM
#4
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))
-
Dec 11th, 2020, 09:34 PM
#5
Re: DBNull to Textbox Value
Originally Posted by dday9
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))
-
Dec 11th, 2020, 09:40 PM
#6
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.
-
Dec 12th, 2020, 01:16 PM
#7
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
-
Jan 5th, 2021, 11:20 PM
#8
Member
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 >
-
Jan 6th, 2021, 02:21 AM
#9
Re: DBNull to Textbox Value
Originally Posted by Sherin
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?
-
Jan 7th, 2021, 11:42 PM
#10
Lively Member
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
-
Jan 8th, 2021, 12:23 AM
#11
Re: DBNull to Textbox Value
Originally Posted by Prahlad
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|