By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,142 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,142 IT Pros & Developers. It's quick & easy.

System.InvalidCastException: Specified cast is not valid.

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a


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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.