Web服务器出错但在本地服务器上运行顺利

时间:2016-10-13 07:53:42

标签: asp.net vb.net

我遇到一些问题,我一直在网络服务器上收到系统错误。但是,当我在本地服务器上运行时没有出现错误。

错误在Try ... Catch异常上。当文件成功插入数据库时​​,它会出现“无法上传文件”的消息。

我不知道为什么会出现这个问题。有人可以向我解释一下吗?。

Try
    'check fileupload
    If uploadPic.HasFile Then
        Dim filecount = 0
        'connection string
        Dim conn As New SqlConnection("server=jkprod01; database=whrepairing; user=sa; password=sa")
        conn.Open()

        'get the multiple file from file upload
        Dim hfc As HttpFileCollection = request.Files
        sqlstring = "select * from dbo.[mst_jigtool] where jigtool_code='" & jigCode.Text & "' and jigtool_type='" & regType.SelectedValue & "'"
        cmd = New SqlCommand(sqlstring, conn)
        dr = cmd.ExecuteReader
        dr.Read()

        If dr.HasRows = False Then
            dr.Close()
            cmd.Dispose()

            For a As Integer = 0 To hfc.Count - 1
                Dim hpf As HttpPostedFile = hfc(a)
                fs = hpf.InputStream
                br = New BinaryReader(fs)
                bytes = br.ReadBytes(Convert.ToInt32(fs.Length))

                sqlstring = "insert into mst_jigtool ([jigtool_code], [jigtool_type], [jigtool_address], [jigtool_image], [upd_by], [created_by], [date_created], [upd_date]) VALUES ('" & jigCode.Text & "', '" & regType.SelectedValue & "', '" & jigAdd.Text & "', @pics, '" & Class1.user_id & "', '" & Class1.user_id & "', getdate(), getdate())"
                cmd = New SqlCommand(sqlstring, conn)
                cmd.Parameters.AddWithValue("@pics", bytes)
                fs = Nothing
                br = Nothing
                bytes = Nothing
                cmd.Dispose()
                filecount += 1
                dr = cmd.ExecuteReader
                dr.Close()
                conn.Close()
                cmd.Dispose()
            Next
            Dim msgRslt As MsgBoxResult = MsgBox("Jig tool succussfully registered. Do you want to continue register jigtool? .", MsgBoxStyle.YesNo)
            If msgRslt = MsgBoxResult.Yes Then
                response.Redirect("regJig.aspx")
            ElseIf msgRslt = MsgBoxResult.No Then
                response.Redirect("home.aspx")
            End If
        Else
            Dim msgRslt As MsgBoxResult = MsgBox("Jig tool already registered !/n Do you want to update ?", MsgBoxStyle.YesNo)
            If msgRslt = MsgBoxResult.Yes Then
                For a As Integer = 0 To hfc.Count - 1
                    Dim hpf As HttpPostedFile = hfc(a)
                    fs = hpf.InputStream
                    br = New BinaryReader(fs)
                    bytes = br.ReadBytes(Convert.ToInt32(fs.Length))
                    Dim conn2 As New SqlConnection("server=jkprod01; database=whrepairing; user=sa; password=sa")
                    conn2.Open()
                    Dim sqlstring2 = "update mst_jigtool set jigtool_type = '" & regType.SelectedValue & "',jigtool_address = '" & jigAdd.Text & "', jigtool_image = @pics,  upd_by='" & Class1.user_id & "', upd_date = getdate() where jigtool_code='" & jigCode.Text & "' and jigtool_type='" & regType.SelectedValue & "' and jigtool_address = '" & jigAdd.Text & "'"
                    Dim cmd2 = New SqlCommand(sqlstring2, conn2)
                    cmd2.Parameters.AddWithValue("@pics", bytes)
                    fs = Nothing
                    br = Nothing
                    bytes = Nothing
                    cmd2.Dispose()
                    filecount += 1
                    dr = cmd.ExecuteReader
                    cmd2.Dispose()
                    conn2.Close()
                Next
                Dim msgRslt1 As MsgBoxResult = MsgBox("Jig tool succussfully updated. Do you want to continue register jigtool? .", MsgBoxStyle.YesNo)
                If msgRslt1 = MsgBoxResult.Yes Then
                    response.Redirect("regJig.aspx")
                ElseIf msgRslt1 = MsgBoxResult.No Then
                    response.Redirect("home.aspx")
                End If
            ElseIf msgRslt = MsgBoxResult.No Then
                response.Redirect("regJig.aspx")
            End If

        End If
        dr.Close()
        cmd.Dispose()
    End If
Catch ex As Exception
    response.Write("<script>alert('Upload Failed!. Try again.')</script>")
End Try

2 个答案:

答案 0 :(得分:0)

使用Redirect方法或Response

的其他重载执行所有重定向

Response.Redirect(url, false)

<强>阐释:

使用Response.Redirect(url)时,第二个参数为true。第二个参数是指定是否结束当前响应(名为endResponse)。当您将其传递为true时,ASP.NET框架将结束响应,并且执行该响应的线程将被中止。

但是当在try块内完成此操作时,它会引发一个异常,然后由catch块捕获,因此异常。

请确认例外是ThreadAbortedException。 这在Response.Write("<script>.....</script>");行中不是例外,而是在try block中使用Response.Redirect(url)

HTH。

答案 1 :(得分:0)

你需要重构那么多才能让它顺利运作。

我建议您使用某种web服务来返回regType.SelectedValue和jigCode的组合是否已经存在。

客户端(使用JavaScript和AJAX),单击按钮时,检查组合是否已存在。如果是,请使用confirm对话框决定是否继续。

如果用户选择继续,请使用AJAX上传并根据成功或失败显示适当的JavaScript对话框。

请记住对SQL查询中的每个值使用SQL参数,否则您的SQL查询将容易受到SQL注入攻击,并且对于某些正常输入字符也会失败,例如但不限于撇号。