Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 3125: get {
Line 3126: try {
Line 3127: return ((global::System.DateTime)(this[this.tableAerDataTable.AerColumn]));
Line 3128: }
Line 3129: catch (global::System.InvalidCastException e) {
It works fine for my columns that have values in them.
I will try your code in
a few hours when I am home again.
Just for now. Assuming the header row is not part of dr then
it is impossible that the whole row is empty since I have
a primary key that is declared NOT NULL. - by the way,
the primary key field, which is not null, loads perfeclty fine
in my textbox. its the other stuff that is causing the issue (I think),
columns that are not necessary NOT NULL.
I have had similar issues in the past with table adapters and solved by setting the column default value to a valid value in the table adapter. such as a text field default = " ".
I tried your code example from today but it still doesnt work. thats so odd. why is it so difficult to assign a bloody value to a textbox and if its not there just dont assign any.
I cant assign a default value for "" or '' or - since I get the message that says that these are invalid.
thats right, I am using a data table with a table adapter, thats how the declaration looks:
Dim dt As AerDataTable
Dim dr As Aer_InpusIsNumberRow
dt = tPAttribute.GetAllAer(TextBoxAerNumber.Text)
dr = DirectCast(dt.Rows(0), Aer_InpusIsNumberRow)
If Not dr("Aer") Is Nothing AndAlso Not String.IsNullOrEmpty(dr("Aer").ToString()) Then
LabelAer.Text = dr("Aer").ToString()
End If
If Not dr("Aer2") Is Nothing AndAlso Not String.IsNullOrEmpty(dr("Aer2").ToString()) Then
LabelAer2.Text = dr("Aer2").ToString()
End If
...
My query, GetAllAe, works perfectly. Some columns are empty, some are not. So when I want to get Aer2, which is empty, I get the error.
polynaux
Member
195 Points
775 Posts
Retrieving row with some empty colmns: Specified cast is not valid.
Oct 21, 2008 07:25 PM|LINK
Hi Guys,
I wonder how to avoid the invalid cast when
the column I retrieve is empty:
If dr.IsNull("Aer") = False Then LabelAer.Text = dr.Aer.ToString()
The above does not do the trick.
Thanks
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 21, 2008 07:51 PM|LINK
If Not dr("Aer") is DbNull.Value AndAlso dr("Aer") IsNot Nothing Then LabelAer.Text = dr("Aer").ToString() End IfDkUltra
Contributor
2813 Points
546 Posts
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 22, 2008 06:44 PM|LINK
Try
If
Not dr("Aer") Is Nothing AndAlso Not String.IsNullOrEmpty(dr("Aer").tostring()) Then LabelAer.Text = dr("Aer").ToString() End IfHope this helps
DK
polynaux
Member
195 Points
775 Posts
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 22, 2008 06:55 PM|LINK
Hm , I tried both ways but I still get the error:
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 3125: get {
Line 3126: try {
Line 3127: return ((global::System.DateTime)(this[this.tableAerDataTable.AerColumn]));
Line 3128: }
Line 3129: catch (global::System.InvalidCastException e) {
It works fine for my columns that have values in them.
DkUltra
Contributor
2813 Points
546 Posts
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 22, 2008 09:48 PM|LINK
Ok, try this
see if the dr itself is not null first
IF not dr is nothing andalso not dr.IsNull("Aer") then
do your thing
end if
which is failing the if statement or the tb.text = dr()... ?
Hope this helps
DK
polynaux
Member
195 Points
775 Posts
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 23, 2008 10:42 AM|LINK
Hi DkUltra,
I will try your code in
a few hours when I am home again.
Just for now. Assuming the header row is not part of dr then
it is impossible that the whole row is empty since I have
a primary key that is declared NOT NULL. - by the way,
the primary key field, which is not null, loads perfeclty fine
in my textbox. its the other stuff that is causing the issue (I think),
columns that are not necessary NOT NULL.
DkUltra
Contributor
2813 Points
546 Posts
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 23, 2008 12:43 PM|LINK
I have had similar issues in the past with table adapters and solved by setting the column default value to a valid value in the table adapter. such as a text field default = " ".
I hope one of these ideas helps
DK
polynaux
Member
195 Points
775 Posts
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 23, 2008 06:52 PM|LINK
I tried your code example from today but it still doesnt work. thats so odd. why is it so difficult to assign a bloody value to a textbox and if its not there just dont assign any.
I cant assign a default value for "" or '' or - since I get the message that says that these are invalid.
thanks anyway
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 23, 2008 07:04 PM|LINK
Why you don't explain the problem in details ?
Are you using typed datasets with Table adapters ?
polynaux
Member
195 Points
775 Posts
Re: Retrieving row with some empty colmns: Specified cast is not valid.
Oct 23, 2008 07:26 PM|LINK
Hi anas,
thats right, I am using a data table with a table adapter, thats how the declaration looks:
Dim dt As AerDataTable Dim dr As Aer_InpusIsNumberRow dt = tPAttribute.GetAllAer(TextBoxAerNumber.Text) dr = DirectCast(dt.Rows(0), Aer_InpusIsNumberRow) If Not dr("Aer") Is Nothing AndAlso Not String.IsNullOrEmpty(dr("Aer").ToString()) Then LabelAer.Text = dr("Aer").ToString() End If If Not dr("Aer2") Is Nothing AndAlso Not String.IsNullOrEmpty(dr("Aer2").ToString()) Then LabelAer2.Text = dr("Aer2").ToString() End If ...My query, GetAllAe, works perfectly. Some columns are empty, some are not. So when I want to get Aer2, which is empty, I get the error.
I hope that makes sense,
polynaux