使用更新面板无效的强制转换异常

时间:2012-12-14 22:52:00

标签: asp.net vb.net

按钮点击

Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
        MsgBox("INSIDE")
        If SocialAuthUser.IsLoggedIn Then
            Dim accountId As Integer = BLL.getAccIDFromSocialAuthSession
            Dim AlbumID As Integer = BLL.createAndReturnNewAlbumId(txtStoryTitle.Text.Trim, "")
            Dim URL As String = BLL.getAlbumPicUrl(txtStoryTitle.Text.Trim)
            Dim dt As New DataTable
            dt.Columns.Add("PictureID")
            dt.Columns.Add("AccountID")
            dt.Columns.Add("AlbumID")
            dt.Columns.Add("URL")
            dt.Columns.Add("Thumbnail")
            dt.Columns.Add("Description")
            dt.Columns.Add("AlbumCover")
            dt.Columns.Add("Tags")
            dt.Columns.Add("Votes")
            dt.Columns.Add("Abused")
            dt.Columns.Add("isActive")
            Dim Row As DataRow

            Dim uniqueFileName As String = ""
            If Session("ID") Is Nothing Then
                lblMessage.Text = "You don't seem to have uploaded any pictures."
                Exit Sub
            Else


**Dim FileCount As Integer = Request.Form(Request.Form.Count - 2)**
                Dim FileName, TargetName As String
                Try
                    Dim Path As String = Server.MapPath(BLL.getAlbumPicUrl(txtStoryTitle.Text.Trim))
                    If Not IO.Directory.Exists(Path) Then
                        IO.Directory.CreateDirectory(Path)
                    End If
                    Dim StartIndex As Integer
                    Dim PicCount As Integer
                    For i As Integer = 0 To Request.Form.Count - 1
                        If Request.Form(i).ToLower.Contains("jpg") Or Request.Form(i).ToLower.Contains("gif") Or Request.Form(i).ToLower.Contains("png") Then
                            StartIndex = i + 1
                            Exit For
                        End If
                    Next
                    For i As Integer = StartIndex To Request.Form.Count - 4 Step 3
                        FileName = Request.Form(i)
                        '## If part here is not kaam ka..but still using it for worst case scenario
                        If IO.File.Exists(Path & FileName) Then
                            TargetName = Path & FileName
                            'MsgBox(TargetName & "--- 1")
                            Dim j As Integer = 1
                            While IO.File.Exists(TargetName)
                                TargetName = Path & IO.Path.GetFileNameWithoutExtension(FileName) & "(" & j & ")" & IO.Path.GetExtension(FileName)
                                j += 1
                            End While
                        Else
                            uniqueFileName = Guid.NewGuid.ToString & "__" & FileName
                            TargetName = Path & uniqueFileName

                        End If
                        IO.File.Move(Server.MapPath("~/TempUploads/" & Session("ID") & "/" & FileName), TargetName)
                        PicCount += 1
                        Row = dt.NewRow()
                        Row(1) = accountId
                        Row(2) = AlbumID
                        Row(3) = URL & uniqueFileName
                        Row(4) = ""
                        Row(5) = "No Desc"
                        Row(6) = "False"
                        Row(7) = ""
                        Row(8) = "0"
                        Row(9) = "0"
                        Row(10) = "True"
                        dt.Rows.Add(Row)
                    Next
                    If BLL.insertImagesIntoAlbum(dt) Then

                        lblMessage.Text = PicCount & IIf(PicCount = 1, " Picture", " Pictures") & " Saved!"
                        lblMessage.ForeColor = Drawing.Color.Black
                        Dim db As SqlDatabase = Connection.connection
                        Using cmd As DbCommand = db.GetSqlStringCommand("SELECT PictureID,URL FROM AlbumPictures WHERE AlbumID=@AlbumID AND AccountID=@AccountID")
                            db.AddInParameter(cmd, "AlbumID", Data.DbType.Int32, AlbumID)
                            db.AddInParameter(cmd, "AccountID", Data.DbType.Int32, accountId)
                            Using ds As DataSet = db.ExecuteDataSet(cmd)
                                If ds.Tables(0).Rows.Count > 0 Then
                                    ListView1.DataSource = ds.Tables(0)
                                    ListView1.DataBind()
                                Else
                                    lblMessage.Text = "No Such Album Exists."
                                End If

                            End Using
                        End Using
                        'WebNavigator.GoToResponseRedirect(WebNavigator.URLFor.ReturnUrl("~/Memories/SortImages.aspx?id=" & AlbumID))
                    Else
                        'TODO:we'll show some error msg
                    End If

                Catch ex As Exception
                    MsgBox(ex.Message)
                    lblMessage.Text = "Oo Poop!!"
                End Try
            End If
        Else
            WebNavigator.GoToResponseRedirect(WebNavigator.URLFor.LoginWithReturnUrl("~/Memories/CreateAlbum.aspx"))
            Exit Sub
        End If

    End Sub

上面的代码工作正常。我在页面中添加了一个更新面板以避免回发,但是当我添加按钮时单击作为触发器

<Triggers>
   <asp:AsyncPostBackTrigger ControlID="btnSubmit" />
</Triggers>

在更新面板中以避免回发,我收到以下错误。当我将按钮单击作为触发器添加到更新面板时,会发生这种情况。

enter image description here

2 个答案:

答案 0 :(得分:1)

Request.Form返回NameValueCollection,可以通过密钥或int索引器的名称访问该String。它始终返回Integer而不是Dim FileCount As String = Request.Form(Request.Form.Count - 2)

{{1}}

答案 1 :(得分:0)

这是来自异常消息的所有直觉,但是在行

FileCount As Integer = Request.Form(Request.Form.Count - 2)

看起来Request.Form(Request.Form.Count - 2)是一个字符串,您正在尝试将其分配给整数类型。

我不知道你要做什么,但字符串看起来像包含“true”你想要以下内容吗?

FileCount As Integer += Boolean.Parse(Request.Form(Request.Form.Count - 2)) ? 1 : 0;