Friday, October 19, 2012
System.InvalidOperationException-- Maximum length exceeded
System.InvalidOperationException-- Maximum length exceeded
Problem
You get following error when you use javascript to call Web Methods defined in Web Service file(ASMX). This usually happens when you are using ASP.Net AJAX framework.The server method failed with the following error: System.InvalidOperationException-- Maximum length exceeded.
Everything works fine until the web service returns a larger number of objects.
Solution
Go to your Web.Config file. Add following sections if they don't exist under <configuration>01.
<
configuration
>
02.
<
configSections
>
03.
<
sectionGroup
name
=
"system.web.extensions"
04.
type="System.Web.Configuration.SystemWebExtensionsSectionGroup,
05.
System.Web.Extensions,
Version
=
1
.0.61025.0,
Culture
=
neutral
,
06.
PublicKeyToken
=
31bf3856ad364e35
">
07.
<
sectionGroup
name
=
"scripting"
08.
type="System.Web.Configuration.ScriptingSectionGroup,
09.
System.Web.Extensions,
Version
=
1
.0.61025.0,
Culture
=
neutral
,
10.
PublicKeyToken
=
31bf3856ad364e35
">
11.
<
section
name
=
"scriptResourceHandler"
12.
type="System.Web.Configuration.ScriptingScriptResourceHandlerSection,
13.
System.Web.Extensions,
Version
=
1
.0.61025.0,
14.
Culture
=
neutral
,
PublicKeyToken
=
31bf3856ad364e35
"
15.
requirePermission
=
"false"
16.
allowDefinition
=
"MachineToApplication"
/>
17.
<
sectionGroup
name
=
"webServices"
18.
type="System.Web.Configuration.ScriptingWebServicesSectionGroup,
19.
System.Web.Extensions,
Version
=
1
.0.61025.0,
20.
Culture
=
neutral
,
PublicKeyToken
=
31bf3856ad364e35
">
21.
<
section
name
=
"jsonSerialization"
22.
type="System.Web.Configuration.ScriptingJsonSerializationSection,
23.
System.Web.Extensions,
Version
=
1
.0.61025.0,
24.
Culture
=
neutral
,
PublicKeyToken
=
31bf3856ad364e35
"
25.
requirePermission
=
"false"
allowDefinition
=
"Everywhere"
/>
26.
<
section
name
=
"profileService"
27.
type="System.Web.Configuration.ScriptingProfileServiceSection,
28.
System.Web.Extensions,
Version
=
1
.0.61025.0,
29.
Culture
=
neutral
,
PublicKeyToken
=
31bf3856ad364e35
"
30.
requirePermission
=
"false"
31.
allowDefinition
=
"MachineToApplication"
/>
32.
<
section
name
=
"authenticationService"
33.
type="System.Web.Configuration.ScriptingAuthenticationServiceSection,
34.
System.Web.Extensions,
Version
=
1
.0.61025.0,
35.
Culture
=
neutral
,
PublicKeyToken
=
31bf3856ad364e35
"
36.
requirePermission
=
"false"
37.
allowDefinition
=
"MachineToApplication"
/>
38.
</
sectionGroup
>
39.
</
sectionGroup
>
40.
</
sectionGroup
>
41.
</
configSections
>
42.
<
system.web.extensions
>
43.
<
scripting
>
44.
<
webServices
>
45.
<
jsonSerialization
maxJsonLength
=
"5000000"
>
46.
<
converters
>
47.
</
converters
>
48.
</
jsonSerialization
>
49.
</
scripting
>
50.
</
system.web.extensions
>
51.
</
configuration
>
Cause
When you call ASP.Net Web Methods from javascript, it uses serialization and de-serialization of returned objects. Serialization converts .Net objects to XML format.When this XML data length exceeds default limit of serialization, ASP.Net throws this error. To prevent this error you should make sure that returned object does not generate large XML data. If it's not possible to decrease data, you can use above solution to fix it. But this may slow down ASP.Net application as it has to transmit so much of data for each call.Server Application Unavailable Error
Server Application Unavailable Error
Problem
I have installed IIS 5.1 on my machine ,it works allright for asp pages but when I try to access .aspx pages it throws out following error,"Server Application Unavailable"
Even when have a look at event viewer I find the following response.
aspnet_wp.exe
could not be launched because the username and/or password supplied in
the processModel section of the config file are invalid.
Solution
You should check following,(1) Go to your website properties in IIS by right clicking on it. Make sure ASP.Net version is 2.0 or whatever you are using.
(2) Navigate to your website folder from windows explorer. Right click on this folder and open properties. Go to Security tab and see if there are read permissions for following accounts
(a) [Your Machine Name]\ASPNet (b) Network Service (c) IUSR_[YourMahineName]
One of these accounts is used for running ASP.Net process. If these accounts do not have read access to your website folder. You will get this error.
(3) Make sure that you have installed IIS first and then .Net Framework. If you have installed in reverse order, run following command from the directory where .Net Framework is installed. In my case it is,
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i
This will install .Net Framework again. -i switch is for installation.
After installing .Net Framework check step (1) again.
Cannot convert type ASP.login_aspx to System.Web.UI.WebControls.Login
Cannot convert type ASP.login_aspx to System.Web.UI.WebControls.Login
Problem
You get following error when you run ASP.Net login page1.
Cannot convert type ASP.login_aspx to System.Web.UI.WebControls.Login
Solution
- Go to your login.aspx.cs code behind file.
- Find line where page class is defined.
1.
public partial class Login : System.Web.UI.Page
- Rename this class to something else. For example,
1.
public partial class clsLogin : System.Web.UI.Page
- Now go to your webform code in login.aspx file. And change inherit attribute to point to clsLogin class.
1.
<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true"
2.
CodeFile="Login.aspx.cs" Inherits="Login" %>
Change this to,1.
<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true"
2.
CodeFile="Login.aspx.cs" Inherits="clsLogin" %>
This should solve your problem
Cause
Login is treated as a reserve word in ASP.Net. If you use it for your class name, it conflicts with existing ASP.Net Login class.Online Stock Streaming using ASP.Net
Online Stock Streaming using ASP.Net
Introduction
It always made me wonder how sites like http://finance.yahoo.com and http://finance.google.com display stock quotes online so quickly and without refreshing entire page. So I decided to implement something similar. It looks very difficult to implement these kind of streaming, but ASP.Net and it's feature rich AJAX controls makes it really simple to implement this. This is my first attempt to build similar streaming web page. This sample may sound fairly easy to you and some of you might think that everyone knows this. But it might provide a good base for those who wants to build similar web sites and still have no idea where to start.Using Code
Sample code provided in this article includes a web page Default.aspx and Default.aspx.cs code behind.Default.aspx (UI)
UI code is fairly simple. First component is Script Manager. This component is necessary for AJAX functionality. After Script Manager, next component is UpdatePanel. This ajax control stops ASP.Net page from posting back. Instead of posting back page, it sends control inputs to server using client side javascripts. When it receives response from server, it refreshes content inside it on client side. Next control is an AJAX timer control. It rebinds grid with new quotes every 2 seconds.
Default.aspx.cs (Code Behind)
01.
DataTable GetGridData()
02.
{
03.
DataTable DT = new DataTable();
04.
DT.Columns.Add("Symbol",typeof(string));
05.
DT.Columns.Add("Company Name",typeof(string));
06.
DT.Columns.Add("LastTrade", typeof(float));
07.
DT.Columns.Add("Bid", typeof(float));
08.
DT.Columns.Add("Ask", typeof(float));
09.
DT.Columns.Add("Volume", typeof(int));
10.
DT.Columns.Add("Market Capital", typeof(string));
11.
DT.Columns.Add("EPS", typeof(float));
12.
DT.Columns.Add("P/E", typeof(float));
13.
14.
Random r = new Random();
15.
16.
//Apple
17.
18.
//Get a random value of last trade, bid, ask and volume.
19.
20.
float LastTrade = ((float)r.Next(17222,17434))/100;
21.
float Bid = LastTrade - (float)0.34;
22.
float Ask = LastTrade + (float)0.43;
23.
int Volume = r.Next(23000000, 23600000);
24.
DT.Rows.Add("AAPL", "Apple Inc.", LastTrade, Bid, Ask,
25.
Volume, "153.88B", 3.93, 44.71);
26.
27.
//Microsoft
28.
29.
LastTrade = ((float)r.Next(3378, 3487)) / 100;
30.
Bid = LastTrade - (float)0.34;
31.
Ask = LastTrade + (float)0.43;
32.
Volume = r.Next(38064903, 40075689);
33.
DT.Rows.Add("MSFT", "Microsoft Corporation", LastTrade,
34.
Bid, Ask, Volume, "153.88B", 2.13, 21.2);
35.
36.
//Yahoo
37.
38.
LastTrade = ((float)r.Next(2520, 2834)) / 100;
39.
Bid = LastTrade - (float)0.34;
40.
Ask = LastTrade + (float)0.43;
41.
Volume = r.Next(14400000, 15500000);
42.
DT.Rows.Add("YHOO", "Yahoo Inc.", LastTrade, Bid, Ask,
43.
Volume, "34.48B", 1.42, 10.23);
44.
45.
//Google
46.
47.
LastTrade = ((float)r.Next(67025, 69000)) / 100;
48.
Bid = LastTrade - (float)5;
49.
Ask = LastTrade + (float)5;
50.
Volume = r.Next(4500000, 5000000);
51.
DT.Rows.Add("GOOG", "Google Inc.", LastTrade, Bid, Ask,
52.
Volume, "213.73B", 12.42, 53.23);
53.
54.
//WYNN
55.
56.
LastTrade = ((float)r.Next(13249, 14022)) / 100;
57.
Bid = LastTrade - (float)0.90;
58.
Ask = LastTrade + (float)0.89;
59.
Volume = r.Next(1222785, 1400000);
60.
DT.Rows.Add("WYNN", "Wynn Resorts Ltd.", LastTrade, Bid, Ask,
61.
Volume, "15.13B",1.81, 73.15);
62.
63.
return DT;
64.
}
65.
1.
protected void Timer1_Tick(object sender, EventArgs e)
2.
{
3.
//Bind grid view
4.
GridView1.DataSource = GetGridData();
5.
6.
GridView1.DataBind();
7.
}
Points of Interest
As you can see here, I am using a simulation for stock quotes. It would be interesting to know how can we fetch real time quotes from Yahoo/Google or any other source.These are few links I have found which discusses this,
http://tutorials.programmingsite.co.uk/yahoocsv.php http://asp.programmershelp.co.uk/xmlstock1.php
http://www.webdeveloper.com/forum/archive/index.php/t-6821.html
http://aspalliance.com/articleViewer.aspx?aId=112&pId=
Virtual directory not being configured as an application in IIS
Virtual directory not being configured as an application in IIS
Problem
01.
Configuration Error
02.
Description: An error occurred during the processing of a configuration
03.
file required to service this request. Please review the specific error
04.
details below and modify your configuration file appropriately.
05.
06.
Parser Error Message: It is an error to use a section registered as
07.
allowDefinition='MachineToApplication' beyond application level.
08.
This error can be caused by a virtual directory not being configured
09.
as an application in IIS.
10.
11.
Source Error:
12.
13.
14.
Line 53:
15.
Line 54:
16.
Line 55:
17.
Line 56:
Solution
Change authentication mode to 'None' in web.config.Go to your web.config file and change following line
1.
1.
< authentication mode="None" />
This should solve this problem.
Note: If above solution does not work then make sure that the directory from where you website is running is configured as an application in IIS. To do this, you need to go into IIS and find your project folder on the server, right click to Properties. Under Application Settings click Create.
Cause
By default ASP.Net creates Windows authentication mode in Web.Config file. When you run this site locally on your computer, it runs fine. This is because it runs under current windows account credentials (i.e. Your windows login which may have administrative rights). But when you copy it to web server it runs under [MACHINENAME]\ASPNet account. If it does not have appropriate rights, server will throw this error.The relative virtual path MyUserControl.ascx is not allowed here error in Web.Config
The relative virtual path MyUserControl.ascx is not allowed here error in Web.Config
Problem
I am trying to add a user control in Web.Config file as below,
< controls>
< add tagPrefix="MyControl" tagName="MyUserControl" src="/MyUserControl.ascx"/>
< /controls>
It gives me following error,
Configuration Error: The relative virtual path 'MyUserControl.ascx' is not allowed here.
Solution
To resolve this issue, move your WebUserControl from root directory to a sub directory. For example in my case, I would copy MyUserControl.ascx to sub folder "Controls". So my Web.Config entry should look like below.
< controls>
< add tagPrefix="MyControl" tagName="MyUserControl" src= "~/Controls/MyUserControl.ascx"/>
< /controls>
~ indicates relative path from root folder. This is one of the limitation of ASP.Net application. It does not allow you to register user control in Web.Config file unless it's in a subfolder.
Send Email from your GMAIL account using ASP.Net and C#
Send Email from your GMAIL account using ASP.Net and C#
Download Sample
You can use your gmail account to send emails from ASP.Net and C#, using SMTP. You need to set SSL credentials. This is not straight forward like a simple SMTP email. After searching for long hours I was able to find a solution. Use following class to send emails using your GMAIL account.
Please supply your gmail user name and password in following class where needed.
01.
using System.Web.Mail;
02.
using System;
03.
public class MailSender
04.
{
05.
public static bool SendEmail(
06.
string pGmailEmail,
07.
string pGmailPassword,
08.
string pTo,
09.
string pSubject,
10.
string pBody,
11.
System.Web.Mail.MailFormat pFormat,
12.
string pAttachmentPath)
13.
{
14.
try
15.
{
16.
System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
17.
myMail.Fields.Add
19.
"smtp.gmail.com");
20.
myMail.Fields.Add
22.
"465");
23.
myMail.Fields.Add
25.
"2");
26.
//sendusing: cdoSendUsingPort, value 2, for sending the message using
27.
//the network.
28.
29.
//smtpauthenticate: Specifies the mechanism used when authenticating
30.
//to an SMTP
31.
//service over the network. Possible values are:
32.
//- cdoAnonymous, value 0. Do not authenticate.
33.
//- cdoBasic, value 1. Use basic clear-text authentication.
34.
//When using this option you have to provide the user name and password
35.
//through the sendusername and sendpassword fields.
36.
//- cdoNTLM, value 2. The current process security context is used to
37.
// authenticate with the service.
38.
myMail.Fields.Add
40.
//Use 0 for anonymous
41.
myMail.Fields.Add
43.
pGmailEmail);
44.
myMail.Fields.Add
46.
pGmailPassword);
47.
myMail.Fields.Add
49.
"true");
50.
myMail.From = pGmailEmail;
51.
myMail.To = pTo;
52.
myMail.Subject = pSubject;
53.
myMail.BodyFormat = pFormat;
54.
myMail.Body = pBody;
55.
if (pAttachmentPath.Trim() != "")
56.
{
57.
MailAttachment MyAttachment =
58.
new MailAttachment(pAttachmentPath);
59.
myMail.Attachments.Add(MyAttachment);
60.
myMail.Priority = System.Web.Mail.MailPriority.High;
61.
}
62.
63.
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
64.
System.Web.Mail.SmtpMail.Send(myMail);
65.
return true;
66.
}
67.
catch (Exception ex)
68.
{
69.
throw;
70.
}
71.
}
72.
}
Example usage of this class,
1.
EmailLib.SendEmail.SendEmail("your_gmail_id",
2.
"your_gmail_password",
3.
"to_email @ anydomain.com",
4.
"This is email subject" ,
5.
"This is email body",
6.
Web.Mail.MailFormat.Text,
7.
"Physical path to your Attachment")
Note: If you do not want to attach a file with email, supply blank string as "".
Subscribe to:
Posts (Atom)