I am generating a javascript alert box from codebehind in asp.net(vb).
The code:
Catch ex As Exception
MesgBox("Error in uploading file due to following exception" & vbNewLine & ex.ToString)
trans.Rollback()
Finally
conn.Close()
End Try
The mesgbox function is as follows:
Private Sub MesgBox(ByVal sMessage As String)
Dim msgedtble As String = sMessage.Replace("\", "\\")
msgedtble = msgedtble.Replace(vbNewLine, "\n")
Page.ClientScript.RegisterStartupScript(Me.GetType,
"myScripts",
"<script language='javascript'>alert('" & msgedtble & "');</script>")
End Sub
Now when exception is thrown, the following script is appended in the form tag of the client side html :
<script language='javascript'>alert('Error in uploading file due to following exception\nSystem.Data.SqlClient.SqlException (0x80131904): Violation of UNIQUE KEY constraint 'IX_AccountMaster'. Cannot insert duplicate key in object 'dbo.AccountMaster'.\nThe statement has been terminated.\n at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)\n at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)\n at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)\n at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)\n at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)\n at UploadAccountMasterXl.Button1_Click(Object sender, EventArgs e) in C:\\Users\\ssaa\\Documents\\Visual Studio 2010\\WebSites\\Dpp2012\\UploadAccountMasterXl.aspx.vb:line 57');</script>
Why is this not appearing in my browser(Firefox)?
Add
Imports System.Web.Script.Serializationto the top of your file, then try this:Using JavaScriptSerializer should take care of the linebreaks, single quotes, and everything else we haven’t already thought of.