By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

Specified cast is not valid Problem

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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****@inbizservices.com> wrote in message
news:#E**************@TK2MSFTNGP10.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

P: n/a
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******@takempis.com> wrote in message
news:O5**************@TK2MSFTNGP12.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****@inbizservices.com> wrote in message
news:#E**************@TK2MSFTNGP10.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

P: n/a
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****@inbizservices.com> wrote in message
news:OF**************@TK2MSFTNGP12.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******@takempis.com> wrote in message
news:O5**************@TK2MSFTNGP12.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****@inbizservices.com> wrote in message
news:#E**************@TK2MSFTNGP10.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

P: n/a
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 AddFieldCriteria(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.Table()
Dim myRow As New TableRow()
Dim myCell As New TableCell()

Select Case ValueType
Case Is = "date"
objCalendar = New Calendar()
objCalendar.ID = "dtecalendar"
objCalendar.ShowTitle = True
objCalendar.DayNameFormat = 3 'First Two Letters
objCalendar.SelectionMode = 1 'Day
objCalendar.BackColor = Color.White
objCalendar.FirstDayOfWeek = 1 'Monday
objCalendar.BorderColor = Color.Black
objCalendar.ForeColor = Color.Black
objCalendar.Height = Unit.Pixel(60)
objCalendar.Width = Unit.Pixel(120)
objCalendar.Font.Name = "Arial, Helvetica, sans-serif"
objCalendar.Font.Size = FontUnit.Point(7)
objCalendar.NextPrevStyle.ForeColor = Color.White
objCalendar.NextPrevStyle.BackColor = Color.Navy

myCell = New TableCell()
myCell.Controls.Add(objCalendar)
myRow.Cells.Add(myCell)
Case Is = "text"
objTextBox = New TextBox()
objTextBox.ID = "txttextbox"
objTextBox.CssClass = "bodytext"
objTextBox.Width = 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.CssClass = "bodytext"
objListBox.Width = Unit.Pixel(180)
objListBox.Height = Unit.Pixel(60)

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

myTable.Rows.Add(myRow)

myTable.CellPadding = 2
myTable.CellSpacing = 2
panelCriteria.Controls.Clear()
panelCriteria.Controls.Add(myTable)

'errors when adding table to panel

End Sub
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:Oo*************@tk2msftngp13.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****@inbizservices.com> wrote in message
news:OF**************@TK2MSFTNGP12.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******@takempis.com> wrote in message
news:O5**************@TK2MSFTNGP12.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****@inbizservices.com> wrote in message
news:#E**************@TK2MSFTNGP10.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

P: n/a
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****@inbizservices.com> wrote in message
news:#s**************@TK2MSFTNGP09.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 AddFieldCriteria(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.Table()
Dim myRow As New TableRow()
Dim myCell As New TableCell()

Select Case ValueType
Case Is = "date"
objCalendar = New Calendar()
objCalendar.ID = "dtecalendar"
objCalendar.ShowTitle = True
objCalendar.DayNameFormat = 3 'First Two Letters
objCalendar.SelectionMode = 1 'Day
objCalendar.BackColor = Color.White
objCalendar.FirstDayOfWeek = 1 'Monday
objCalendar.BorderColor = Color.Black
objCalendar.ForeColor = Color.Black
objCalendar.Height = Unit.Pixel(60)
objCalendar.Width = Unit.Pixel(120)
objCalendar.Font.Name = "Arial, Helvetica, sans-serif"
objCalendar.Font.Size = FontUnit.Point(7)
objCalendar.NextPrevStyle.ForeColor = Color.White
objCalendar.NextPrevStyle.BackColor = Color.Navy

myCell = New TableCell()
myCell.Controls.Add(objCalendar)
myRow.Cells.Add(myCell)
Case Is = "text"
objTextBox = New TextBox()
objTextBox.ID = "txttextbox"
objTextBox.CssClass = "bodytext"
objTextBox.Width = 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.CssClass = "bodytext"
objListBox.Width = Unit.Pixel(180)
objListBox.Height = Unit.Pixel(60)

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

myTable.Rows.Add(myRow)

myTable.CellPadding = 2
myTable.CellSpacing = 2
panelCriteria.Controls.Clear()
panelCriteria.Controls.Add(myTable)

'errors when adding table to panel

End Sub
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:Oo*************@tk2msftngp13.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****@inbizservices.com> wrote in message
news:OF**************@TK2MSFTNGP12.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******@takempis.com> wrote in message
news:O5**************@TK2MSFTNGP12.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****@inbizservices.com> wrote in message
> news:#E**************@TK2MSFTNGP10.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

P: n/a
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******@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.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****@inbizservices.com> wrote in message
news:#s**************@TK2MSFTNGP09.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 AddFieldCriteria(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.Table()
Dim myRow As New TableRow()
Dim myCell As New TableCell()

Select Case ValueType
Case Is = "date"
objCalendar = New Calendar()
objCalendar.ID = "dtecalendar"
objCalendar.ShowTitle = True
objCalendar.DayNameFormat = 3 'First Two Letters
objCalendar.SelectionMode = 1 'Day
objCalendar.BackColor = Color.White
objCalendar.FirstDayOfWeek = 1 'Monday
objCalendar.BorderColor = Color.Black
objCalendar.ForeColor = Color.Black
objCalendar.Height = Unit.Pixel(60)
objCalendar.Width = Unit.Pixel(120)
objCalendar.Font.Name = "Arial, Helvetica, sans-serif"
objCalendar.Font.Size = FontUnit.Point(7)
objCalendar.NextPrevStyle.ForeColor = Color.White
objCalendar.NextPrevStyle.BackColor = Color.Navy

myCell = New TableCell()
myCell.Controls.Add(objCalendar)
myRow.Cells.Add(myCell)
Case Is = "text"
objTextBox = New TextBox()
objTextBox.ID = "txttextbox"
objTextBox.CssClass = "bodytext"
objTextBox.Width = 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.CssClass = "bodytext"
objListBox.Width = Unit.Pixel(180)
objListBox.Height = Unit.Pixel(60)

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

myTable.Rows.Add(myRow)

myTable.CellPadding = 2
myTable.CellSpacing = 2
panelCriteria.Controls.Clear()
panelCriteria.Controls.Add(myTable)

'errors when adding table to panel

End Sub
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:Oo*************@tk2msftngp13.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****@inbizservices.com> wrote in message
news:OF**************@TK2MSFTNGP12.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******@takempis.com> wrote in message
> news:O5**************@TK2MSFTNGP12.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****@inbizservices.com> wrote in message
> > news:#E**************@TK2MSFTNGP10.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

P: n/a
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****@inbizservices.com> wrote in message
news:uM**************@TK2MSFTNGP09.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******@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.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****@inbizservices.com> wrote in message
news:#s**************@TK2MSFTNGP09.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 AddFieldCriteria(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.Table()
Dim myRow As New TableRow()
Dim myCell As New TableCell()

Select Case ValueType
Case Is = "date"
objCalendar = New Calendar()
objCalendar.ID = "dtecalendar"
objCalendar.ShowTitle = True
objCalendar.DayNameFormat = 3 'First Two Letters
objCalendar.SelectionMode = 1 'Day
objCalendar.BackColor = Color.White
objCalendar.FirstDayOfWeek = 1 'Monday
objCalendar.BorderColor = Color.Black
objCalendar.ForeColor = Color.Black
objCalendar.Height = Unit.Pixel(60)
objCalendar.Width = Unit.Pixel(120)
objCalendar.Font.Name = "Arial, Helvetica, sans-serif"
objCalendar.Font.Size = FontUnit.Point(7)
objCalendar.NextPrevStyle.ForeColor = Color.White
objCalendar.NextPrevStyle.BackColor = Color.Navy

myCell = New TableCell()
myCell.Controls.Add(objCalendar)
myRow.Cells.Add(myCell)
Case Is = "text"
objTextBox = New TextBox()
objTextBox.ID = "txttextbox"
objTextBox.CssClass = "bodytext"
objTextBox.Width = 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.CssClass = "bodytext"
objListBox.Width = Unit.Pixel(180)
objListBox.Height = Unit.Pixel(60)

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

myTable.Rows.Add(myRow)

myTable.CellPadding = 2
myTable.CellSpacing = 2
panelCriteria.Controls.Clear()
panelCriteria.Controls.Add(myTable)

'errors when adding table to panel

End Sub
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:Oo*************@tk2msftngp13.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****@inbizservices.com> wrote in message
> news:OF**************@TK2MSFTNGP12.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******@takempis.com> wrote in message
> > news:O5**************@TK2MSFTNGP12.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****@inbizservices.com> wrote in message
> > > news:#E**************@TK2MSFTNGP10.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

P: n/a
You wrote: Are you SURE that that is the line which throws the exception?

Referring to this line: panelCriteria.Controls.Add(myTable)

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******@takempis.com> wrote in message
news:%2***************@TK2MSFTNGP10.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****@inbizservices.com> wrote in message
news:uM**************@TK2MSFTNGP09.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******@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.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****@inbizservices.com> wrote in message
news:#s**************@TK2MSFTNGP09.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 AddFieldCriteria(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.Table()
> Dim myRow As New TableRow()
> Dim myCell As New TableCell()
>
> Select Case ValueType
> Case Is = "date"
> objCalendar = New Calendar()
> objCalendar.ID = "dtecalendar"
> objCalendar.ShowTitle = True
> objCalendar.DayNameFormat = 3 'First Two Letters
> objCalendar.SelectionMode = 1 'Day
> objCalendar.BackColor = Color.White
> objCalendar.FirstDayOfWeek = 1 'Monday
> objCalendar.BorderColor = Color.Black
> objCalendar.ForeColor = Color.Black
> objCalendar.Height = Unit.Pixel(60)
> objCalendar.Width = Unit.Pixel(120)
> objCalendar.Font.Name = "Arial, Helvetica, sans-serif"
> objCalendar.Font.Size = FontUnit.Point(7)
> objCalendar.NextPrevStyle.ForeColor = Color.White
> objCalendar.NextPrevStyle.BackColor = Color.Navy
>
> myCell = New TableCell()
> myCell.Controls.Add(objCalendar)
> myRow.Cells.Add(myCell)
> Case Is = "text"
> objTextBox = New TextBox()
> objTextBox.ID = "txttextbox"
> objTextBox.CssClass = "bodytext"
> objTextBox.Width = 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.CssClass = "bodytext"
> objListBox.Width = Unit.Pixel(180)
> objListBox.Height = Unit.Pixel(60)
>
> myCell = New TableCell()
> myCell.Controls.Add(objListBox)
> myRow.Cells.Add(myCell)
> End Select
>
> myTable.Rows.Add(myRow)
>
> myTable.CellPadding = 2
> myTable.CellSpacing = 2
> panelCriteria.Controls.Clear()
> panelCriteria.Controls.Add(myTable)
>
> 'errors when adding table to panel
>
> End Sub
>
>

Nov 18 '05 #9

P: n/a
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****@inbizservices.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl... You wrote: Are you SURE that that is the line which throws the exception?

Referring to this line: panelCriteria.Controls.Add(myTable)

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******@takempis.com> wrote in message
news:%2***************@TK2MSFTNGP10.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****@inbizservices.com> wrote in message
news:uM**************@TK2MSFTNGP09.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******@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.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****@inbizservices.com> wrote in message
> news:#s**************@TK2MSFTNGP09.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 AddFieldCriteria(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.Table()
> > Dim myRow As New TableRow()
> > Dim myCell As New TableCell()
> >
> > Select Case ValueType
> > Case Is = "date"
> > objCalendar = New Calendar()
> > objCalendar.ID = "dtecalendar"
> > objCalendar.ShowTitle = True
> > objCalendar.DayNameFormat = 3 'First Two Letters
> > objCalendar.SelectionMode = 1 'Day
> > objCalendar.BackColor = Color.White
> > objCalendar.FirstDayOfWeek = 1 'Monday
> > objCalendar.BorderColor = Color.Black
> > objCalendar.ForeColor = Color.Black
> > objCalendar.Height = Unit.Pixel(60)
> > objCalendar.Width = Unit.Pixel(120)
> > objCalendar.Font.Name = "Arial, Helvetica, sans-serif"
> > objCalendar.Font.Size = FontUnit.Point(7)
> > objCalendar.NextPrevStyle.ForeColor = Color.White
> > objCalendar.NextPrevStyle.BackColor = Color.Navy
> >
> > myCell = New TableCell()
> > myCell.Controls.Add(objCalendar)
> > myRow.Cells.Add(myCell)
> > Case Is = "text"
> > objTextBox = New TextBox()
> > objTextBox.ID = "txttextbox"
> > objTextBox.CssClass = "bodytext"
> > objTextBox.Width = 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.CssClass = "bodytext"
> > objListBox.Width = Unit.Pixel(180)
> > objListBox.Height = Unit.Pixel(60)
> >
> > myCell = New TableCell()
> > myCell.Controls.Add(objListBox)
> > myRow.Cells.Add(myCell)
> > End Select
> >
> > myTable.Rows.Add(myRow)
> >
> > myTable.CellPadding = 2
> > myTable.CellSpacing = 2
> > panelCriteria.Controls.Clear()
> > panelCriteria.Controls.Add(myTable)
> >
> > 'errors when adding table to panel
> >
> > End Sub
> >
> >


Nov 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.