我的班级没有给我可以使用的价值

时间:2015-03-16 21:18:12

标签: c# oop

这就是我尝试使用我的课程为我的网站打印有价值的东西,当我尝试写我的课程时根本找不到它。

我无法找到为我的用户返回有价值的东西。

问题在于它无法在所有课程中找到。

我的课程:

public class AbonnementsId
{
    public int indhold { get; set; }
}

public AbonnementsId HentAbonnementsId()
{
    AbonnementsId AbonnementsidReturn = new AbonnementsId();

    AbonnementsidReturn.indhold = 0;

    SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    SqlCommand cmd1 = conn1.CreateCommand();
    cmd1.Connection = conn1;
    int brugerid = Convert.ToInt32(HttpContext.Current.Session["id"]);
    cmd1.CommandText = @"SELECT abonnementsid from brugere WHERE id = @id";
    cmd1.Parameters.AddWithValue("@id", brugerid);
    conn1.Open();
    SqlDataReader readerBrugerA = cmd1.ExecuteReader();
    if (readerBrugerA.Read())
    {
        AbonnementsidReturn.indhold = Convert.ToInt32(readerBrugerA["abonnementsid"]);
    }
    conn1.Close();
    return AbonnementsidReturn;
}

以下是我为自己的内容需要时编写课程的方法。

if (Session["AbonnementsId"] != null)
        {
            _subscriptionId = long.Parse(Session["AbonnementsId"].ToString());
        }
        else
        {
            //when I need to print my class do I like it here
            _subscriptionId = AbonnementsidReturn.indhold();
        }

1 个答案:

答案 0 :(得分:0)

我完全在黑暗中射击:

public static class AbonnementsId
{
    public static int GetAbonnementsId()
    {
        int abonnementsid;

        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        using(var conn = new SqlConnection(connectionString))
        {
            int brugerid = Convert.ToInt32(HttpContext.Current.Session["id"]);

            SqlCommand cmd1 = conn.CreateCommand();
            cmd1.Connection = conn;
            cmd1.CommandText = @"SELECT abonnementsid from brugere WHERE id = @id";
            cmd1.Parameters.AddWithValue("@id", brugerid);
            conn.Open();

            SqlDataReader readerBrugerA = cmd1.ExecuteReader();
            if (readerBrugerA.Read())
                abonnementsid = Convert.ToInt32(readerBrugerA["abonnementsid"]);

            conn.Close();
        }

        return abonnementsid;
    }
}

然后您可以在代码中调用它:

if (Session["AbonnementsId"] != null)
{
    _subscriptionId = long.Parse(Session["AbonnementsId"].ToString());
}
else
{
    //when I need to print my class do I like it here
    _subscriptionId = AbonnementsId.GetAbonnementsId();
}