How to fix Error executing child request? - asp.net

I am building an asp.net app and I keep getting this error while trying to transfer pages:
Error executing child request for Edit_PropertyData.aspx
I have no clue what's causing it or how to fix it, any help would be greatly appreciated!
Here is the Page_Load event of Edit_PropertyData.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmdChangeMaterial.Attributes.Add("onclick", "CheckSaveStatus();")
cmdEditMaterial.Attributes.Add("onclick", "CheckSaveStatus();")
If Session("Mode") Is "Edit" Then
cmdEditMaterial.Enabled = True
cmdSaveData.Enabled = True
Else
cmdEditMaterial.Enabled = False
cmdSaveData.Enabled = False
End If
pnlMain.Visible = True
pnlMain.Height = Unit.Pixel(650)
End Sub

Server.Transfer doesn't allow you to do a lot of things. You're probably trying to use it to pass query string parameters or some such. But you don't need that - the URL doesn't change in the target of the transfer, so you've still got the original query string. This can be both a good and a bad thing, depending on how you use it :)
You probably want to use a different transfer method instead - if you're inside a HttpModule, have a look at HttpContext.Current.RewritePath.

What have you tried so far? A quick search gives a number of similar error messages with proposed and accepted solutions.
Server.Transfer throws Error executing child request. How to resolve?
http://forums.asp.net/t/1489436.aspx?Server+Transfer+Error+executing+child+request
http://support.microsoft.com/kb/320439
You don't mention what you're transferring from, but I suspect Response.Redirect or HttpContext.Current.RewritePath will be a better alternative for you.

Related

ASP Email Validator

I have a textbox with multiple lines in it, each being an email address. I have a button that when clicked should read the emails from the textbox and validate that they are working emails (no spelling errors or anything). How would I go about doing something like this? For context the textbox is called emailBox
VB:
Protected Sub SaveButton(sender As Object, e As EventArgs)
End Sub
Thank you all so much in advanced!
Hi there I'm guessing by spelling errors you just mean valid email syntax.
You could use a regular expression. Like this:
Protected Sub SaveButton(sender As Object, e As EventArgs)
dim regexExpression As New Regex("^[_a-z0-9-]+(.[a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$")
'get an array of emails
dim emails = emailBox.text.Split(",")
'loop through emails checking each one
For Each email in emails
dim valid = regexExpression.IsMatch(email)
'do whatever with result
Next
End Sub
you will need to import this too, add it to the top of your file:
Imports System.Text.RegularExpressions
then the variable 'valid' will have a value of True or False that you can do what you like with.
I have edited my answer, assuming you can use a comma to separate your emails.
Edit 2: I can't guarantee the regex will work 100% correctly. Run your own tests, tweak it if need be or find/write another.

How to use caching?

Basically, I retrieved a dataset and I want to cache it in the server memory for one month. So I don't need to call the query again and again when running the page within this month.
I did some research and think http://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemremovedcallback%28v=vs.110%29.aspx is the way to do the cache, basically i modified the sub codes to fit into my application
Public Sub RemovedCallback(k As String, v As Object, r As CacheItemRemovedReason)
itemRemoved = True
reason = r
End Sub
Function AddItemToCache(cacheKey as String, ds as Dataset)
itemRemoved = False
onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)
If (IsNothing(Cache(cacheKey))) Then
Cache.Add(cacheKey, ds, Nothing, DateTime.Now.AddMonths(1), TimeSpan.Zero, CacheItemPriority.High, onRemove)
End If
End Function
There are quite a few errors in this piece of code. One of the error is for Cache(cacheKey) says that " Cache is a type and cannot be used for expression"? where did i do wrong?
Sounds like you're using IIS cache. First of all, if this is your route - if you have an assemblies that may use cache (when available), you need to create Caching assembly in which you check for HTTPcontext. If it is null - you are running outside of IIS and caching will not be available.
A good alternative is to download Enterprise Library Caching blocks if you working with framework up to 3.5. If you use FW4.0+ you use system.runtime.caching. This way cache will be available always. There is also AppFaric and some third party cache implementations but this is probably outside the scope for you.
For your error, it sounds like your identifier Cache [your code is not showing how you assign it] is actually a type. That is if you did this
If Integer Is Nothing....
What you need is to use is syntax
System.Web.Caching.Cache.Add...
Now, this is instance. So, what you can do is
Dim c as Cache = System.Web.Caching.Cache
c.Add(....

ASP.NET Details View - Editing into Null Value Error

I am using ASP.NET in Visual Studio 2010.
I have a website that features a shopping cart, the user can signup which has validation using the RequiredFieldValidator control meaning they must enter something for certain fields e.g. Address 1 / E-Mail. This is also tied to a Database which for these fields does not allow null values.
My issue that is after logging in, adding items to cart, quantities etc, I have a confirmation order page - similar to most online shopping websites were the shopping basket is displayed along with the user's Customer Details, which is shown through a DetailsView, connected to a SqlDataSource which is running a query that just displays their shipping details as previously entered.
However, if I say select Edit, which is using the DetailsView's own property and change the Address1 line to nothing, the DetailsView sends it as null value after accepting and as a result the whole thing crashes.
I have tried examples like:
Protected Sub DetailsView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewPageEventArgs) Handles DetailsView1.PageIndexChanging
Try
Catch ex As Exception
lblError.Text = "Please verify changes made to Customer Details."
End Try
End Sub
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Try
Catch ex As Exception
lblError.Text = "Please verify changes made to Customer Details."
End Try
End Sub
Protected Sub detailsView1_ItemUpdated(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Try
Catch ex As Exception
lblError.Text = "Please verify changes made to Customer Details."
End Try
End Sub
So basically Try / Catch statements which has worked for everything else. But because I can't get access to any proper code behind for the DetailsView since it's just taken from Visual Studio I'm stumped. Has left me curious now, looked about here and elsewhere and couldn't find much, anything seemed similar to what I've tried there but they don't make a difference.
I want to try and create/find some form of Error Handling for this situation, similar to registering, either not proceeding with sending to the server if left blank or display a message.
This is the main part of the error:
[SqlException (0x80131904): Cannot insert the value NULL into column 'Address1', table 'C:\USERS\xxxx\PROJ (2)\PROJ\APP_DATA\ORDERS.MDF.dbo.Customer'; column does not allow nulls. UPDATE fails.
The statement has been terminated.]
Something simple I'm missing? Was considering the option of creating text boxes instead and connecting to the database individually, but that's a completely different route to this and there is quite nice, simple customizable options with the DetailsView.
Well...typical after spending weeks confused by this one I managed to get it just after posting the question.
For anyone interested in this I used the 'If e.Exception Is Nothing Then' statement within the code similar to the two bottom examples I tried, THEN, used a Try/Catch statement.

Rewrite URLS using global.asax

I'm trying to make some friendlyurls in my vb.net (.net 4) project and I'm trying to do it using something I read about global.asax and Application_Beginrequest but I can't get it to compile.
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim httpContext As System.Web.HttpContext = httpContext.Current
Dim currentURL As String = currentURL.Request.Path.ToLower()
If currentURL.IndexOf("widgets") > 0 Then
objHttpContext.RewritePath("products.aspx?ID=123")
Else
objHttpContext.RewritePath(httpContext)
End If
End Sub
Above is what i'm trying but it's erroring on the objHttpContext. is there another method? Ideally once I get the above method working I'm going to be attempting to use a database call to work out the URLs. So any suggestions in that direction will also be very welcome. I'm trying to get away from having to install anything on IIS as it's a load balenced enviroment that I'd rather not install something on every server.
Thanks
Tom
You should access HttpApplication.Context. Here is how I do it (C#):
string reqPath = Request.Url.AbsolutePath;
if(reqPath=="/")
newPath="/Pages/PL/Main.aspx";
if (newPath != "")
HttpApplication.Context.RewritePath(newPath);
As I can see in documentation you should be able to use exactly the same syntax to access the context in VB.NET.
You could also use II7 url rewrite module if you really wanted.
Why do you want use rewriting, when you can do it really easy using asp.net routing?
Please, look at the following link for more info:
http://msdn.microsoft.com/en-us/library/cc668201.aspx

Creating a dynamic graphic in asp.net

I am busy creating a control in asp.net. The control consists of some text and an image. The image will be used to display a graph. The control gets the data for the graph by reading it from a database. It seems I have two options to display the graph in the image box. First, I can create a jpg from the data and save the file on the server. I can then load this file into the image box. I think this would cause a problem if various users try to access the site at the same time, so I don't think it is a good option. The other options is to create a file called output, that outputs the graph like this: objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
I can then display the image like this in my control: Image1.ImageUrl = "output.aspx"
The problem that I am facing is how do I get the data from my control to the output page? As far as I know it is too much data to pass as a parameter. If there is another better method of doing it, please let me know
Thanks
You can write the image content to the response of output.aspx. Something like this (taken from one of my existing GetImage pages:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strFilename As String
strFilename = Request.QueryString("filename")
Dim strPath As String = AppSettings("ProxyImageSavePath") & strFilename
Response.Clear()
Response.ContentType = "image/jpeg"
If Not IO.File.Exists(strPath) Then
'erm
Response.Write("file not found")
Else
Response.WriteFile(strPath)
End If
Response.End()
End Sub
Edit: I've just realised this may not be the solution you're looking for; Your webserver should be able to cache this though.
You need handlers. See in this article, Protecting Your Images, section "Creating the ImageHandler HTTP Handler", you'll find the code how to write the handler, so it outputs an image. You don't need all, it's only an example.
You also need to register the handler in the web.config.
To reference it Image1.ImageUrl = "handler.ashx?graphdata=xxxx". Use QueryString to get the data source to generate the graph.

Resources