473,700 Members | 2,484 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 2150
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
3631
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
1814
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
5835
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
10512
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
3091
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
2181
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
1622
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
1382
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
1719
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
8647
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
9076
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...
1
8974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8926
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
7813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4404
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
4659
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.