|
 |
aspx thread: Re: Databound Control : GetPropertyValue Only Way To extract data?
Message #1 by "Chris Kersey" <ckersey@m...> on Mon, 17 Jun 2002 11:44:45 -0700
|
|
Checking for DBNull does the trick, thanks.
Does anyone happen to have the answer for the other question I raised
concerning another way to pull data from the IEnumerable other than using
GetPropertyValue? From what I understand that method uses late binding
(reflection) correct? As such it would create a heavier overhead. Or is
the overhead incurred negligible considering the the application that wil be
using it is only a navigation component that will be pulling about 20
entries or less on average?
Hmm.. I should probably cache that data now that I'm thinking about it.
It's application wide and accessed on every page (or will be...)
Thanks for your input.
Chris
----- Original Message -----
From: "Feduke Cntr Charles R" <FedukeCR@m...>
To: "ASP+" <aspx@p...>
Sent: Monday, June 17, 2002 4:30 AM
Subject: [aspx] Re: Databound Control : Databind : GetPropertyValue :
Checking for null && Only Way?
> 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
>
>
Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Mon, 17 Jun 2002 15:38:20 -0400
|
|
Chris,
Is there any reason why you're using an IEnumerator and not a
*DataReader or another named class? Are you trying to make this accept
anything that implements IEnumerator? You'd get a bit better performance
using an actual class (like *DataReader) than you do with a generic
IEnumerator, but as you said you're looking at 20 records or so each so the
performance hit isn't going to be noticeable.
- Chuck
-----Original Message-----
From: Chris Kersey [mailto:ckersey@m...]
Sent: Monday, June 17, 2002 2:45 PM
To: ASP+
Subject: [aspx] Re: Databound Control : GetPropertyValue Only Way To
extract data?
Checking for DBNull does the trick, thanks.
Does anyone happen to have the answer for the other question I raised
concerning another way to pull data from the IEnumerable other than using
GetPropertyValue? From what I understand that method uses late binding
(reflection) correct? As such it would create a heavier overhead. Or is
the overhead incurred negligible considering the the application that wil be
using it is only a navigation component that will be pulling about 20
entries or less on average?
Hmm.. I should probably cache that data now that I'm thinking about it.
It's application wide and accessed on every page (or will be...)
Thanks for your input.
Chris
----- Original Message -----
From: "Feduke Cntr Charles R" <FedukeCR@m...>
To: "ASP+" <aspx@p...>
Sent: Monday, June 17, 2002 4:30 AM
Subject: [aspx] Re: Databound Control : Databind : GetPropertyValue :
Checking for null && Only Way?
> 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
>
>
|
|
 |