Windows service and aspx - asp.net

1) I have a windows service which will download documents. Now it is saving correctly to textfile.
2) Class library which have load function and save function
3) Website for viewing the files load and save to database.
For this In my windows service I have a reference to class library.
In my website I have reference to class library . But when I run my website "Exception of type 'System.OutOfMemoryException' was thrown" error is showing.
I am confused as the reference made is wrong or not.Help will be greatly appreciated.
Thanks
Code in Class library SerLib
Public Function SavetoDB()
dim X As String
Dim obj As clsMRec
Try
cmd =
New SqlCommand
cmd.Connection = con
For Each obj In clsMCollect_mails
cmd.CommandText =
"Insert into Master(From,,Subject) Values('" & mailRecObj.From & "',' " & mailRecObj.Sub & "',')"
cmd.ExecuteNonQuery()
Next
Catch ex As Exception
X = ex.Message()
End Try
End Function
End Class
In the website
Dim insMarec As New clsMaRec
' Dim c As p
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
insMarec.SavetoDB()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
insMarec.LoadfromServer()
End Sub

It seems there is not a problem with your reference
System.OutOfMemoryException' It is thrown by the .NET Framework execution engine. It can occur during any allocation call during runtime.
Example that raises out-of-memory exception: C#
class Program
{
static void Main()
{
// This program attempts to create a string of 2.1 billion characters.
// ... This results in an out-of-memory error.
// ... It would require 4.2 billion bytes (4 gigabytes).
string value = new string('a', int.MaxValue);
}
}
Output
Unhandled Exception: OutOfMemoryException.
Question
How much memory can be assumed?
No specific amount of memory in bytes can be counted on when executing a program. For most programs that do not allocate huge amounts of memory, this is not a serious problem.
However:
If you have a problem with this exception and the cause is not obvious, you can use MemoryFailPoint to help diagnose the issue.
So finally check again your code where you made any mistake like above example...
or Add some code in your qustion so we can understand batter ..

Related

Make asp.net webpage respond to WebClient request

I have run into a problem in my asp.net and vb.net programming. I have made a website which connects to a database to authenticate users. I have made another page which my windows form application uses to authenticate users so the program does not have to connect directly to the MySql Database as this is insecure.
So far, the code for my asp.net page is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If Request.QueryString.Keys(0) = "key" And Request.QueryString.Keys(1) = "getapikeyv1" And Request.QueryString.Keys(2) = "authapienc2" Then
Dim key As String = Request.QueryString.Get("key")
If key = "dua9r3rqpK{WPDWp9r93af03r8yqrEPIF{A}W~DW)D08q3raewdawdug9q8wdyaw9wq" Then
returnLabel.Text = "XYeJay4XVh6amvf1kJ34Y6y6hR0sSokP9oJFsi0Mxl4QB0T86W0iac9o2nRfCmFmsONq6sPz+rdX+/NqFk3inHRGnwnqf0IsQXE/M/SvnvY="
Else
returnLabel.Text = "invk"
End If
Else
returnLabel.Text = "invalidrequest"
End If
End If
End Sub
The default value for 'returnLabel' is INVALIDREQUEST. My api keys defined above are working fine so are not needed to be considered here. However, when I use the following code to communicate with the webpage, it always returns with returnLabel's value as 'INVALIDREQUEST'. Even if I put the returnLabel.Text = "1233" at the beginning of the Me.Load sub, the page still returns INVALIDREQUEST when I communicate with it through my program!
I use this code to request:
Try
Dim APIRequest As New WebClient
APIRequest.QueryString.Add("key", r9j0wqfha0fh0wf0.DecryptString128Bit(APIKey5, dVadaihwdq93ra0w0))
APIRequest.QueryString.Add("getapikeyv1", "")
APIRequest.QueryString.Add("authapienc2", "")
Dim ako39rqwfa As String = APIRequest.DownloadString(EncryptionPageUrl)
MsgBox(ako39rqwfa)
Dim m As Match = Regex.Match(ako39rqwfa, "<span id=""returnLabel"">.*?</span>")
APIKey00 = m.Value.Replace("<span id=""returnLabel"">", "").Replace("</span>", "")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
As you can see, the WebClient I use is fine but the Me.Load sub in the webpage is not responding the request made by the program...
Is there any way I can make the webpage respond to the WebClient.DownloadString() function?
Thanks,
Rodit
(ps. c# or vb code would be appreciated)
Change your asp.net page to check the query string like this, not sure if you can assume they are in a certain order:
If Request.QueryString.Get("key") isnot nothing And
Request.QueryString.Get("getapikeyv1") isnot nothing And
Request.QueryString.Get("authapienc2") isnot nothing Then

Update server side control while exporting Crystal report using exporttohttpresponse to reponse

I am using the following code to create a Crystal report after pressing an ASP button on my webpage:
label1.text = now.tostring()
oRpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "Ticket")
The label doesn't get updated because the full postback is stopped by the download request. I use that label to update some database entries and can't get it to persist. Do I have to use a session variable to store the date or is there some known workaround for this issue?
You may try the multi-Threading, separating into two processes, one for update label1.Text (run as BackgroundWorker) and another for export the report. Final, let them parallel run.
As you used VB.Net, here is sample code:
Sub UpdateDate()
...
Label1.Text = now.ToString()
End Sub
Sub ExportReport()
...
oRpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "Ticket")
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Thread1 As New System.Threading.Thread(AddressOf UpdateDate)
Thread1.IsBackground = True
Thread1.Start()
Dim Thread2 As New System.Threading.Thread(AddressOf ExportReport)
Thread2.Start()
End Sub
Don't forget to import needed namspace
Imports System.Threading

Scope of Page.Cache property in ASP.NET 4.0

I'm now work on ASP.NET project
and want to use Page.Cache property to cache the String data like bellow way.
but, it behaves like having a Session scope.
I understand Page.Cache property is retuning a current System.Caching.Cache object
and that must have an Application scope.
I could check below code works fine, but my project's code not -- it makes cache for per session.
And, that replaced Cache of Application (with Lock and UnLock) works fine too.
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim key_str As String = "cache_key"
Dim cached_value = Cache.Get(key_str)
If cached_value Is Nothing Then
cached_value = "stored_value"
Cache.Insert(key_str, cached_value, Nothing, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5), CacheItemPriority.Normal, New CacheItemRemovedCallback(AddressOf RemovedCallback))
End If
Label1.Text = cached_value
End Sub
Public Sub RemovedCallback(ByVal key As String, ByVal value As Object, ByVal removedReason As CacheItemRemovedReason)
Debug.WriteLine("#Callback!")
End Sub
End Class
above code
works fine
my project code
works like session scope
and If replaced Cache with Application, that works fine
Are there any possible to occur such a behavior or not?
(or I just made a mistake on anywhere else in logics?)
Please point out If concerning some configure files.
I maybe made a mistake.
My project is a Azure project and so Page.Cache is to store a data for per azure instance? Right?

I can't send post data to another server

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.

Report Viewer error: The request failed with HTTP status 401: Unauthorized

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

Resources