reading NULL from a field in a database

  • Thread starter Thread starter nabil m
  • Start date Start date
N

nabil m

please help - i have a field that is null in my sql database - i am using C#
i getthis error everytimethe fild is null - how do i check to see if the
field in my DB is null then output an empty string ?
please help - thx in advance


Line 757: get {
Line 758: try {
Line 759: return
((string)(this[this.tablest_table_contactInfo.ContactDOBColumn]));
Line 760: }
Line 761: catch (InvalidCastException e) {
 
nabil m said:
please help - i have a field that is null in my sql database - i am
using C# i getthis error everytimethe fild is null - how do i check
to see if the field in my DB is null then output an empty string ?
please help - thx in advance


Line 757: get {
Line 758: try {
Line 759: return
((string)(this[this.tablest_table_contactInfo.ContactDOBColumn]));
Line 760: }
Line 761: catch (InvalidCastException e) {

You can use the DBNull class. For more info:
http://msdn.microsoft.com/library/d...us/cpref/html/frlrfSystemDBNullClassTopic.asp
 
thank you

i tried this - i think i have it wrong - gettingsame error
if (this.dataSet21.st_table_contactInfo[0].ContactDOB.ToString() !=
System.DBNull.Value)

this.TextBoxDOB.Text =
this.dataSet21.st_table_contactInfo[0].ContactDOB.ToString();

Davide Vernole said:
nabil m said:
please help - i have a field that is null in my sql database - i am
using C# i getthis error everytimethe fild is null - how do i check
to see if the field in my DB is null then output an empty string ?
please help - thx in advance


Line 757: get {
Line 758: try {
Line 759: return
((string)(this[this.tablest_table_contactInfo.ContactDOBColumn]));
Line 760: }
Line 761: catch (InvalidCastException e) {

You can use the DBNull class. For more info:
http://msdn.microsoft.com/library/d...us/cpref/html/frlrfSystemDBNullClassTopic.asp
 
nabil m said:
thank you

i tried this - i think i have it wrong - gettingsame error
if (this.dataSet21.st_table_contactInfo[0].ContactDOB.ToString() !=
System.DBNull.Value)

this.TextBoxDOB.Text =
this.dataSet21.st_table_contactInfo[0].ContactDOB.ToString();

don't use the ToString() method in the if condition !
 
when i remove the ToString in the If statement i get these 2 compile error
test\contactForm.aspx.cs(86): Cannot implicitly convert type 'string' to
'bool'
test\contactForm.aspx.cs(86): Cannot implicitly convert type 'System.DBNull'
to 'string'

thx nabil

Davide Vernole said:
nabil m said:
thank you

i tried this - i think i have it wrong - gettingsame error
if (this.dataSet21.st_table_contactInfo[0].ContactDOB.ToString() !=
System.DBNull.Value)

this.TextBoxDOB.Text =
this.dataSet21.st_table_contactInfo[0].ContactDOB.ToString();

don't use the ToString() method in the if condition !
 
nabil m said:
when i remove the ToString in the If statement i get these 2 compile
error test\contactForm.aspx.cs(86): Cannot implicitly convert type
'string' to 'bool'
test\contactForm.aspx.cs(86): Cannot implicitly convert type
'System.DBNull' to 'string'

thx nabil

I said to remove the ToString() in the condition not in the code inside the
parenthesis. Don't worry ! Try a code like this:

if(MyDataSet.Tables[0].Rows[0]["MyField"] != System.DBNull.Value)
{
myTextBox.Text = MyDataSet.Tables[0].Rows[0]["MyField"].ToString();
}

Also verify that the DataSet is not null and that the Rows collection of the
DataTable contains at least one row.
 
i dont know ..but it is not working
if (this.dataSet21.st_table_contactInfo[0].ContactDOB !=
System.DBNull.Value)

comiler error
test\contactForm.aspx.cs(86): Operator '!=' cannot be applied to operands of
type 'string' and 'System.DBNull'

Davide Vernole said:
nabil m said:
when i remove the ToString in the If statement i get these 2 compile
error test\contactForm.aspx.cs(86): Cannot implicitly convert type
'string' to 'bool'
test\contactForm.aspx.cs(86): Cannot implicitly convert type
'System.DBNull' to 'string'

thx nabil

I said to remove the ToString() in the condition not in the code inside
the parenthesis. Don't worry ! Try a code like this:

if(MyDataSet.Tables[0].Rows[0]["MyField"] != System.DBNull.Value)
{
myTextBox.Text = MyDataSet.Tables[0].Rows[0]["MyField"].ToString();
}

Also verify that the DataSet is not null and that the Rows collection of
the DataTable contains at least one row.
 
nabil m said:
i dont know ..but it is not working
if (this.dataSet21.st_table_contactInfo[0].ContactDOB !=
System.DBNull.Value)

comiler error
test\contactForm.aspx.cs(86): Operator '!=' cannot be applied to
operands of type 'string' and 'System.DBNull'

Davide Vernole said:
nabil m said:
when i remove the ToString in the If statement i get these 2 compile
error test\contactForm.aspx.cs(86): Cannot implicitly convert type
'string' to 'bool'
test\contactForm.aspx.cs(86): Cannot implicitly convert type
'System.DBNull' to 'string'

thx nabil

I said to remove the ToString() in the condition not in the code
inside the parenthesis. Don't worry ! Try a code like this:

if(MyDataSet.Tables[0].Rows[0]["MyField"] != System.DBNull.Value)
{
myTextBox.Text =
MyDataSet.Tables[0].Rows[0]["MyField"].ToString(); }

Also verify that the DataSet is not null and that the Rows
collection of the DataTable contains at least one row.

You're right. Your are using a typed Dataset ! In this case you should use
IsContactDOBNull property. Read this KB for more info:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;310371

Try this:

if (!this.dataSet21.st_table_contactInfo[0].IsContactDOBNull)
{
//insert here your code for not null value
}
 
Got it now !!
thank you so much i learned something now

Davide Vernole said:
nabil m said:
i dont know ..but it is not working
if (this.dataSet21.st_table_contactInfo[0].ContactDOB !=
System.DBNull.Value)

comiler error
test\contactForm.aspx.cs(86): Operator '!=' cannot be applied to
operands of type 'string' and 'System.DBNull'

Davide Vernole said:
nabil m <[email protected]> typed:
when i remove the ToString in the If statement i get these 2 compile
error test\contactForm.aspx.cs(86): Cannot implicitly convert type
'string' to 'bool'
test\contactForm.aspx.cs(86): Cannot implicitly convert type
'System.DBNull' to 'string'

thx nabil

I said to remove the ToString() in the condition not in the code
inside the parenthesis. Don't worry ! Try a code like this:

if(MyDataSet.Tables[0].Rows[0]["MyField"] != System.DBNull.Value)
{
myTextBox.Text =
MyDataSet.Tables[0].Rows[0]["MyField"].ToString(); }

Also verify that the DataSet is not null and that the Rows
collection of the DataTable contains at least one row.

You're right. Your are using a typed Dataset ! In this case you should use
IsContactDOBNull property. Read this KB for more info:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;310371

Try this:

if (!this.dataSet21.st_table_contactInfo[0].IsContactDOBNull)
{
//insert here your code for not null value
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top