|
 |
aspx thread: Databound Control : Databind : GetPropertyValue : Checking for null && Only Way?
Message #1 by "Chris Kersey" <ckersey@m...> on Sat, 15 Jun 2002 11:47:24 -0700
|
|
I have 2 questions I'm hoping someone can help me to clear up. I've been
creating a databound web control that contains basically 3 properties (for
now) that I'm trying to initialize from the datasource. According to a wrox
book that I have (Professional ASP.NET Server Controls...), I can declare a
property named _datasource of type IEnumerable, then assign my resultset to
this datasource variable and then iterate using the .MoveNext() method.
Up to this point, everything is fine and dandy. I can access the database
column using the following syntax, up to the first GetPropertyValue...
after that, my app throws an "invalid cast" exception at the first (int)
cast I use...: I think I have deduced that the problem is that my database
column is a nullable column and so naturally trying to cast (int)ParentId
null is a nono.
Here is some of the source code that is throwing the exception.
// IEnumerator links;
while(links.MoveNext())
{
string Url = (string)Databinder.GetPropertyValue(links.Current, "Url");
/* EXCEPTION THROWN ON FOLLOWING: ParentId == null on first pass */
int ParentId = (int)Databinder.GetPropertyValue(links.Current,
"ParentId");
int LinkId = (int)Databinder.GetPropertyValue(links.Current,
"LinkId");
}
Question 1: is there a way to check for null using the
DataBinder.GetPropertyValue in order to avoid an invalid cast error?
Question 2: is there another way to extract values (other than using
GetPropertyValue()) from the IEnumerator variable "links"?
Thanks,
Chris
Message #2 by "Mingkun Goh" <mangokun@h...> on Sun, 16 Jun 2002 18:22:40
|
|
You can check against any database field for null value using the syntax
below:
if (data != System.DBNull.Value)
{
// Do your stuff
}
Similarly, you can use the IsDBNull Function
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/vblr7/html/vafctisnull.asp
Prevous message:
> I have 2 questions I'm hoping someone can help me to clear up. I've been
creating a databound web control that contains basically 3 properties (for
now) that I'm trying to initialize from the datasource. According to a
wrox
book that I have (Professional ASP.NET Server Controls...), I can declare a
property named _datasource of type IEnumerable, then assign my resultset to
this datasource variable and then iterate using the .MoveNext() method.
Up to this point, everything is fine and dandy. I can access the database
column using the following syntax, up to the first GetPropertyValue...
after that, my app throws an "invalid cast" exception at the first (int)
cast I use...: I think I have deduced that the problem is that my database
column is a nullable column and so naturally trying to cast (int)ParentId
null is a nono.
Here is some of the source code that is throwing the exception.
// IEnumerator links;
while(links.MoveNext())
{
string Url = (string)Databinder.GetPropertyValue(links.Current, "Url");
/* EXCEPTION THROWN ON FOLLOWING: ParentId == null on first pass */
int ParentId = (int)Databinder.GetPropertyValue(links.Current,
"ParentId");
int LinkId = (int)Databinder.GetPropertyValue(links.Current,
"LinkId");
}
Question 1: is there a way to check for null using the
DataBinder.GetPropertyValue in order to avoid an invalid cast error?
Question 2: is there another way to extract values (other than using
GetPropertyValue()) from the IEnumerator variable "links"?
Thanks,
Chris
Message #3 by Feduke Cntr Charles R <FedukeCR@m...> on Mon, 17 Jun 2002 07:30:42 -0400
|
|
Chris,
Aside from the checking for DBNull, you can't implicitly cast
something as an int unless its some other numeric data type. Since you're
probably dealing with an object data type, you might have to go as far as:
// dr = your SqlDataReader
int something = Int32.Parse(dr["user_id"].ToString());
// or in your case
int something2 = Int32.Parse(
DataBinder.GetPropertyValue(links.Current,
"ParentId").ToString());
HTH,
- Chuck
-----Original Message-----
From: Mingkun Goh [mailto:mangokun@h...]
Sent: Sunday, June 16, 2002 2:23 PM
To: ASP+
Subject: [aspx] Re: Databound Control : Databind : GetPropertyValue :
Checking for null && Only Way?
You can check against any database field for null value using the syntax
below:
if (data != System.DBNull.Value)
{
// Do your stuff
}
Similarly, you can use the IsDBNull Function
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/vblr7/html/vafctisnull.asp
Prevous message:
> I have 2 questions I'm hoping someone can help me to clear up. I've been
creating a databound web control that contains basically 3 properties (for
now) that I'm trying to initialize from the datasource. According to a
wrox
book that I have (Professional ASP.NET Server Controls...), I can declare a
property named _datasource of type IEnumerable, then assign my resultset to
this datasource variable and then iterate using the .MoveNext() method.
Up to this point, everything is fine and dandy. I can access the database
column using the following syntax, up to the first GetPropertyValue...
after that, my app throws an "invalid cast" exception at the first (int)
cast I use...: I think I have deduced that the problem is that my database
column is a nullable column and so naturally trying to cast (int)ParentId
null is a nono.
Here is some of the source code that is throwing the exception.
// IEnumerator links;
while(links.MoveNext())
{
string Url = (string)Databinder.GetPropertyValue(links.Current, "Url");
/* EXCEPTION THROWN ON FOLLOWING: ParentId == null on first pass */
int ParentId = (int)Databinder.GetPropertyValue(links.Current,
"ParentId");
int LinkId = (int)Databinder.GetPropertyValue(links.Current,
"LinkId");
}
Question 1: is there a way to check for null using the
DataBinder.GetPropertyValue in order to avoid an invalid cast error?
Question 2: is there another way to extract values (other than using
GetPropertyValue()) from the IEnumerator variable "links"?
Thanks,
Chris
|
|
 |