473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2128
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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:...
6
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...
1
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...
3
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...
2
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: ...
3
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
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...
0
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....
0
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.