Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my asp.net project I have connected to sql server in class file which is inside App_Code , Inside that with the help of session I have a connection , when session is null else part is executing where I have written
C#
public static SqlConnection OpenConnection()
        {
            try
            {
                if (HttpContext.Current.Session["DATABASE"] != null && HttpContext.Current.Session["USERNAME"] != null && HttpContext.Current.Session["PASSWORD"] != null && HttpContext.Current.Session["SERVERNAME"] != null)
                {
                    cn = new SqlConnection("uid=" + HttpContext.Current.Session["USERNAME"].ToString() + ";pwd=" + HttpContext.Current.Session["PASSWORD"].ToString() + ";server=" + HttpContext.Current.Session["SERVERNAME"].ToString() + ";database=" + HttpContext.Current.Session["DATABASE"].ToString() + "");
                    if (cn.State == ConnectionState.Closed)
                    {
                        cn.Open();
                    }

                }
                else
                {
                    HttpContext.Current.Response.Redirect("~/Login.aspx");

                }
                return cn;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

My Login Page in In the root Folder. It is coming to the else part but showing error that "Thread Was Being Abroted". Is It not getting The Proper Login Path Or What. And Is It the good way to redirect To a page.

Please Help Me.

Thanks.
Posted
Updated 16-Nov-11 18:03pm
v2
Comments
Pranav-BiTwiser 14-Mar-14 3:53am    
what if my Login.aspx is in the Folder Admin...??

You can get the details of thread aborted, using .NET framework's ThreadAbortException class.

C#
try
{
... ur code
}
catch (ThreadAbortException ex)
{
}

Redirection is always good method in web application from customer point of view.
 
Share this answer
 
Comments
[no name] 17-Nov-11 0:14am    
A redirect is not always best. Server.Transfer does not incur the client round trip.
You should not be storing your database connection information in session state. This is a very unsecure application.

If you read the documentation you'll find that the exception is expected
http://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.80).aspx[^]
 
Share this answer
 
v2
Comments
JBSAHU 17-Nov-11 0:12am    
What Should I Do Now ?
[no name] 17-Nov-11 0:16am    
read the documentation on both redirect and ado.net with asp.net
Pass the "false" value to endResponse parameter of Response.Redirect Method as below.
C#
HttpContext.Current.Response.Redirect("~/Login.aspx", false);


Have a look at below link to understand this in more details.

http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx
 
Share this answer
 
Make sure HttpContext.Current.Response.Redirect is executed before any output data (response data) is sent back. Else it will fail.

If output data is sent to the user-client then there is no chance to add the redirect header "Location:" to the response header (this is what HttpContext.Current.Response.Redirect does...) because its already writing the body.
 
Share this answer
 
Comments
OriginalGriff 31-Dec-22 11:56am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 11 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900