473,385 Members | 1,707 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,385 software developers and data experts.

System.InvalidCastException: Specified cast is not valid.

Hello All,
Please help. I get the following error from my webpage:

System.InvalidCastException: Specified cast is not valid.
at Time.MaintAtRiskProjects.UpdateProject(String Project, String
AtRiskCode, String Comment)

Code for UpdateProject(String Project, String AtRiskCode, String Comment)
is:

Private Sub UpdateProject(ByVal Project As String, ByVal AtRiskCode As
String, ByVal Comment As String)

Try

Dim strProject As String = Trim(Project)

Dim strAtRiskCode As String = Trim(AtRiskCode)

Dim strComment As String = Trim(Comment)

Dim r As AtRiskProjectsDS.AtRiskProjectsRow

Dim rows() As AtRiskProjectsDS.AtRiskProjectsRow =
AtRiskProjDS.AtRiskProjects.Select(AtRiskProjDS.At RiskProjects.ProjectColumn.ColumnName
& "= '" & strProject & "' AND " &
AtRiskProjDS.AtRiskProjects.AtRiskCodeColumn.Colum nName & "= '" &
strAtRiskCode & "' ")

If rows.Length <> 0 Then

r = rows(0)

If r.Comment <> strComment Then

r.Comment = strComment

End If

End If

Catch ex As Exception

Trace.Write(ex.ToString)

End Try

End Sub
UpdateProject(ByVal Project As String, ByVal AtRiskCode As String, ByVal
Comment As String) is being called from an update button I have in the page.
The code for Update click is as follows:

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click

Try

Dim strProject As String = Trim(lblProject.Text)

AtRiskProjDS.Clear()

AtRiskProjDS = LoadAtRiskProjDS(strProject)

For Each item As DataGridItem In dgAtRiskInfo.Items

If item.ItemType = ListItemType.Item Or item.ItemType =
ListItemType.AlternatingItem Then

Dim RiskCodeOld As CheckBox = CType(item.FindControl("chkCodeOld"),
CheckBox)

Dim RiskCodeNew As CheckBox = CType(item.FindControl("chkCodeNew"),
CheckBox)

Dim AtRiskCode As TextBox = CType(item.FindControl("txtAtRiskCode"),
TextBox)

Dim Description As Label = CType(item.FindControl("lblDescription"), Label)

Dim Comment As TextBox = CType(item.FindControl("txtComment"), TextBox)

Dim strAtRiskCode As String = Trim(AtRiskCode.Text)

Dim strDescription As String = Trim(Description.Text)

Dim strComment As String = Trim(Comment.Text)

If RiskCodeOld.Checked = False And RiskCodeNew.Checked = True Then

InsertProject(RiskCodeNew.Checked, strProject, strAtRiskCode,
strDescription, strComment)

RiskCodeOld.Checked = RiskCodeNew.Checked

ElseIf RiskCodeOld.Checked = True And RiskCodeNew.Checked = False Then

DeleteProject(strProject, strAtRiskCode)

RiskCodeOld.Checked = RiskCodeNew.Checked

Else

UpdateProject(strProject, strAtRiskCode, strComment)

End If

End If

Next

SubmitChanges(strProject)

Catch ex As Exception

Trace.Write(ex.ToString)

End Try

End Sub

Here is what the code for ASPX page looks like:
<%@ Import namespace="Time" %>
<%@ Import namespace="System.Data" %>
<%@ Register TagPrefix="uc1" TagName="footer" src="../footer.ascx" %>
<%@ Register TagPrefix="uc1" TagName="header" src="../header.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="MaintAtRiskProjects.aspx.vb"
Inherits="Time.MaintAtRiskProjects"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>MaintAtRiskProjects</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<LINK href="../master.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="MaintAtRiskProjectsFrm" method="post" runat="server">
<uc1:header id="PageHeader" runat="server"></uc1:header>
<P><asp:dropdownlist id="ddlProject" style="Z-INDEX: 101; LEFT: 16px;
POSITION: absolute; TOP: 72px"
runat="server" OnSelectedIndexChanged="DisplayAtRiskProjInfo"
Font-Names="Lucida Console, Courier New"
Width="168px" autopostback="True"></asp:dropdownlist></P>
<hr style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 104px">
<asp:Button id="btnReport" style="Z-INDEX: 106; LEFT: 504px; POSITION:
absolute; TOP: 64px"
runat="server" Width="97px" Text="View Report"></asp:Button>
<asp:Button id="btnUpdate" style="Z-INDEX: 105; LEFT: 280px; POSITION:
absolute; TOP: 64px"
runat="server" Width="72px" Height="24px"
Text="Update"></asp:Button><asp:label id="lblProject" style="Z-INDEX: 104;
LEFT: 8px; POSITION: absolute; TOP: 128px"
runat="server" Width="160px" ForeColor="#CC0000"
Height="24px"></asp:label><asp:datagrid id="dgAtRiskInfo" style="Z-INDEX:
103; LEFT: 184px; POSITION: absolute; TOP: 128px"
runat="server" Width="385px" Height="160px" ShowFooter="False"
AutoGenerateColumns="False">
<AlternatingItemStyle BackColor="MistyRose"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Right"></ItemStyle>
<HeaderStyle ForeColor="#CC0000" BackColor="Silver"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtAtRiskCode" Text='<%
#Container.DataItem("AtRiskCode") %>' Runat=server Visible=False>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkCodeOld" Checked='<%
#Container.DataItem("RiskCodeFlag") %>' Runat="server" Visible=False>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Check/Uncheck">
<ItemTemplate>
<asp:CheckBox ID="chkCodeNew" Checked='<%
#Container.DataItem("RiskCodeFlag") %>' Width="15px" Runat=server>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" text='<%
#Container.DataItem("Description") %>' width="250px" runat=server>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Comment">
<ItemTemplate>
<asp:TextBox ID="txtComment" Text='<% #Container.DataItem("Comment")
%>' Width="400px" Runat=server>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></form>
</body>
</HTML>


Please help.. Can't figure out what is going on.

Thanks,

Biva


--------------------------------------------------------------------------------

--------------------------------------------------------------------------------


Nov 19 '05 #1
6 6376
"Biva" <bi***********@redprairie.com> wrote in message
news:41***********************@news.twtelecom.net. ..
Hello All,
Please help. I get the following error from my webpage:

System.InvalidCastException: Specified cast is not valid.
at Time.MaintAtRiskProjects.UpdateProject(String Project, String
AtRiskCode, String Comment)

Code for UpdateProject(String Project, String AtRiskCode, String Comment)
is:
....
Please help.. Can't figure out what is going on.


I can't figure out what's going on either, since you did not tell us which
line was causing the error.

This sort of thing happens frequently when a database column contains NULL.
NULL is not a value, so if you try to treat it as a string, you'll get the
invalid cast exception.

John Saunders
Nov 19 '05 #2


John,
My frustration is that it works on my PC that has Framework 1.1.
But when I move it to production that has framework 1.0, it does not
work.
I have the backward compatibility turned on, so it should work.
Since it is on production, i don't have an easy way to debug.
All I could do was set debug=true on web.config and look at the
trace.axd to get that error.
Is there an easy way for me to find out what line is actually giving me
that error?

thanks...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #3
"sqlgirl" <sq*****@devdex.com> wrote in message
news:ur**************@tk2msftngp13.phx.gbl...


John,
My frustration is that it works on my PC that has Framework 1.1.
But when I move it to production that has framework 1.0, it does not
work.
I have the backward compatibility turned on, so it should work.
Since it is on production, i don't have an easy way to debug.
All I could do was set debug=true on web.config and look at the
trace.axd to get that error.
Is there an easy way for me to find out what line is actually giving me
that error?


I don't know, but this kind of thing is a reason to develop on the platform
you're going to use in production.

John Saunders
Nov 19 '05 #4
Here is my DeleteProject method.
Dim rows() As AtRiskProjectsDS.AtRiskProjectsRow =
AtRiskProjDS.AtRiskProjects.Select(AtRiskProjDS.At RiskProjects.ProjectCo
lumn.ColumnName & "= '" & strProject & "' AND " &
AtRiskProjDS.AtRiskProjects.AtRiskCodeColumn.Colum nName & "= '" &
strAtRiskCode & "' ")

is the statement that is not being liked for some reason.

Sub DeleteProject(ByVal Project As String, ByVal AtRiskCode As String)

Try
Dim strProject As String = Project
Dim strAtRiskCode As String = AtRiskCode

Dim rows() As AtRiskProjectsDS.AtRiskProjectsRow =
AtRiskProjDS.AtRiskProjects.Select(AtRiskProjDS.At RiskProjects.ProjectCo
lumn.ColumnName & "= '" & strProject & "' AND " &
AtRiskProjDS.AtRiskProjects.AtRiskCodeColumn.Colum nName & "= '" &
strAtRiskCode & "' ")
Trace.Write("proj1: " & strProject & ", code1: " &
strAtRiskCode)
Dim r As AtRiskProjectsDS.AtRiskProjectsRow

If rows.Length <> 0 Then
r = rows(0)
r.Delete()
End If
Catch ex As Exception
Trace.Write(ex.ToString)
End Try
End Sub

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #5
"sqlgirl" <sq*****@devdex.com> wrote in message
news:Ou**************@TK2MSFTNGP09.phx.gbl...
Here is my DeleteProject method.
Dim rows() As AtRiskProjectsDS.AtRiskProjectsRow =
AtRiskProjDS.AtRiskProjects.Select(AtRiskProjDS.At RiskProjects.ProjectCo
lumn.ColumnName & "= '" & strProject & "' AND " &
AtRiskProjDS.AtRiskProjects.AtRiskCodeColumn.Colum nName & "= '" &
strAtRiskCode & "' ")


Ok, if there's something on that line that ASP.NET doesn't like, then
simplify it:

Dim projectColumnName as String =
AtRiskProjDS.AtRiskProjects.ProjectColumn.ColumnNa me
Dim atRiskCodeColumnName As String =
AtRiskProjDS.AtRiskProjects.AtRiskCodeColumn.Colum nName
Dim query As String = projectColumnName & "= '" & strProject & "' AND " &
atRiskCodeColumnName & "= '" & strAtRiskCode & "' "
Dim rows() As AtRiskProjectsDS.AtRiskProjectsRow =
AtRiskProjDS.AtRiskProjects.Select(query)

Try that and tell us which line has the problem.

John Saunders
Nov 19 '05 #6

I had defined rows() variable as my dataset.datatablerowtype.
eg.
Dim rows() As AtRiskProjectsDS.AtRiskProjectsRow

Framework 1.0 did not like it.

After I changed the rows() variable to simple datarow type as shown
below, it worked fine.

Dim rows() As datarow

Just so if anyone has this problem in the future.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: karunakar | last post by:
Hi All Here iam getting this Error "Specified cast is not valid." My Project has Onc solution diffrent class libarary In that solution DALfactory solution iam getting this error I was calling...
2
by: Michael Moreno | last post by:
Hello, I am failing to understand why I get an invalid cast exception with this very simple code. I have derived the EventArgs class as follow: public class StringArgs : System.EventArgs {...
1
by: Arjen | last post by:
Hello, Does somebody haves a solution for this? Thanks! Line 74: if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
1
by: Miguel Angel Dinelli | last post by:
I´m trying to execute a project and I get this error System.InvalidCastException: Specified cast is not valid. I found in microsoft some Q in 327287, but I not sure how make to solution it. ...
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
0
by: Miriam | last post by:
Hello, I am trying to edit an xml file. I am using Visual Studio .NET 2003. When I click on the "Data" tab (instead of the XML tab) to enter in data like in excel, the following occurs: If I...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
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...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
4
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.