我的连接即将出现错误 [英] coming error in my connection

查看:77
本文介绍了我的连接即将出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
            }
        }


我已经通过类文件连接了它.当我的会话期满时,它将进入其他部分,但在其他部分则显示错误并直接转到catch块.

有没有解决的办法.....


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.....

推荐答案

尝试替换此行
try replace this line
HttpContext.Current.Response.Redirect("~/Login.aspx");



与此



with this

HttpContext.Current.Response.Redirect("~/Login.aspx",false);


我认为您的连接将实时打开,没有任何问题.
并且
I assume in true time, your connection will open with out any matter.
and
cn

始终是返回值.

如果是这样,则会发生此错误,原因是数据库",密码"或服务器名称"之一抛出并发生异常.您可以在所有情况下使用三个if块而不是一个if块来检查并填充它.将调试点保留在第一个if块之前,并检查代码.

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...


这篇关于我的连接即将出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆