Specified cast is not valid.

0
August 17, 2011

I have a form, with a user control in it.  I get an error in the task list that says "Specified cast is not valid", and it points to the beginning of the form, so I'm guessing it's a problem with the user control.  Am I wrong in assuming this?  I don't see any explicit or implicit casts in the code.  It lets me build the solution and run it fine, but the error in the task list just won't go away.  Any ideas?

Here's the code for the UserControl :

Public Class ctlResSegments

    Inherits System.Windows.Forms.UserC

ontrol

    Dim dsSegments As DataSet

    Private lngReservationID As Long

    Public Sub Init()

        Dim tableStyle As New DataGridTableStyle

        With tableStyle

            .MappingName = "tblReservationElements"

            .ReadOnly = True

            .AllowSorting = False

        End With

        Dim txtCol As New DataGridNoActiveCellColumn

        ' Reservation ID

        With txtCol

            .Alignment = HorizontalAlignment.Left

            .MappingName = "ReservationElementID"

            .HeaderText = ""

            .Width = 1

            .ReadOnly = True

            .NullText = ""

        End With

        tableStyle.GridColumnStyle

s.Add(txtC

ol)

        txtCol = New DataGridNoActiveCellColumn

        With txtCol

            .Alignment = HorizontalAlignment.Left

            .MappingName = "AccountNumber"

            .HeaderText = "Acct #"

            .Width = 80

            .ReadOnly = True

            .NullText = ""

        End With

        tableStyle.GridColumnStyle

s.Add(txtC

ol)

        txtCol = New DataGridNoActiveCellColumn

        With txtCol

            .Alignment = HorizontalAlignment.Left

            .MappingName = "ResStartDate"

            .HeaderText = "Start Date"

            .Width = 80

            .ReadOnly = True

            .NullText = ""

        End With

        tableStyle.GridColumnStyle

s.Add(txtC

ol)

        txtCol = New DataGridNoActiveCellColumn

        With txtCol

            .Alignment = HorizontalAlignment.Left

            .MappingName = "ResEndDate"

            .HeaderText = "End Date"

            .Width = 80

            .ReadOnly = True

            .NullText = ""

        End With

        tableStyle.GridColumnStyle

s.Add(txtC

ol)

        txtCol = New DataGridNoActiveCellColumn

        With txtCol

            .Alignment = HorizontalAlignment.Left

            .MappingName = "ResTypeCode"

            .HeaderText = "Res Type"

            .Width = 80

            .ReadOnly = True

            .NullText = ""

        End With

        tableStyle.GridColumnStyle

s.Add(txtC

ol)

        txtCol = New DataGridNoActiveCellColumn

        With txtCol

            .Alignment = HorizontalAlignment.Left

            .MappingName = "rtCode"

            .HeaderText = "Room Type"

            .Width = 80

            .ReadOnly = True

            .NullText = ""

        End With

        tableStyle.GridColumnStyle

s.Add(txtC

ol)

        txtCol = New DataGridNoActiveCellColumn

        With txtCol

            .Alignment = HorizontalAlignment.Left

            .MappingName = "DoorNbr"

            .HeaderText = "Room #"

            .Width = 80

            .ReadOnly = True

            .NullText = ""

        End With

        tableStyle.GridColumnStyle

s.Add(txtC

ol)

        With dgResSegments

            .TableStyles.Add(tableStyl

e)

            .CaptionFont = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)

            .Font = New Font("Microsoft Sans Serif", 8, FontStyle.Regular)

        End With

    End Sub

    Private Sub ctlResSegments_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim frmTemp As New frmResMaint

        frmTemp = ParentForm

        lngReservationID = frmTemp.lngResID

        Init()

        Clear()

        RefreshData()

    End Sub

    Private Sub RefreshData()

        Dim strErrors As String

        With dgResSegments

            dsSegments = SelectListReservationSegme

nts(lngRes

ervationID

, strErrors)

            .SetDataBinding(dsSegments

, "tblReservationElements")

            If dsSegments.Tables(0).Rows.

Count > 0 Then

                .CurrentRowIndex = dsSegments.Tables(0).Rows.

Count - 1

                .Focus()

            End If

        End With

    End Sub

    Public Sub Clear()

        Dim c As Control

        Dim strErrors As String

        dsSegments = SelectListReservationSegme

nts(CLng(-

1), strErrors)

        dgResSegments.SetDataBindi

ng(dsSegme

nts, "tblReservationElements")

        'Bind Data to Controls

        For Each c In Controls

            If TypeOf c Is TextBox Then

                If c.Tag <> "" Then

                    c.DataBindings.Clear()

                    c.Text = ""

                End If

            End If

        Next

    End Sub

End Class

Comments See all(0)

Add comment
0
August 17, 2011

according to a site I found, they said the problem is with this:

Private Sub ctlResSegments_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

try

Private Sub ctlResSegments_Load(ByVal sender As Object, ByVal e As System.EventArgs)

However, according to Microsoft.com, they say that if the lower bound of the returned array is set to any number other than 0, you'll receive this message, so i think the error could also be in your for loop

        For Each c In Controls

            If TypeOf c Is TextBox Then

                If c.Tag <> "" Then

                    c.DataBindings.Clear()

                    c.Text = ""

                End If

            End If

        Next

0
August 17, 2011

remove this:

Private lngReservationID As Long

0
August 17, 2011

Tried to remove it.  Still no luck.

0
August 17, 2011

ok, sorry, i looked at it wrong

I'm kinda guessing here since you didn't give a line number error, can you give us the complete error?

I'm wondering if you one of your dimmings you call

IE: Dim tableStyle as new DataGridTableStyle  - that DataGridTableStyle  

 is not a valid declare.

Ok, I'll wait for a line number :)

0
August 17, 2011

That's what's killing me.  It's line Number 0.

0
August 17, 2011

PS - Why isn't that a valid declare?

0
August 17, 2011

the error is at this line

 frmTemp = ParentForm

remove it

B..G

0
August 17, 2011

if mmarinov's suggestion doesn't work, i think seeing the whole err might help...

I hope it works though :)

0
August 17, 2011

Did you get this problem solved?  

Did you try putting a break point in the  ctlResSegments_Load event?  

Also, you could put a Try...Catch...End Try block to trap any errors that are happening in this UserControl routines.  Otherwise the error will propagate back up to the caller, which happens to be when the form is instantiated, and the error message gets a little confusing.

0
August 17, 2011

That's just it.  It's not a runtime error.  I can build and compile and everything works fine.  It's just something that shows up in the task list.  But it will let me build and run and deploy like nothing is wrong.  I tried breakpoints, etc. but there's really nothing to catch since it's not erroring out.  It all works perfectly.  It's just showing up in the task list.

0
August 17, 2011

when u double click on the task list entry, where does it go?  is anything underlined?

is this a class file, module, or a form?

0
August 17, 2011

Do you have Option Strict On or Off?

0
August 17, 2011

you mean Explicit?  that gives "not declared" messages.....

0
August 17, 2011

Strict On, Explicit Off.  Tried different settings of those as well.  When I double click on the task list entry, it takes me to the first line of the form that contains the control.  (Since it's technically Line 0 in that form, I guess it's trying to go there.)  Nothing underlined.

0
August 17, 2011

Do you have any other controls or components on the form besides your UserControl?

0
August 17, 2011

Yay!  Removing the Handles MyBase.Load did the trick.  Did the article mention why that causes a problem?

Thanks.

0
August 17, 2011

I dunno, I figured by reading this article:

http://p2p.wrox.com/topic. asp?TOPIC_ ID=1361

I'm glad it work!  thanx for the pts.

Bear

Related Questions

Microsoft, Visual Studio, 2005, C#, System.InvalidCastException: Specified cast is not valid.

I am returning values from a database using a TableAdapter and a DataView. &#160;However, when I run the program I get an error message saying that &quot;Specified cast is not valid.&quot; *********** Read More

Views

24

Votes

0

Answers

11

August 15, 2011

System.InvalidCastException: Specified cast is not valid, Microsoft Visual Studio, C#

I am writing a program which runs a stored procedure to return the data, then output the content to a CSV file. &#160; When running my stored procedure, I get all the values back. &#160;However, when Read More

Views

33

Votes

0

Answers

7

August 16, 2011

Datagrid on the fly - "Specified cast is not valid."

With Visual Studio, I am attempting to make a DATAGRID in a Web App which will be populated ON THE FLY. &#160;I have declared a DATASET and a DATAADAPTER and have tested the data connection. &#160;All Read More

Views

23

Votes

0

Answers

3

August 16, 2011

.NET - "Specified cast is not valid" error when instantiating oleDBCommand

Specified cast is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where Read More

Views

11

Votes

0

Answers

4

August 17, 2011

Specified Cast in not valid

Hi, there: &#160; &#160; I have 3 sets of runtime controls. &#160;Depending on the user&#39;s choice from a radio button list, different set of runtime controls will show up. &#160;It works fine unles Read More

Views

19

Votes

0

Answers

8

August 16, 2011

nullable types and datareader, invalid cast exception

System.Nullable&lt;int&gt; test = System.Nullable&lt;int&gt;dataRe ader[1]; &#160; where i use this syntax at run time i get System.InvalidCastExceptio n: Specified cast is not valid. of course i coul Read More

Views

7

Votes

0

Answers

1

August 16, 2011

VB.NET connection to MySQL

Having worked with JET and Access my entire life!, I have finally decided to take a look at SQL - in particular MySQL Sever version 4.1, MySQL Connector Net 1.0 (Net 1.1 , Visual Basic 2003). Many exa Read More

Views

21

Votes

0

Answers

8

August 15, 2011

problem with cast

p.floLatitude = dr.GetFloat(dr.GetOrdinal( &quot;Latitude&quot; )); {&quot;Specified cast is not valid.&quot;} Read More

Views

13

Votes

0

Answers

20

August 16, 2011

Specified cast is not valid.

How can I solve the following problem? Line 43: &#160; &#160; &#160; &#160; MembershipUser User = Membership.GetUser(UserNam eTextBox.T ext); the complete code is: &#160; TextBox UserNameTextBox = (Te Read More

Views

18

Votes

0

Answers

4

August 16, 2011

get the value of the first cell in the datagrid

Hi, How can we get the value of the first cell in the datagrid? the datagrid is bound to a table and using edit bound in asp.net vb. Thanks. Sub manage_update( s As Object, e As DataGridCommandEventAr Read More

Views

9

Votes

0

Answers

10

August 17, 2011

how to bind a DataItem to an Enum value?

Hi experts, i want to change images depending on an Enum value. My display method is to bind a dataTable to a repeater. i&#39;ve added an Enum value to the datatable in order to change the image for t Read More

Views

5

Votes

0

Answers

1

August 17, 2011

c# +returning hashtable.into another function.

i have this class library im building for an application. every thing works good, except that i want to add a new function that would return an hashtable &quot;database value and their field&quot; to Read More

Views

25

Votes

0

Answers

4

August 17, 2011

Help Needed translating some C# code into VB.NET

Hi All, In our quest to redo some software of ours, I have hit a stumbling block in porting some C# code into VB.NET Can anyone tell me it&#39;s equivalent in VB.NET? ----- CODE ----- m_cT = cT; &#160 Read More

Views

6

Votes

0

Answers

1

August 17, 2011

Module installation problem

I am trying to install a module into the DotNetNuke Portal v.1.10. &#160;I&#39;m doing the manual installation. Since I&#39;m not able to set up a private assembly. &#160;So when I try to access the m Read More

Views

7

Votes

0

Answers

6

August 17, 2011

C#, DCOM, type library

HI all! I have problem working with DCOM (writing in VC++ 6.0)...so: I create xyzInterop.dll wrapper calling &quot;TlbImp.exe mydcom.tlb&quot; Then I remove DCOM (unregister) component from local (my) Read More

Views

5

Votes

0

Answers

7

August 17, 2011

how to dynamically set height and width in label

Hi, I&#39;m trying to dynamically set a controls (label) width and height. &lt;asp:Label ID=&quot;Label1&quot; Width=&#39;&lt;% #Eval(&quot;bredd&quot;)%&gt;&#39; Height=&#39;&lt;% #Eval(&quot;h&#246; Read More

Views

7

Votes

0

Answers

9

August 16, 2011

DateTime ToString()

I need to read DateTime from MS SQL Server to ASP.NET application through C#. &#160;There are three DateTime fields I need converted ToString() to read in data table in application. &#160;I have attac Read More

Views

14

Votes

0

Answers

39

August 15, 2011

How to display selected columns from datatable using LINQ

I am passing datatable instance to the dataadapter to fetch the resultset from database.the datatable default it doesn&#39;t any schema. Datatable dt = new DataTable(); datadapter da = new dataadapter Read More

Views

10

Votes

0

Answers

3

August 16, 2011

Date problem

I get the following error because my ordr[&quot;DATE_OUT&quot;] is blank/NULL in the database (ORACLE): &quot;Specified cast is not valid.&quot; How can I fix this? Thx. if (ordr.Read()) Read More

Views

0

Votes

0

Answers

10

August 16, 2011

oledbdatareader.GetDecimal() c# VS 2005

how do a get oracle &quot;Number&quot; datatype in c# web app using oleDBDataReader? I get below exception message Specified cast is not valid. System.Data at System.Data.OleDb.ColumnBi nding.Valu eDe Read More

Views

16

Votes

0

Answers

4

August 16, 2011