[Solved] JavaScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

makumbi
 
on Dec 05, 2022 02:46 AM
416 Views

Please help I am unable to view my file when i click on the view file after adding update panel

<div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<div class="container">
  <button class="btn btn-default btn-lg btn-link" style="font-size:36px; width: 74px;">
      <asp:Label runat="server" class="badge badge-notify" ID="Counterd" Height="16px" Width="28px">3</asp:Label>
        <span class="glyphicon glyphicon-comment" style="left: -65px; top: -5px; width: 9px"></span>
        &nbsp;  </button>
        </div>    
        <table cellpadding="2" class="auto-style1">
            <tr>
                <td class="auto-style3">Student Name</td>
                <td class="auto-style3">
                    <asp:Label ID="StudentName" runat="server" Text="Label"></asp:Label>
                </td>
                <td class="auto-style3">Account</td>
                <td class="auto-style3">
                    <asp:Label ID="Account" runat="server" Text="Label"></asp:Label>
                </td>
                <td class="auto-style3">Class</td>
                <td class="auto-style3">
                    <asp:Label ID="Classr" runat="server" Text="Label"></asp:Label>
                </td>
                <td class="auto-style3">
                    <asp:Label ID="Stream" runat="server" Text="Label"></asp:Label>
                </td>
                <td class="auto-style3">Subject</td>
                <td class="auto-style3">
                            <asp:DropDownList ID="SubjectData" runat="server" Height="23px" Width="215px">
                            </asp:DropDownList>
                        </td>
                <td class="auto-style3">
                    <asp:Button ID="Button1" runat="server" Text="SignOut" />
                </td>
            </tr>
            <tr>
                <td class="auto-style10" colspan="10">
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:ScriptManager ID="ScriptManager1" runat="server">
                            </asp:ScriptManager>
                            <asp:GridView ID="StudentGrid" runat="server" AutoGenerateColumns="False" Width="723px" CssClass="auto-style2">
                                <Columns>
                                    <asp:BoundField DataField="Topic" HeaderText="Topic" />
                                    <asp:BoundField DataField="Subject" HeaderText="Subject" />
                                    <asp:BoundField DataField="Dates" HeaderText="Date Uploaded" />
                                    <asp:BoundField DataField="Name" HeaderText="File Name" />
                                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lnkView" runat="server" CommandArgument='<%# Eval("Id") %>' OnClick="View" Text="View"></asp:LinkButton>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                <td colspan="5" class="auto-style2">&nbsp;</td>
            </tr>
        </table>    
    </div>

 

Public Class StudentWork
    Inherits System.Web.UI.Page
    Dim constr As String = ConfigurationManager.ConnectionStrings("SLISConnectionString").ConnectionString
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT subkey, Subject FROM Subjects")
                    cmd.CommandType = CommandType.Text
                    cmd.Connection = con
                    con.Open()
                    SubjectData.DataSource = cmd.ExecuteReader()
                    SubjectData.DataTextField = "Subject"
                    SubjectData.DataValueField = "subkey"
                    SubjectData.DataBind()
                    con.Close()
                End Using
            End Using
            SubjectData.Items.Insert(0, New ListItem("--Select Subject--", "0"))

            Account.Text = Session("account").ToString
            StudentName.Text = Session("Name").ToString
            Classr.Text = Session("Class").ToString
            Stream.Text = Session("Stream").ToString
            BindGrid()
        End If
    End Sub

    Private Sub BindGrid()
        Dim constr As String = ConfigurationManager.ConnectionStrings("SLISConnectionString").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand()
                cmd.Parameters.AddWithValue("@c", Classr.Text)
                cmd.CommandText = "SELECT Id, Name,Topic,Class,Stream,Subject,Dates FROM tblFiles2 where class=@c"
                cmd.Connection = con
                con.Open()
                StudentGrid.DataSource = cmd.ExecuteReader()
                StudentGrid.DataBind()
                con.Close()
            End Using
        End Using
    End Sub

    Protected Sub View(sender As Object, e As EventArgs)
        Dim id As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
        Try
            ProcessRequest2(id, HttpContext.Current)
        Catch ex As SystemException

        End Try
    End Sub
    Public Sub ProcessRequest2(id As Integer, context As HttpContext)
        Dim bytes As Byte()
        Dim fileextension As String
        Dim constr As String = ConfigurationManager.ConnectionStrings("SLISConnectionString").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand()
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                con.Open()
                cmd.Parameters.AddWithValue("@FileID", id)
                cmd.CommandText = "SELECT Name, Data, ContentType FROM tblFiles2 WHERE Id=@FileID"
                Using sdr2 As SqlDataReader = cmd.ExecuteReader()
                    sdr2.Read()
                    bytes = DirectCast(sdr2("Data"), Byte())
                    fileextension = sdr2("Name").ToString()
                End Using
                con.Close()
            End Using
        End Using
        context.Response.Buffer = True
        context.Response.Charset = ""
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
        If fileextension.Substring(fileextension.IndexOf("."c) + 1).ToLower() = "pdf" Then
            context.Response.Buffer = True
            context.Response.Charset = ""
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
            context.Response.ContentType = "application/pdf"
            context.Response.BinaryWrite(bytes)
            context.Response.Flush()
            context.Response.[End]()
        ElseIf fileextension.Substring(fileextension.IndexOf("."c) + 1).ToLower() = "doc" OrElse fileextension.Substring(fileextension.IndexOf("."c) + 1).ToLower() = "docx" Then
            File.WriteAllBytes(Server.MapPath(Convert.ToString("~/Temp/") & fileextension), bytes)
            WriteWordFile(Server.MapPath(Convert.ToString("~/Temp/") & fileextension))
        End If
    End Sub
    Private Sub WriteWordFile(filePath As Object)
        Dim missingType As Object = Type.Missing
        Dim [readOnly] As Object = True
        Dim isVisible As Object = False
        Dim documentFormat As Object = 8
        Dim randomName As String = DateTime.Now.Ticks.ToString()
        Dim htmlFilePath As Object = (Server.MapPath("~/Temp/") & randomName) + ".htm"
        Dim directoryPath As String = (Server.MapPath("~/Temp/") & randomName) + "_files"
        Dim applicationclass As New Application
        applicationclass.Documents.Open(filePath, [readOnly], missingType, missingType, missingType, missingType,
         missingType, missingType, missingType, missingType, isVisible, missingType,
         missingType, missingType, missingType, missingType)
        applicationclass.Visible = False
        Dim document As Document = applicationclass.ActiveDocument
        document.SaveAs(htmlFilePath, documentFormat, missingType, missingType, missingType, missingType,
         missingType, missingType, missingType, missingType, missingType, missingType,
         missingType, missingType, missingType, missingType)
        document.Close(missingType, missingType, missingType)
        Dim bytes As Byte()
        Using fs As New FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read)
            Dim reader As New BinaryReader(fs)
            bytes = reader.ReadBytes(CInt(fs.Length))
            fs.Close()
        End Using
        Response.BinaryWrite(bytes)
        Response.Flush()
        File.Delete(htmlFilePath.ToString())
        For Each file_1 As String In Directory.GetFiles(directoryPath)
            File.Delete(file_1)
        Next
        Directory.Delete(directoryPath)
        If File.Exists(filePath.ToString()) Then
            File.Delete(filePath.ToString())
        End If
        Response.[End]()
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Response.Redirect("~/Logindata.aspx")
    End Sub
End Class
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 05, 2022 03:31 AM
on Dec 05, 2022 11:11 PM

Hi makumbi,

Add PostBack Trigger to the Update Panel.

<Triggers>
    <asp:PostBackTrigger ControlID="StudentGrid" />
</Triggers>

Like below.

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:ScriptManager ID="ScriptManager2" runat="server">
        </asp:ScriptManager>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="723px" CssClass="auto-style2">
            <Columns>
                <asp:BoundField DataField="Topic" HeaderText="Topic" />
                <asp:BoundField DataField="Subject" HeaderText="Subject" />
                <asp:BoundField DataField="Dates" HeaderText="Date Uploaded" />
                <asp:BoundField DataField="Name" HeaderText="File Name" />
                <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkView" runat="server" CommandArgument='<%# Eval("Id") %>' OnClick="View" Text="View"></asp:LinkButton>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="StudentGrid" />
    </Triggers>
</asp:UpdatePanel>