Specified cast is not valid Problem

M

Matt Tapia

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?
 
K

Kevin Spencer

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.
 
M

Matt Tapia

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.
 
K

Kevin Spencer

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.
 
M

Matt Tapia

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
 
K

Kevin Spencer

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.
 
M

Matt Tapia

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.
 
K

Kevin Spencer

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.
 
M

Matt Tapia

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?
 
K

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top