Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
            }
        }

I have connected It through class file. When my session expire it is coming to else part but in else part it showing error and directly go to catch block.

Is there any solution.....
Posted
Updated 15-Nov-11 19:05pm
v2
Comments
hitech_s 16-Nov-11 1:05am    
added pre tags
hitech_s 16-Nov-11 1:06am    
can u post the error
JBSAHU 16-Nov-11 2:57am    
Exception was unhandled by user code

Thread was being abroted

This error is coming
Mehdi Gholam 16-Nov-11 1:15am    
What is the exception that you are getting?
JBSAHU 16-Nov-11 2:57am    
Exception was unhandled by user code

Thread was being abroted

This error is coming

try replace this line
HttpContext.Current.Response.Redirect("~/Login.aspx");


with this
HttpContext.Current.Response.Redirect("~/Login.aspx",false);
 
Share this answer
 
Comments
RaisKazi 18-Nov-11 2:09am    
Correct. 5ed. By default endResponse property is true, which cusses "ThreadAbort" Exception.
hitech_s 18-Nov-11 3:53am    
thanq
I assume in true time, your connection will open with out any matter.
and
cn
is always return value.

If so this error occurs one of "DATABASE", "PASSWORD" or "SERVERNAME" throws and exception. you can check it and populate it by using three if blocks instead of one if block for all conditions. keep the debug point before the first if block and check the code..

if(HttpContext.Current.Session["DATABASE"] != null) 
if(HttpContext.Current.Session["PASSWORD"] != null)
 HttpContext.Current.Session["SERVERNAME"] != null
{
///your code goes here
}


Hope this will help to find out the real exception...
 
Share this answer
 

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