A Place for C Sharpers/.Netters

I Will do coding till last moment of life-Kiran Patil

Posts Tagged ‘ASP.NET’

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occured because all pooled connections were in use and max pool size was reached.

Posted by kiranpatils on June 21, 2011

Challenge:

Have you seen following error?

timeout-expired-the-timeout-period-elapsed-prior-to-obtaining-a-connection-from-the-pool-this-may-have-occured-because-all-pooled-connections-were-in-use-and-max-pool-size-was-reached

Then this post is to solve it!

Solution:

As per the error your code has not closed the opened SqlConnection properly. For example

SqlConnection conn = new SqlConnection(

myConnectionString);
conn.Open();
doSomething(); /*  If some error occurs here — Next line will not get called and it will leave connection open */
conn.Close();

Solution:

1.
SqlConnection conn = new SqlConnection(myConnectionString);
try
{
conn.Open();
doSomething(conn);
}
finally
{
conn.Close();    // This line will get called in any case — success/failure
}

So, open your solution in Visual Studio and search in entire solution for all open connections and for all the code implement above suggested solution.

Just a note : If you have written Data Access layer code in code behind file then you are in trouble here. You have to do changes at N number of places. If you would have created separate Data Access layer (Technically Class Library) and Method to do DB operation then your task would have been easy enough!
2) You can raise the connection pool size in the connection string.  For example, you can add “Max Pool Size=100″ to your connection string to increase the pool size to 100.

Implement above solutions. You should not see any issues any more.

Good to read :
http://blogs.msdn.com/b/tolong/archive/2006/11/21/max-pool-size-was-reached.aspx
Happy DB Access! :-)

Posted in .NET, ASP.NET | Tagged: , , | 2 Comments »

ASP.NET Session with proxy server

Posted by kiranpatils on June 21, 2011

Challenge:

Y’day I came across with nice issue. Basically there is one Web Application developed using ASP.NET and deployed on IIS Server. Now, this application will be accessed by more than 2-3 people from Local area network. So, far so good.

This application uses Session and here issue comes — For all users across different machines were getting access of different persons session data — which should not be the case. Because as per theory “session” is unique for each user. But my dear friend theory is theory in real life you have to face lots of challenges like this. :-)

Solution:

So, I jumped in to this issue and first tried to understand what is going on [The basic stuff -- which I always do!].

To quick check the Session ID and other Session related information. I decided to code one page which will print my session info on page and it should show me some direction. I opened my favorite tool — Visual Studio and added one page using single file model — which I can deploy easily on server.

The code looks like below:

<%@ Page Language=”C#” AutoEventWireup=”true” %>

<%@ Import Namespace=”System” %>
<!–DOCTYPE html PUBLIC “/-/W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
xmlns=”http://www.w3.org/1999/xhtml”>
<script language=”C#” runat=”server”>

// new line
private const string breakLine = “
“;

// Strong Start
private const string strongStart = ““;

// Strong End
private const string strongEnd = “”;

private const string sessionTimeKey = “SessionTime”;

protected void Page_Load(object sender, EventArgs e)
{
// generate string with all required information
StringBuilder sessionInformation = new StringBuilder();

if (Session[sessionTimeKey] == null)
// Current Time
Session[sessionTimeKey] = DateTime.Now;

// IsCookieless
sessionInformation.Append(strongStart);
sessionInformation.Append(“IsCookieless : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Session.IsCookieless.ToString());
sessionInformation.Append(breakLine);

// IsNewSession
sessionInformation.Append(strongStart);
sessionInformation.Append(“IsNewSession : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Session.IsNewSession.ToString());
sessionInformation.Append(breakLine);

// Session.Keys.Count
sessionInformation.Append(strongStart);
sessionInformation.Append(” Total Keys Count : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Session.Keys.Count.ToString());
sessionInformation.Append(breakLine);

// Mode
sessionInformation.Append(strongStart);
sessionInformation.Append(” Session Mode : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Session.Mode.ToString());
sessionInformation.Append(breakLine);

// SessionID
sessionInformation.Append(strongStart);
sessionInformation.Append(” SessionID : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Session.SessionID);
sessionInformation.Append(breakLine);

// Timeout
sessionInformation.Append(strongStart);
sessionInformation.Append(” Timeout : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Session.Timeout.ToString());
sessionInformation.Append(breakLine);

// Session Value
sessionInformation.Append(strongStart);
sessionInformation.Append(” Session Value(DateTime) : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Convert.ToString(Session[sessionTimeKey]));
sessionInformation.Append(breakLine);

// SERVER_NAME
sessionInformation.Append(strongStart);
sessionInformation.Append(” SERVER_NAME : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Request.ServerVariables["SERVER_NAME"]);
sessionInformation.Append(breakLine);

// SERVER_PORT
sessionInformation.Append(strongStart);
sessionInformation.Append(” SERVER_PORT : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Request.ServerVariables["SERVER_PORT"]);
sessionInformation.Append(breakLine);

// LOCAL_ADDR
sessionInformation.Append(strongStart);
sessionInformation.Append(” LOCAL_ADDR : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Request.ServerVariables["LOCAL_ADDR"]);
sessionInformation.Append(breakLine);

// REMOTE_ADDR
sessionInformation.Append(strongStart);
sessionInformation.Append(” REMOTE_ADDR : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Request.ServerVariables["REMOTE_ADDR"]);
sessionInformation.Append(breakLine);

// REMOTE_HOST
sessionInformation.Append(strongStart);
sessionInformation.Append(” REMOTE_HOST : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Request.ServerVariables["REMOTE_HOST"]);
sessionInformation.Append(breakLine);

// SERVER_NAME
sessionInformation.Append(strongStart);
sessionInformation.Append(” SERVER_NAME : “);
sessionInformation.Append(strongEnd);
sessionInformation.Append(Request.ServerVariables["SERVER_NAME"]);
sessionInformation.Append(breakLine);

Response.Write(sessionInformation.ToString());

sessionInformation.Append(breakLine);
Response.Write(“———-SESSION CONTENT——————-”);
Response.Write(breakLine);

foreach (string item in Session.Contents)
{
if (Session[item] != null)
{
Response.Write(string.Format(“Key :{0} – Value : {1}”,
item,Convert.ToString(Session[item])));
Response.Write(breakLine);
}
}

}

protected void btnRefresh_Click(object sender, EventArgs e)
{
Response.Redirect(Request.Url.ToString());
}

</script>
<head runat=”server”>
<title>Session Check Page</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Button ID=”btnRefresh” runat=”server” Text=”Refresh” OnClick=”btnRefresh_Click” />
</div>
<br />
</form>
</body>
</html>

Basically, this page is my favorite page. Before a year using this page’s code only. I fixed one Live issue [Sticky Session was of on Load balancer on our web farm environment]. If you see the code of page it is quite simple. But very useful!

What I check there is Session ID, LOCAL Address and Session value which I store on page load’s not postback only. And it has one nice button called “Refresh” which redirects on same page.

We deployed this page on Server and started accessing it from different – different machines. And we started clicking on “Refresh” button which shown us few strange thing as below:

1. SessionID was changing on each Refresh button’s click – This should not be the case. Because Session ID will be unique till session expires or user closes the browser.

2. Session’s value was also changing on each refresh.

3. REMOTE_ADDR was coming same on 2-3 machines.

Now, we started removing possibilities one by one.

For issue#1 — We checked Web.Config and found that StateNetworkTimeout property has been assigned, frankly it should not cause an issue. But we thought better to remove it. So, we removed it.

#2 ,#3 – Then we realized that we are using Proxy on our LAN and REMOTE_ADDR’s IP address was IP Address of proxy server.

So, we found that Proxy is causing us an issue. To resolve it we removed proxy server’s setting from Internet explorer’s settings for few machines from which we need to access this application.

Isn’t it simple?

Happy Coding! :-)

Posted in ASP.NET | Tagged: | 6 Comments »

Access Internet/Local Website from Your Windows Mobile Device Emulators

Posted by kiranpatils on November 19, 2009

Challenge

If you want to access any website Or You want your locally developed mobile application/web application to be viewed in Windows Device Emulator then you are at the right post.

Solution
  1. Download ActiveSync and install on your local machine where you would like to run Emulator. How to setup ActiveSync?
  2. Open Visual Studio and click on Tools | Connect to Device:

clip_image002

  1. Select Device emulator to run and say connect.

clip_image004

  1. It will open your device emulator:

clip_image006

  1. Verify that ActiveSync is up and running by its symbol in Notification Area.

clip_image008

  1. Open Device Emulator Manager

clip_image010

  1. Currently running Device Emulator Manager will be shown in Green.

clip_image012

  1. Right click and say “Cradle”

clip_image014

  1. As you click on Cradle ActiveSync window will popup. Select Guest Partnership[Or better do cancel it will by default to Guest Mode]. If your Emulator got synced with ActiveSync then Notification Icon will go Green.
  1. Now you are ready to access website.
    1. Internet – Enter your URL.

clip_image016

    1. Local – To Access your Local Website which is running on Cassini(Development Web Server). To access it you need to access it by IP ADDRESS:PORTNUMBER. Please note that you can’t access it by localhost:PORTNO Because localhost points to Emulator’s localhost and it will never go to your machine. You have to see both Emulator and your local machine as a two different devices even though they are running on same machine.

clip_image018

If this blog helped you. Say a big thanks to my friends who always give me a shout whenever they found something challenging like this and inspire me to learn a new things which finally inspires me to pen down on my blog and share with you :)

Happy Emulating!

Webliography

Setup ActiveSync

Nice blog – Thanks to this blog

If you have Windows Vista or Later

Accessing Device Emulator Without ActiveSync

Feedback

Posted in ASP.NET, Windows Mobile | Tagged: , , | 10 Comments »

How to delete Cookie?

Posted by kiranpatils on June 3, 2009

Challenge:

Today, i was implementing Remember Me Functionality in one of my application. I know you know what is “Remember Me” functionality. But when user says “Signout” i need to Forgot the user and to do that i need to delete my Cookie which i persisted when user says “Remember Me”..So, you will say simple delete cookie..But sorry sir you can’t delete cookie because it is on client side and user/browser can delete it..So, shall we ask user to delete cookie?? no we should not..then we just have one friend left “Browser”. But we need to cheat our friend[I strongly discourage cheating a real friend in real life :) ]. We don’t have any method by which we can delete the cookie directly but we can delete it..Want to know how??

Solution:

Okay, Boss relax!!! Here is the solution you should set the cookies Expiry date to past date..So, whenever Browser will see that it is expired it will discard that cookie.. I have created one method which you can directly use using Control Inheritance[COPY-PASTE] :) .. in which you just need to pass Cookie’s name rest smart method will manage :)

Here it is:

public static void RemoveCookie(string key)
{
//get cookie
HttpCookie cookie = null;
if (HttpContext.Current.Request.Cookies[key] != null)
{
cookie = HttpContext.Current.Request.Cookies[key];
//You can't directly delte cookie you should
//set its expiry date to earlier date cookie.Expires = DateTime.Now.AddDays(-1);
//Add expired cookie back to response - send to browser
HttpContext.Current.Response.Cookies.Add(cookie);
}
}

Posted in ASP.NET | Tagged: , | Leave a Comment »

How to Create ASP.NET web Service and how to consume it: A Beginner’s Guide

Posted by kiranpatils on April 23, 2008

Creating this post especially for my friend. Who want to learn How to create His First Web Service. If you too want to do the same then go ahead and start your visual studio.

Service

  1. Click on Web Site Project

  2. It will open New Web site Dialog box. Choose ASP.NET web service and give it appropriate name(HelloWorldService) and select your Coding language(C#). Click OK.

    It will take some time because it will make everything ready for you sir. So, you can start quickly

  3. What it has created?

    Service.cs: is the main file where you can write you code. E.g application logic etc..Calling Data Access layer etc.

    Service.asmx will expose your service to outer world.

  4. Web Method

    If you will see Service.cs it has one method already written. You can use it as a reference implementation

[WebMethod]

public
string HelloWorld() {


return
“Hello World”;

}

Any method you want to expose you have to declare it by attribute WebMethod

Let’S create our new method.So, you can get more into methods.

Just copy paste above one and modify it will like this:

///
<summary>


/// This method will take argument as a string and show it with greeting


///
</summary>


///
<param name=”name”></param>


///
<returns></returns>

[WebMethod]


public
string
MyHelloWorld(string name)

{


return
“Hello World,”+name;


//Here you can call your Insert,Update,Delete from DAL and write return statement

}

  1. Build Web Site [CTRL+SHIFT+B]. That’s it we have our service Ready. Let’s create one consumer for it.

Consumer

Our Service is ready. It will take name and return it with Hello World name. I am creating here one Windows application for demo. You can create any client to use it.[That's why it is service :) ]

1. Click on your solution file name Means [Your focus should be on C:\HelloService or your solution name]

2. Select File | Add | New Project

3. It will open Add New Project Dialog Box. Select Windows application and give appropriate name to it. Click OK

4. So now you will have structure as under:

5. Right Click on HelloWorldConsumer project and Click on Add Web Reference- For Adding service reference.

6.It will open Add Web Reference window. Just click on Web Services in this solution. Because we have our service under same solution.

7. It will find out service. Just click on it.

8. It will load our methods as shown here: [Web reference name I had changed "localhost" to "HelloWorldClient". So you should also do it. Its good practice]. Click on Add reference button. – Which will add our service reference to client.

9. Now your soln structure should look like:

10. Click on Form1 and drag some controls so we can test it. So, your form should look like this.

11. Double click on “Call Service” Button it is Buttton1 for me. For creating its Click event handler code. And it will point it to handler code call your service here and show result in Messagebox as shown here.

12. Build solution. And ensure that HelloWorldConsumer-windows application is your startup project. Then run the project [CTRL+F5]

13. looks Cool… It’s working man. Try with your name and enjoy!!!

Links:

http://quickstarts.asp.net/QuickStartv20/webservices/

http://www.w3schools.com/webservices/default.asp

Happy Service Coding!!!

I have also kept solution here for reference. But I request you first try yourself and if get some error then and then try this. Pls try to do it yourself first. you can download it from Here

Update:

Add Reference in VS 2008 — http://www.carlosfemmer.com/post/2008/01/How-to-add-web-reference-in-VS-2008-Project.aspx

Thanks to Ravindra Joshi for pointing it out!

 

Posted in ASP.NET | Tagged: | 15 Comments »

ASP.NET TEMP ERROR While Building+WCSF/General ASP.NET

Posted by kiranpatils on February 8, 2008

from last few days when we are implementing WCSF. we are facing some errors which are really not ours..it is of TEMP files generated by ASP.NET.

Problem:

Suppose i have one Interface named as IEmployee and it has 3 members EmpName,EmpID,EmpAddress and one of my page let’s say Employee.aspx implements it so it looks like this:

Employee.aspx:IEmployee

{

//Three Members implemented

}

now i build and it shows “Build Succeeded” .

after sometime i think that i don’t need “EmpID” in interface so i delete it and also deleted its implementation from Employee.aspx and now when i Build it. it shows me error and when i click on it it points to me ASP.NET Temp files location. which are not mine. [For your knowledge i want to say that ASP.NET Generates Temporary files at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\<Web site name>]. so it will point you there. But as i think it should work fine……..for that i have to work around it for hours and hours..so want to know its solution???

Stack Trace:

Error:
Error 126 ‘Employeet’ does not implement interface member ‘IEmployee.EmployeeID’ c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\developmentwebsitea97a19d\326f353\App_Web_euqzzc2o.0.cs 14

Solution:

For solving this error just follow me:

1.Take Copy/Backup of Employee.aspx page.

2.Now Delete if from WebSite Solution.
3.Now Build Solution. [you will not found that error]

4. Now add that page again from Backup path.

5. Now Build it and That’s it…..You have killed the errors successfully.

Enjoy ASP.NET!!

-Kiran Patil

Posted in ASP.NET, Web Client Software Factory | Tagged: | 2 Comments »

 
Follow

Get every new post delivered to your Inbox.

Join 102 other followers