I want to pass the exception Url as a string to a method that I have created , so that it shows the exception Url in the mail (that is sent).
I have done the following in my ApplicationError() event in my Global.asax
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
Dim lastException As Exception = Server.GetLastError()
If (lastException Is Nothing) Then
Return
End If
Dim context As HttpContext = HttpContext.Current
Dim myMethodforMail As New myMethodforMail()
Dim exUrl As String = context.Request.Url.ToString()
Dim webServerName As String = Server.MachineName
myMethodforMail.HandleApplicationError(exUrl, webServerName)
Server.ClearError()
End Sub
But the url is printed as http://localhost:22035/examplepage.aspx?PageNumber!0331
instead of http://localhost:22035/examplepage.aspx?PageNumber=650331
could anyone kindly help me how to decode the ! in the Url and represent the Url as it is, please . ThankYou.
Related
I want to send a Message by clicking on a LinkButton through yahoo messenger in Asp.net. Some times It works and sometimes I get this errors: Windows can not access the spesified device, path or file. You may not have appropriate permission to access this item and also Error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 400 This is my code in VB, What is wrong with it?
Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
Dim idname As String = "YMID"
Dim message As String = "Hello, This is my YM message"
Dim sendim As String = String.Format("ymsgr:SendIM?{0}&m={1}", idname, message)
LinkButton2.PostBackUrl = (sendim)
End Sub
I have a form that the user fills out and submits. I have vb code that converts that form into an email and sends it to me. I need to know what page it is coming from so I want to assign the current url to a variable that will be included in the email.
Simply put: How do I assign the URL to a variable?
You can find this in the request object. e.g. Request.Url.AbsoluteUri
Dim url As String = Request.Url.AbsoluteUri
VB.NET
'static variable
Private Shared prevPage As String = String.Empty
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Not IsPostBack) Then
prevPage = Request.UrlReferrer.ToString()
End If
End Sub
this is my code right now:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
System.Net.ServicePointManager.Expect100Continue = False
Dim client = New System.Net.WebClient()
Dim postdata = New System.Collections.Specialized.NameValueCollection
postdata.Add("username", "qweqwe")
postdata.Add("password", "asdasd")
Dim bytes = client.UploadValues("https://juzcode.com/post.php", postdata) ' exception here
Response.Write(Encoding.ASCII.GetString(bytes))
End Sub
However I'm getting exception "Unable to connect to the remote server". Am I doing something wrong?
Btw this is the code for http://juzcode.com/post.php:
<?php if($_POST["username"]==="qweqwe" && $_POST["password"]==="asdasd")echo "<b>success</b>";else echo "<i>failed</i>";
Here is an example on the web of using c# to post to PHP:
http://blog.brezovsky.net/en-text-1.html
Put your code into a Try / Catch you may be trying to fight a loosing battle behind a corporate firewall.
Check the exception.
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
Hello everyone I've been struggling with this error message for days:
Here is the error message:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 7:
Line 8: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Line 9: UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString()
Line 10: cannotUploadImageMessage.Visible = False
Line 11: End Sub
Source File: C:\Users\Collins\Documents\Visual Studio 2005\WebSites\living to please god world\PhotoAdmin\Default.aspx.vb Line: 9
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
PhotoAdmin_Default.Page_Load(Object sender, EventArgs e) in C:\Users\Collins\Documents\Visual Studio 2005\WebSites\living to please god world\PhotoAdmin\Default.aspx.vb:9
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
This is my complete code to upload photos. Can someone help me please?
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Partial Class PhotoAdmin_Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString()
cannotUploadImageMessage.Visible = False
End Sub
Protected Sub dvPictureInsert_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvPictureInsert.ItemInserted
'If the record was successfully inserted, save the picture
If e.AffectedRows > 0 Then
'Determine the maximum pictureID for this user
Dim results As DataView = CType(maxPictureIDDataSource.Select(DataSourceSelectArguments.Empty), DataView)
Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer)
'Reference the FileUpload control
Dim imageUpload As FileUpload = CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)
If imageUpload.HasFile Then
Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")
imageUpload.SaveAs(baseDirectory & pictureIDJustAdded & ".jpg")
End If
End If
If e.Exception Is Nothing Then
' Use the AffectedRows property to determine whether the
' record was inserted. Sometimes an error might occur that
' does not raise an exception, but prevents the insert
' operation from completing.
If e.AffectedRows = 1 Then
MessageLabel.Text = "Record inserted successfully."
Else
MessageLabel.Text = "An error occurred during the insert operation."
' Use the KeepInInsertMode property to remain in insert mode
' when an error occurs during the insert operation.
e.KeepInInsertMode = True
End If
Else
' Insert the code to handle the exception.
MessageLabel.Text = e.Exception.Message
' Use the ExceptionHandled property to indicate that the
' exception has already been handled.
e.ExceptionHandled = True
e.KeepInInsertMode = True
End If
End Sub
Protected Sub dvPictureInsert_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvPictureInsert.ItemInserting
Dim cancelInsert As Boolean = False
Dim imageUpload As FileUpload =CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)
If Not imageUpload.HasFile Then
cancelInsert = True
Dim acceptedExtensions = New String() {".jpg", ".png", ".gif"}
If Not acceptedExtensions.Contains(imageUpload.FileName, StringComparer.OrdinalIgnoreCase) Then
cancelInsert = True 'Invalid image file!
End If
Else
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(imageUpload.PostedFile.InputStream)
If image.Width > 1300 Or image.Height > 950 Then
cancelInsert = True
End If
End If
If cancelInsert Then
e.Cancel = True
cannotUploadImageMessage.Visible = True
End If
'Set the UserId value to the currently logged on user's ID
e.Values("UserId") = Membership.GetUser().ProviderUserKey
'Set the UploadedOn value to the current date/time
e.Values("UploadedOn") = DateTime.Now
End Sub
Protected Sub gvPictures_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles gvPictures.RowDeleted
Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")
Dim fileName As String = baseDirectory & e.Keys("PictureID") & ".jpg"
File.Delete(fileName)
End Sub
Protected Sub gvPictures_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvPictures.RowUpdating
e.NewValues("UserId") = Membership.GetUser().ProviderUserKey
End Sub
End Class
Are you sure Membership.GetUser() is actually returning a user?
You are authenticated and not an anonymous user, correct? You should add a null check or a check to make sure they are authenticated first. I believe you can use User.Identity.IsAuthenticated
Just while editing for formatting I see line 9 has to do with membership and a control. Gut instinct is saying you're not logged in or that control doesn't exist.
Place a breakpoint at your line 9, run the site in debug, and see if the Membership.GetUser() or UserIdValue are null.
I have a asp.net c# web application with contains reports in remote processing mode. I am using the report-viewer control to render the reports. When I run the application in debug mode, I'm able to view my reports however when I publish the application to a different server I get this error message:
The request failed with HTTP status 401: Unauthorized.
My report server is on a different server than the location of my published web application. I have added new role assignment to my report server and also added to my web.config but the error persists. I think I am missing something in my aspx page for reportviewer.
Any input would be appreciated.
I'm assuming you already have set the server in your codebehind such as this
reportviewer.ServerReport.ReportServerUrl = "http://{server_ip}/reportserver";
or via the properties of the report viewer control. Make sure you change {server_ip} to the actual for the report server.
Other such problems I've seen in the past have to do with access for individual reports. Since this is cross-server, you'll need a proxy user set to view the reports.
Here are 2 examples from MSDN:
Example 1
Example 2
Yes! use Page_Load & Page_Init in this way
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ReportViewer1.Visible = True
Dim strReportsFolder As String = ConfigurationManager.AppSettings("ReportsFolder")
Dim reportName As String = "report1"
ReportViewer1.ServerReport.ReportPath = strReportsFolder + reportName
End Sub
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
PopulateReport(ReportViewer1, "")
End Sub
Public Shared Sub PopulateReport(ByVal rptViewer As ReportViewer, ByVal reportName As String)
Dim strReportServer As String = ConfigurationManager.AppSettings("ReportServer")
Dim strUserName As String = ConfigurationManager.AppSettings("Username")
Dim strPassword As String = ConfigurationManager.AppSettings("Password")
Dim strDomain As String = ConfigurationManager.AppSettings("Domain")
rptViewer.ServerReport.ReportServerUrl = New Uri(strReportServer)
rptViewer.ServerReport.ReportServerCredentials = New ReportViewerCredentials(strUserName, strPassword)
End Sub