473,769 Members | 6,208 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specified cast is not valid Problem

I having a problem that receives the following error:

Specified cast is not valid

And I need some help. Here is what is happening:

I have a form with a drop-down control that contains a list of control names
(TextBox, DropDownList, Calendar). On Post back of this drop-down control, I
clear a panel control (I have on the same page) of all its controls, then
dymamically add the selected control to the panel. For example, if in ddl
control I had TextBox and switched it to Calendar, on postback the panel is
cleared of the TextBox control and the Calendar control is added. So far no
problem.

However, if i select a date on the calendar control, then change the drop
down control to either Text Box or Drop Down list, I get the error above
when adding the control to the panel (after I cleared out the old control).

What's going on?
Nov 18 '05 #1
9 2153
You need to find the line of code which threw the error, and that should
lead you to the cause of the problem. You're attempting to use an object
that is of one type as an object of a different type, or the value of the
object is null (Nothing), and you're attempting to use it as an object. If
you're a VB developer, make sure you turn Option Strict ON to prevent this
sort of issue in the future. .Net is not nearly so forgiving as VB.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:#E******** ******@TK2MSFTN GP10.phx.gbl...
I having a problem that receives the following error:

Specified cast is not valid

And I need some help. Here is what is happening:

I have a form with a drop-down control that contains a list of control names (TextBox, DropDownList, Calendar). On Post back of this drop-down control, I clear a panel control (I have on the same page) of all its controls, then
dymamically add the selected control to the panel. For example, if in ddl
control I had TextBox and switched it to Calendar, on postback the panel is cleared of the TextBox control and the Calendar control is added. So far no problem.

However, if i select a date on the calendar control, then change the drop
down control to either Text Box or Drop Down list, I get the error above
when adding the control to the panel (after I cleared out the old control).
What's going on?

Nov 18 '05 #2
The error happens when I actually add the new control to the panel. I
checked to see if it is null and it's not. If I don't add the control to the
panel, it does not error.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:O5******** ******@TK2MSFTN GP12.phx.gbl...
You need to find the line of code which threw the error, and that should
lead you to the cause of the problem. You're attempting to use an object
that is of one type as an object of a different type, or the value of the
object is null (Nothing), and you're attempting to use it as an object. If
you're a VB developer, make sure you turn Option Strict ON to prevent this
sort of issue in the future. .Net is not nearly so forgiving as VB.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:#E******** ******@TK2MSFTN GP10.phx.gbl...
I having a problem that receives the following error:

Specified cast is not valid

And I need some help. Here is what is happening:

I have a form with a drop-down control that contains a list of control names
(TextBox, DropDownList, Calendar). On Post back of this drop-down control, I
clear a panel control (I have on the same page) of all its controls,

then dymamically add the selected control to the panel. For example, if in ddl control I had TextBox and switched it to Calendar, on postback the panel

is
cleared of the TextBox control and the Calendar control is added. So far

no
problem.

However, if i select a date on the calendar control, then change the drop down control to either Text Box or Drop Down list, I get the error above
when adding the control to the panel (after I cleared out the old

control).

What's going on?


Nov 18 '05 #3
You'll have to post the offending code, just the part that is causing the
problem.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
The error happens when I actually add the new control to the panel. I
checked to see if it is null and it's not. If I don't add the control to the panel, it does not error.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:O5******** ******@TK2MSFTN GP12.phx.gbl...
You need to find the line of code which threw the error, and that should
lead you to the cause of the problem. You're attempting to use an object
that is of one type as an object of a different type, or the value of the
object is null (Nothing), and you're attempting to use it as an object. If you're a VB developer, make sure you turn Option Strict ON to prevent this sort of issue in the future. .Net is not nearly so forgiving as VB.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:#E******** ******@TK2MSFTN GP10.phx.gbl...
I having a problem that receives the following error:

Specified cast is not valid

And I need some help. Here is what is happening:

I have a form with a drop-down control that contains a list of control

names
(TextBox, DropDownList, Calendar). On Post back of this drop-down control,
I
clear a panel control (I have on the same page) of all its controls,

then dymamically add the selected control to the panel. For example, if in ddl control I had TextBox and switched it to Calendar, on postback the panel is
cleared of the TextBox control and the Calendar control is added. So
far
no
problem.

However, if i select a date on the calendar control, then change the

drop down control to either Text Box or Drop Down list, I get the error

above when adding the control to the panel (after I cleared out the old

control).

What's going on?



Nov 18 '05 #4
Ok I will post the code which errors on the last line of the procedure (see
below). I did have a thought. I went on MSDN web site and found an article
that talked about this error. It made me think about the fact that I only
get the error when I select to show a date or calendar, THEN select a date,
and finally try to show another control (textbox, listbox) other than date.
If I don't select the date, I don't get an error. Could it be something with
the ViewState and the fact that the view state is trying to restore the
state of the calendar control which is no longer there? just a thought. I
appreciate your help.

'Value Type comes from a drop down control with list items
'date, text, or listbox
Sub AddFieldCriteri a(ValueType As String)

Dim objTextBox As TextBox
Dim objCalendar As Calendar
Dim objComboBox As DropDownList
Dim objListBox As ListBox
Dim objLabel As Label
Dim objButton As Button

Dim myTable As New WebControls.Tab le()
Dim myRow As New TableRow()
Dim myCell As New TableCell()

Select Case ValueType
Case Is = "date"
objCalendar = New Calendar()
objCalendar.ID = "dtecalenda r"
objCalendar.Sho wTitle = True
objCalendar.Day NameFormat = 3 'First Two Letters
objCalendar.Sel ectionMode = 1 'Day
objCalendar.Bac kColor = Color.White
objCalendar.Fir stDayOfWeek = 1 'Monday
objCalendar.Bor derColor = Color.Black
objCalendar.For eColor = Color.Black
objCalendar.Hei ght = Unit.Pixel(60)
objCalendar.Wid th = Unit.Pixel(120)
objCalendar.Fon t.Name = "Arial, Helvetica, sans-serif"
objCalendar.Fon t.Size = FontUnit.Point( 7)
objCalendar.Nex tPrevStyle.Fore Color = Color.White
objCalendar.Nex tPrevStyle.Back Color = Color.Navy

myCell = New TableCell()
myCell.Controls .Add(objCalenda r)
myRow.Cells.Add (myCell)
Case Is = "text"
objTextBox = New TextBox()
objTextBox.ID = "txttextbox "
objTextBox.CssC lass = "bodytext"
objTextBox.Widt h = Unit.Pixel(100)

myCell = New TableCell()
myCell.Controls .Add(objTextBox )
myRow.Cells.Add (myCell)
Case Is = "listbox"
objListBox = New ListBox()
objListBox.ID = "lst" + DataField
objListBox.CssC lass = "bodytext"
objListBox.Widt h = Unit.Pixel(180)
objListBox.Heig ht = Unit.Pixel(60)

myCell = New TableCell()
myCell.Controls .Add(objListBox )
myRow.Cells.Add (myCell)
End Select

myTable.Rows.Ad d(myRow)

myTable.CellPad ding = 2
myTable.CellSpa cing = 2
panelCriteria.C ontrols.Clear()
panelCriteria.C ontrols.Add(myT able)

'errors when adding table to panel

End Sub
"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:Oo******** *****@tk2msftng p13.phx.gbl...
You'll have to post the offending code, just the part that is causing the
problem.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
The error happens when I actually add the new control to the panel. I
checked to see if it is null and it's not. If I don't add the control to the
panel, it does not error.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:O5******** ******@TK2MSFTN GP12.phx.gbl...
You need to find the line of code which threw the error, and that should lead you to the cause of the problem. You're attempting to use an object that is of one type as an object of a different type, or the value of the object is null (Nothing), and you're attempting to use it as an object. If
you're a VB developer, make sure you turn Option Strict ON to prevent this sort of issue in the future. .Net is not nearly so forgiving as VB.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:#E******** ******@TK2MSFTN GP10.phx.gbl...
> I having a problem that receives the following error:
>
> Specified cast is not valid
>
> And I need some help. Here is what is happening:
>
> I have a form with a drop-down control that contains a list of
control names
> (TextBox, DropDownList, Calendar). On Post back of this drop-down

control,
I
> clear a panel control (I have on the same page) of all its controls,

then
> dymamically add the selected control to the panel. For example, if
in ddl
> control I had TextBox and switched it to Calendar, on postback the

panel is
> cleared of the TextBox control and the Calendar control is added. So far no
> problem.
>
> However, if i select a date on the calendar control, then change the

drop
> down control to either Text Box or Drop Down list, I get the error above > when adding the control to the panel (after I cleared out the old
control).
>
> What's going on?
>
>



Nov 18 '05 #5
Are you SURE that that is the line which throws the exception? I don't see
anything necessarily wrong with it. What exactly is panelCriteria? I assum
it's a panel, but how is it created?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:#s******** ******@TK2MSFTN GP09.phx.gbl...
Ok I will post the code which errors on the last line of the procedure (see below). I did have a thought. I went on MSDN web site and found an article
that talked about this error. It made me think about the fact that I only
get the error when I select to show a date or calendar, THEN select a date, and finally try to show another control (textbox, listbox) other than date. If I don't select the date, I don't get an error. Could it be something with the ViewState and the fact that the view state is trying to restore the
state of the calendar control which is no longer there? just a thought. I
appreciate your help.

'Value Type comes from a drop down control with list items
'date, text, or listbox
Sub AddFieldCriteri a(ValueType As String)

Dim objTextBox As TextBox
Dim objCalendar As Calendar
Dim objComboBox As DropDownList
Dim objListBox As ListBox
Dim objLabel As Label
Dim objButton As Button

Dim myTable As New WebControls.Tab le()
Dim myRow As New TableRow()
Dim myCell As New TableCell()

Select Case ValueType
Case Is = "date"
objCalendar = New Calendar()
objCalendar.ID = "dtecalenda r"
objCalendar.Sho wTitle = True
objCalendar.Day NameFormat = 3 'First Two Letters
objCalendar.Sel ectionMode = 1 'Day
objCalendar.Bac kColor = Color.White
objCalendar.Fir stDayOfWeek = 1 'Monday
objCalendar.Bor derColor = Color.Black
objCalendar.For eColor = Color.Black
objCalendar.Hei ght = Unit.Pixel(60)
objCalendar.Wid th = Unit.Pixel(120)
objCalendar.Fon t.Name = "Arial, Helvetica, sans-serif"
objCalendar.Fon t.Size = FontUnit.Point( 7)
objCalendar.Nex tPrevStyle.Fore Color = Color.White
objCalendar.Nex tPrevStyle.Back Color = Color.Navy

myCell = New TableCell()
myCell.Controls .Add(objCalenda r)
myRow.Cells.Add (myCell)
Case Is = "text"
objTextBox = New TextBox()
objTextBox.ID = "txttextbox "
objTextBox.CssC lass = "bodytext"
objTextBox.Widt h = Unit.Pixel(100)

myCell = New TableCell()
myCell.Controls .Add(objTextBox )
myRow.Cells.Add (myCell)
Case Is = "listbox"
objListBox = New ListBox()
objListBox.ID = "lst" + DataField
objListBox.CssC lass = "bodytext"
objListBox.Widt h = Unit.Pixel(180)
objListBox.Heig ht = Unit.Pixel(60)

myCell = New TableCell()
myCell.Controls .Add(objListBox )
myRow.Cells.Add (myCell)
End Select

myTable.Rows.Ad d(myRow)

myTable.CellPad ding = 2
myTable.CellSpa cing = 2
panelCriteria.C ontrols.Clear()
panelCriteria.C ontrols.Add(myT able)

'errors when adding table to panel

End Sub
"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:Oo******** *****@tk2msftng p13.phx.gbl...
You'll have to post the offending code, just the part that is causing the
problem.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
The error happens when I actually add the new control to the panel. I
checked to see if it is null and it's not. If I don't add the control to
the
panel, it does not error.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:O5******** ******@TK2MSFTN GP12.phx.gbl...
> You need to find the line of code which threw the error, and that should > lead you to the cause of the problem. You're attempting to use an object > that is of one type as an object of a different type, or the value
of the
> object is null (Nothing), and you're attempting to use it as an object.
If
> you're a VB developer, make sure you turn Option Strict ON to

prevent this
> sort of issue in the future. .Net is not nearly so forgiving as VB.
>
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
>
> "Matt Tapia" <mt****@inbizse rvices.com> wrote in message
> news:#E******** ******@TK2MSFTN GP10.phx.gbl...
> > I having a problem that receives the following error:
> >
> > Specified cast is not valid
> >
> > And I need some help. Here is what is happening:
> >
> > I have a form with a drop-down control that contains a list of control > names
> > (TextBox, DropDownList, Calendar). On Post back of this drop-down
control,
> I
> > clear a panel control (I have on the same page) of all its
controls, then
> > dymamically add the selected control to the panel. For example, if

in ddl
> > control I had TextBox and switched it to Calendar, on postback the

panel
> is
> > cleared of the TextBox control and the Calendar control is added. So far
> no
> > problem.
> >
> > However, if i select a date on the calendar control, then change

the drop
> > down control to either Text Box or Drop Down list, I get the error

above
> > when adding the control to the panel (after I cleared out the old
> control).
> >
> > What's going on?
> >
> >
>
>



Nov 18 '05 #6
That is the line that errors out when I view the aspx page (I am using
in-page coding). panelCriteria is a panel that is created at design time,
not run-time.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Are you SURE that that is the line which throws the exception? I don't see
anything necessarily wrong with it. What exactly is panelCriteria? I assum
it's a panel, but how is it created?

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:#s******** ******@TK2MSFTN GP09.phx.gbl...
Ok I will post the code which errors on the last line of the procedure (see
below). I did have a thought. I went on MSDN web site and found an article
that talked about this error. It made me think about the fact that I only get the error when I select to show a date or calendar, THEN select a

date,
and finally try to show another control (textbox, listbox) other than

date.
If I don't select the date, I don't get an error. Could it be something

with
the ViewState and the fact that the view state is trying to restore the
state of the calendar control which is no longer there? just a thought. I appreciate your help.

'Value Type comes from a drop down control with list items
'date, text, or listbox
Sub AddFieldCriteri a(ValueType As String)

Dim objTextBox As TextBox
Dim objCalendar As Calendar
Dim objComboBox As DropDownList
Dim objListBox As ListBox
Dim objLabel As Label
Dim objButton As Button

Dim myTable As New WebControls.Tab le()
Dim myRow As New TableRow()
Dim myCell As New TableCell()

Select Case ValueType
Case Is = "date"
objCalendar = New Calendar()
objCalendar.ID = "dtecalenda r"
objCalendar.Sho wTitle = True
objCalendar.Day NameFormat = 3 'First Two Letters
objCalendar.Sel ectionMode = 1 'Day
objCalendar.Bac kColor = Color.White
objCalendar.Fir stDayOfWeek = 1 'Monday
objCalendar.Bor derColor = Color.Black
objCalendar.For eColor = Color.Black
objCalendar.Hei ght = Unit.Pixel(60)
objCalendar.Wid th = Unit.Pixel(120)
objCalendar.Fon t.Name = "Arial, Helvetica, sans-serif"
objCalendar.Fon t.Size = FontUnit.Point( 7)
objCalendar.Nex tPrevStyle.Fore Color = Color.White
objCalendar.Nex tPrevStyle.Back Color = Color.Navy

myCell = New TableCell()
myCell.Controls .Add(objCalenda r)
myRow.Cells.Add (myCell)
Case Is = "text"
objTextBox = New TextBox()
objTextBox.ID = "txttextbox "
objTextBox.CssC lass = "bodytext"
objTextBox.Widt h = Unit.Pixel(100)

myCell = New TableCell()
myCell.Controls .Add(objTextBox )
myRow.Cells.Add (myCell)
Case Is = "listbox"
objListBox = New ListBox()
objListBox.ID = "lst" + DataField
objListBox.CssC lass = "bodytext"
objListBox.Widt h = Unit.Pixel(180)
objListBox.Heig ht = Unit.Pixel(60)

myCell = New TableCell()
myCell.Controls .Add(objListBox )
myRow.Cells.Add (myCell)
End Select

myTable.Rows.Ad d(myRow)

myTable.CellPad ding = 2
myTable.CellSpa cing = 2
panelCriteria.C ontrols.Clear()
panelCriteria.C ontrols.Add(myT able)

'errors when adding table to panel

End Sub
"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:Oo******** *****@tk2msftng p13.phx.gbl...
You'll have to post the offending code, just the part that is causing the problem.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
> The error happens when I actually add the new control to the panel. I > checked to see if it is null and it's not. If I don't add the control to
the
> panel, it does not error.
>
> "Kevin Spencer" <ks******@takem pis.com> wrote in message
> news:O5******** ******@TK2MSFTN GP12.phx.gbl...
> > You need to find the line of code which threw the error, and that should
> > lead you to the cause of the problem. You're attempting to use an

object
> > that is of one type as an object of a different type, or the value of the
> > object is null (Nothing), and you're attempting to use it as an

object.
If
> > you're a VB developer, make sure you turn Option Strict ON to prevent this
> > sort of issue in the future. .Net is not nearly so forgiving as
VB. > >
> > --
> > HTH,
> > Kevin Spencer
> > .Net Developer
> > Microsoft MVP
> > Big things are made up
> > of lots of little things.
> >
> > "Matt Tapia" <mt****@inbizse rvices.com> wrote in message
> > news:#E******** ******@TK2MSFTN GP10.phx.gbl...
> > > I having a problem that receives the following error:
> > >
> > > Specified cast is not valid
> > >
> > > And I need some help. Here is what is happening:
> > >
> > > I have a form with a drop-down control that contains a list of

control
> > names
> > > (TextBox, DropDownList, Calendar). On Post back of this drop-down > control,
> > I
> > > clear a panel control (I have on the same page) of all its

controls, > then
> > > dymamically add the selected control to the panel. For example, if in
> ddl
> > > control I had TextBox and switched it to Calendar, on postback
the panel
> > is
> > > cleared of the TextBox control and the Calendar control is added. So far
> > no
> > > problem.
> > >
> > > However, if i select a date on the calendar control, then change the > drop
> > > down control to either Text Box or Drop Down list, I get the

error above
> > > when adding the control to the panel (after I cleared out the old > > control).
> > >
> > > What's going on?
> > >
> > >
> >
> >
>
>



Nov 18 '05 #7
I'm sorry, but I don't understand what you mean by "that is the line that
errors out when I view the aspx page."

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:uM******** ******@TK2MSFTN GP09.phx.gbl...
That is the line that errors out when I view the aspx page (I am using
in-page coding). panelCriteria is a panel that is created at design time,
not run-time.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Are you SURE that that is the line which throws the exception? I don't see
anything necessarily wrong with it. What exactly is panelCriteria? I assum it's a panel, but how is it created?

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:#s******** ******@TK2MSFTN GP09.phx.gbl...
Ok I will post the code which errors on the last line of the procedure (see
below). I did have a thought. I went on MSDN web site and found an article that talked about this error. It made me think about the fact that I only get the error when I select to show a date or calendar, THEN select a

date,
and finally try to show another control (textbox, listbox) other than

date.
If I don't select the date, I don't get an error. Could it be something with
the ViewState and the fact that the view state is trying to restore
the state of the calendar control which is no longer there? just a thought. I appreciate your help.

'Value Type comes from a drop down control with list items
'date, text, or listbox
Sub AddFieldCriteri a(ValueType As String)

Dim objTextBox As TextBox
Dim objCalendar As Calendar
Dim objComboBox As DropDownList
Dim objListBox As ListBox
Dim objLabel As Label
Dim objButton As Button

Dim myTable As New WebControls.Tab le()
Dim myRow As New TableRow()
Dim myCell As New TableCell()

Select Case ValueType
Case Is = "date"
objCalendar = New Calendar()
objCalendar.ID = "dtecalenda r"
objCalendar.Sho wTitle = True
objCalendar.Day NameFormat = 3 'First Two Letters
objCalendar.Sel ectionMode = 1 'Day
objCalendar.Bac kColor = Color.White
objCalendar.Fir stDayOfWeek = 1 'Monday
objCalendar.Bor derColor = Color.Black
objCalendar.For eColor = Color.Black
objCalendar.Hei ght = Unit.Pixel(60)
objCalendar.Wid th = Unit.Pixel(120)
objCalendar.Fon t.Name = "Arial, Helvetica, sans-serif"
objCalendar.Fon t.Size = FontUnit.Point( 7)
objCalendar.Nex tPrevStyle.Fore Color = Color.White
objCalendar.Nex tPrevStyle.Back Color = Color.Navy

myCell = New TableCell()
myCell.Controls .Add(objCalenda r)
myRow.Cells.Add (myCell)
Case Is = "text"
objTextBox = New TextBox()
objTextBox.ID = "txttextbox "
objTextBox.CssC lass = "bodytext"
objTextBox.Widt h = Unit.Pixel(100)

myCell = New TableCell()
myCell.Controls .Add(objTextBox )
myRow.Cells.Add (myCell)
Case Is = "listbox"
objListBox = New ListBox()
objListBox.ID = "lst" + DataField
objListBox.CssC lass = "bodytext"
objListBox.Widt h = Unit.Pixel(180)
objListBox.Heig ht = Unit.Pixel(60)

myCell = New TableCell()
myCell.Controls .Add(objListBox )
myRow.Cells.Add (myCell)
End Select

myTable.Rows.Ad d(myRow)

myTable.CellPad ding = 2
myTable.CellSpa cing = 2
panelCriteria.C ontrols.Clear()
panelCriteria.C ontrols.Add(myT able)

'errors when adding table to panel

End Sub
"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:Oo******** *****@tk2msftng p13.phx.gbl...
> You'll have to post the offending code, just the part that is
causing
the
> problem.
>
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
>
> "Matt Tapia" <mt****@inbizse rvices.com> wrote in message
> news:OF******** ******@TK2MSFTN GP12.phx.gbl...
> > The error happens when I actually add the new control to the
panel.
I > > checked to see if it is null and it's not. If I don't add the control
to
> the
> > panel, it does not error.
> >
> > "Kevin Spencer" <ks******@takem pis.com> wrote in message
> > news:O5******** ******@TK2MSFTN GP12.phx.gbl...
> > > You need to find the line of code which threw the error, and
that should
> > > lead you to the cause of the problem. You're attempting to use an object
> > > that is of one type as an object of a different type, or the

value of
> the
> > > object is null (Nothing), and you're attempting to use it as an
object.
> If
> > > you're a VB developer, make sure you turn Option Strict ON to

prevent
> this
> > > sort of issue in the future. .Net is not nearly so forgiving as VB. > > >
> > > --
> > > HTH,
> > > Kevin Spencer
> > > .Net Developer
> > > Microsoft MVP
> > > Big things are made up
> > > of lots of little things.
> > >
> > > "Matt Tapia" <mt****@inbizse rvices.com> wrote in message
> > > news:#E******** ******@TK2MSFTN GP10.phx.gbl...
> > > > I having a problem that receives the following error:
> > > >
> > > > Specified cast is not valid
> > > >
> > > > And I need some help. Here is what is happening:
> > > >
> > > > I have a form with a drop-down control that contains a list of
control
> > > names
> > > > (TextBox, DropDownList, Calendar). On Post back of this drop-down > > control,
> > > I
> > > > clear a panel control (I have on the same page) of all its

controls,
> > then
> > > > dymamically add the selected control to the panel. For
example, if in
> > ddl
> > > > control I had TextBox and switched it to Calendar, on postback the > panel
> > > is
> > > > cleared of the TextBox control and the Calendar control is added.
So
> far
> > > no
> > > > problem.
> > > >
> > > > However, if i select a date on the calendar control, then

change the
> > drop
> > > > down control to either Text Box or Drop Down list, I get the

error > above
> > > > when adding the control to the panel (after I cleared out the old > > > control).
> > > >
> > > > What's going on?
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #8
You wrote: Are you SURE that that is the line which throws the exception?

Referring to this line: panelCriteria.C ontrols.Add(myT able)

I wrote: that is the line that errors out when I view the aspx page

Meaning: All of this code is in an aspx web form page. When the page
postbacks, the above line is the one that throws the exception (errors out -
in my own words).

Does this help?
"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
I'm sorry, but I don't understand what you mean by "that is the line that
errors out when I view the aspx page."

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:uM******** ******@TK2MSFTN GP09.phx.gbl...
That is the line that errors out when I view the aspx page (I am using
in-page coding). panelCriteria is a panel that is created at design time,
not run-time.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Are you SURE that that is the line which throws the exception? I don't

see anything necessarily wrong with it. What exactly is panelCriteria? I assum it's a panel, but how is it created?

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:#s******** ******@TK2MSFTN GP09.phx.gbl...
> Ok I will post the code which errors on the last line of the procedure (see
> below). I did have a thought. I went on MSDN web site and found an

article
> that talked about this error. It made me think about the fact that I

only
> get the error when I select to show a date or calendar, THEN select a date,
> and finally try to show another control (textbox, listbox) other than date.
> If I don't select the date, I don't get an error. Could it be something with
> the ViewState and the fact that the view state is trying to restore the > state of the calendar control which is no longer there? just a

thought.
I
> appreciate your help.
>
> 'Value Type comes from a drop down control with list items
> 'date, text, or listbox
> Sub AddFieldCriteri a(ValueType As String)
>
> Dim objTextBox As TextBox
> Dim objCalendar As Calendar
> Dim objComboBox As DropDownList
> Dim objListBox As ListBox
> Dim objLabel As Label
> Dim objButton As Button
>
> Dim myTable As New WebControls.Tab le()
> Dim myRow As New TableRow()
> Dim myCell As New TableCell()
>
> Select Case ValueType
> Case Is = "date"
> objCalendar = New Calendar()
> objCalendar.ID = "dtecalenda r"
> objCalendar.Sho wTitle = True
> objCalendar.Day NameFormat = 3 'First Two Letters
> objCalendar.Sel ectionMode = 1 'Day
> objCalendar.Bac kColor = Color.White
> objCalendar.Fir stDayOfWeek = 1 'Monday
> objCalendar.Bor derColor = Color.Black
> objCalendar.For eColor = Color.Black
> objCalendar.Hei ght = Unit.Pixel(60)
> objCalendar.Wid th = Unit.Pixel(120)
> objCalendar.Fon t.Name = "Arial, Helvetica, sans-serif"
> objCalendar.Fon t.Size = FontUnit.Point( 7)
> objCalendar.Nex tPrevStyle.Fore Color = Color.White
> objCalendar.Nex tPrevStyle.Back Color = Color.Navy
>
> myCell = New TableCell()
> myCell.Controls .Add(objCalenda r)
> myRow.Cells.Add (myCell)
> Case Is = "text"
> objTextBox = New TextBox()
> objTextBox.ID = "txttextbox "
> objTextBox.CssC lass = "bodytext"
> objTextBox.Widt h = Unit.Pixel(100)
>
> myCell = New TableCell()
> myCell.Controls .Add(objTextBox )
> myRow.Cells.Add (myCell)
> Case Is = "listbox"
> objListBox = New ListBox()
> objListBox.ID = "lst" + DataField
> objListBox.CssC lass = "bodytext"
> objListBox.Widt h = Unit.Pixel(180)
> objListBox.Heig ht = Unit.Pixel(60)
>
> myCell = New TableCell()
> myCell.Controls .Add(objListBox )
> myRow.Cells.Add (myCell)
> End Select
>
> myTable.Rows.Ad d(myRow)
>
> myTable.CellPad ding = 2
> myTable.CellSpa cing = 2
> panelCriteria.C ontrols.Clear()
> panelCriteria.C ontrols.Add(myT able)
>
> 'errors when adding table to panel
>
> End Sub
>
>

Nov 18 '05 #9
Hi Matt,
Does this help?
Sort of. It's hard to tell so far, as the syntax seems fine, and you can
certainly add another Control to any Control's Controls collection. A
possibility is that one of the objects referenced in the line is null (null
cannot be cast to another type), but there are other possibilities as well.

Can you post the enitre text of the error message?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:ea******** ******@TK2MSFTN GP10.phx.gbl... You wrote: Are you SURE that that is the line which throws the exception?

Referring to this line: panelCriteria.C ontrols.Add(myT able)

I wrote: that is the line that errors out when I view the aspx page

Meaning: All of this code is in an aspx web form page. When the page
postbacks, the above line is the one that throws the exception (errors out - in my own words).

Does this help?
"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
I'm sorry, but I don't understand what you mean by "that is the line that
errors out when I view the aspx page."

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Matt Tapia" <mt****@inbizse rvices.com> wrote in message
news:uM******** ******@TK2MSFTN GP09.phx.gbl...
That is the line that errors out when I view the aspx page (I am using
in-page coding). panelCriteria is a panel that is created at design time, not run-time.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
> Are you SURE that that is the line which throws the exception? I don't see
> anything necessarily wrong with it. What exactly is panelCriteria? I assum
> it's a panel, but how is it created?
>
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
>
> "Matt Tapia" <mt****@inbizse rvices.com> wrote in message
> news:#s******** ******@TK2MSFTN GP09.phx.gbl...
> > Ok I will post the code which errors on the last line of the procedure > (see
> > below). I did have a thought. I went on MSDN web site and found an
article
> > that talked about this error. It made me think about the fact that
I only
> > get the error when I select to show a date or calendar, THEN
select a > date,
> > and finally try to show another control (textbox, listbox) other than > date.
> > If I don't select the date, I don't get an error. Could it be

something
> with
> > the ViewState and the fact that the view state is trying to

restore the
> > state of the calendar control which is no longer there? just a

thought.
I
> > appreciate your help.
> >
> > 'Value Type comes from a drop down control with list items
> > 'date, text, or listbox
> > Sub AddFieldCriteri a(ValueType As String)
> >
> > Dim objTextBox As TextBox
> > Dim objCalendar As Calendar
> > Dim objComboBox As DropDownList
> > Dim objListBox As ListBox
> > Dim objLabel As Label
> > Dim objButton As Button
> >
> > Dim myTable As New WebControls.Tab le()
> > Dim myRow As New TableRow()
> > Dim myCell As New TableCell()
> >
> > Select Case ValueType
> > Case Is = "date"
> > objCalendar = New Calendar()
> > objCalendar.ID = "dtecalenda r"
> > objCalendar.Sho wTitle = True
> > objCalendar.Day NameFormat = 3 'First Two Letters
> > objCalendar.Sel ectionMode = 1 'Day
> > objCalendar.Bac kColor = Color.White
> > objCalendar.Fir stDayOfWeek = 1 'Monday
> > objCalendar.Bor derColor = Color.Black
> > objCalendar.For eColor = Color.Black
> > objCalendar.Hei ght = Unit.Pixel(60)
> > objCalendar.Wid th = Unit.Pixel(120)
> > objCalendar.Fon t.Name = "Arial, Helvetica, sans-serif"
> > objCalendar.Fon t.Size = FontUnit.Point( 7)
> > objCalendar.Nex tPrevStyle.Fore Color = Color.White
> > objCalendar.Nex tPrevStyle.Back Color = Color.Navy
> >
> > myCell = New TableCell()
> > myCell.Controls .Add(objCalenda r)
> > myRow.Cells.Add (myCell)
> > Case Is = "text"
> > objTextBox = New TextBox()
> > objTextBox.ID = "txttextbox "
> > objTextBox.CssC lass = "bodytext"
> > objTextBox.Widt h = Unit.Pixel(100)
> >
> > myCell = New TableCell()
> > myCell.Controls .Add(objTextBox )
> > myRow.Cells.Add (myCell)
> > Case Is = "listbox"
> > objListBox = New ListBox()
> > objListBox.ID = "lst" + DataField
> > objListBox.CssC lass = "bodytext"
> > objListBox.Widt h = Unit.Pixel(180)
> > objListBox.Heig ht = Unit.Pixel(60)
> >
> > myCell = New TableCell()
> > myCell.Controls .Add(objListBox )
> > myRow.Cells.Add (myCell)
> > End Select
> >
> > myTable.Rows.Ad d(myRow)
> >
> > myTable.CellPad ding = 2
> > myTable.CellSpa cing = 2
> > panelCriteria.C ontrols.Clear()
> > panelCriteria.C ontrols.Add(myT able)
> >
> > 'errors when adding table to panel
> >
> > End Sub
> >
> >


Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3639
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception: "Specified cast is not valid." The only thing i put there was a "test this." inside the form. What might be the problem here? Thanks in advance. The Exception:
6
1822
by: Raj | last post by:
I have a c# program in which I used cast. That worked perfectly on .Net platform 1.0. When I moved to .Net 2003, I am getting the execption "Specified cast is not valid". I have not made any changes to the code. Is there any difference in project settings or something else between v1.0 and v1.1? Thanks in advance. -- Raj
1
5840
by: Ron Holmes | last post by:
I posted this question on the Crystal Reports Support site and I am still waiting for an answer. Using Crystal Reports 9.0 Developer Full edition: My Crystal report .RPT file has a Picture box which is an OLE Object located in the Page Header section of my .RPT file. This is the call which works perfectly in VB6 to format the crystal reports Page Header section of my report file EquipLst.rpt.
3
10516
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my columns is a template column where I'd like to evaluate the data brought back from the db and populate the column with a "Y" or "N" depending on the value in the db. Here is the code that is causing it as well as the error (below). ERROR...
2
3092
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: try {
3
2185
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application. --------------------------------------------------------------------------------
0
623
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is not valid.","Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> System.InvalidCastException: Specified...
0
1625
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks. ----------------------------------------------------------- Server VS.NET 2003 SQLServer 2000 IIS 66.0
0
1391
by: df | last post by:
I'm seeing a strange problem in the Web App config tool: After creating a role and a user, going back to the Security tab home page, I get a "Specified cast is not valid" error. I've set up my web application to use SQL Server to store the Membership database using aspnet_regsql.exe and have successfully opened the Web Config Tool. Then, I create a role, and go look in the SQL Server aspnet_roles table, and there is the role. All...
1
1730
by: planet | last post by:
Hi, I wish I can solve this problem not having recode the sub. Here is goes: I got this error' specified cast is not valid' at the second line. from the code Sub ChkDelete_OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs) DataGrid1_DeleteCommand(sender, e)
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.