SQL Server最奇怪的错误

时间:2016-05-28 17:53:22

标签: c# sql-server

C#代码:

SqlConnection c;

string str = "Data Source =(LocalDB)\\MSSQLLocalDB;";
str += "AttachDbFilename=|DataDirectory|\\DinoData.mdf;";
str += "Integrated Security= True";

c = new SqlConnection(str);

if (Session["Connect"] != null)
{
    if ((bool)Session["Connect"])
    {
        logdiv.Visible = false;
        Usernamec.InnerHtml = (string)Session["CurentUserid"];
        Connected.Visible = true;

        SqlCommand exp = new SqlCommand("SELECT xp FROM[User] Where Username = @username", c);
        exp.Parameters.AddWithValue("@username", (string)Session["CurentUserid"]);

        c.Open();
        Session["exp"] = exp.ExecuteReader();
        c.Close();

        int b = (int)Session["exp"] / 2;
        string a = b + "px";
        xp.Style.Add("width", ((string)Session["exp"])+"%");
    }
    else
    {
        Connected.Visible = false;
        logdiv.Visible = true;
    }
}

错误:

  

类型' System.InvalidCastException'的例外情况发生在   App_Web_ump4h2pq.dll但未在用户代码中处理

     

其他信息:指定的演员表无效。

1 个答案:

答案 0 :(得分:2)

 SqlConnection c;
        string str = "Data Source =(LocalDB)\\MSSQLLocalDB;";
        str += "AttachDbFilename=|DataDirectory|\\DinoData.mdf;";
        str += "Integrated Security= True";
        c = new SqlConnection(str);
        if (Session["Conect"] != null)
        {
            if ((bool)Session["Conect"])
            {
                logdiv.Visible = false;
                Usernamec.InnerHtml = (string)Session["CurentUserid"];
                Connected.Visible = true;
                SqlCommand exp = new SqlCommand("SELECT xp FROM[User] Where Username = @username", c);
                exp.Parameters.AddWithValue("@username", (string)Session["CurentUserid"]);
                c.Open();
                Session["exp"] = exp.ExecuteScalar();
                c.Close();
                int b = (int)Session["exp"] / 2;
                string a = b + "%";
                xp.Style.Add("width", a);
            }
            else
            {
                Connected.Visible = false;
                logdiv.Visible = true;
            }

修正:)