方法问题

时间:2011-04-21 22:44:45

标签: asp.net c#-4.0

我还有另外一个问题。我的方法:

public StudentProfile GetFullStudentProfile(int userID)
{
    SqlConnection conn = new SqlConnection(Config.DbConnectionString);
    SqlCommand cmd = new SqlCommand("GetFullUserProfile", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int));
    cmd.Parameters["@UserID"].Value = userID;
    StudentProfile sp = null;
    try
    {
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);

        if (reader.Read())
        {
            sp = new StudentProfile((int)reader["UserID"], (string)reader["UserName"], (string)reader["UserFamilyName"], (DateTime)reader["UserBirthDay"], (string)reader["UserTelephone"], (string)reader["UserEmail"], (DateTime)reader["UserRegDate"], (string)reader["UserComment"], (bool)reader["UserActive"]);
        }

        reader.Close();

    }
    catch (Exception ex)
    {
        //lbl.Text = ex.Message;
    }
    finally
    {
        conn.Close();
    }
    return sp;
}

WebUserControl中的代码:

protected void ddlStudents_SelectedIndexChanged(object sender, EventArgs e)
{
    CatalogAccess ca=new CatalogAccess();
    lblEmail.Visible = true;
    lblName.Visible = true;

    lblTelephone.Visible = true;
    HiddenID.Value = ddlStudents.SelectedValue;
    lblName.Text = HiddenID.Value;
    lblEmail.Text = ca.GetFullStudentProfile(ddlStudents.SelectedIndex).UserEmail;
    lblFamilyName.Visible = true;
    lblBirth.Visible = true;
    ddlStudents.Items.Clear();
    PopulateStudentsDDL();

}

当我运行代码时,我会遇到异常 enter image description here

我是否理解问题出在方法GetFullStudentProfile()中?如果是这样,怎么解决?

我认为问题是:

StudentProfile sp = null;

所以它说变量sp没有填充try块中的数据。是吗?

现在添加StudentProfile.cs类列表:

public class StudentProfile

{     public StudentProfile(int userID,string userName,string userFamilyName,DateTime userBirthDay,string userTelephone,string userEmail,DateTime userRegDate,string userComment,bool userActive)     {         UserID = userID;         UserName = userName;         UserFamilyName = userFamilyName;         UserBirthDay = userBirthDay;         UserTelephone = userTelephone;         UserEmail = userEmail;         UserRegdate = userRegDate;         UserComment = userComment;         UserActive = userActive;     }

public int UserID
{
    get;
    set;
}

public string UserName
{
    get;
    set;
}

public string UserFamilyName
{
    get;
    set;
}

public DateTime UserBirthDay
{
    get;
    set;
}

public string UserTelephone
{
    get;
    set;
}

public string UserEmail
{
    get;
    set;
}

public DateTime UserRegdate
{
    get;
    set;
}

public string UserComment
{
    get;
    set;
}

public bool UserActive
{
    get;
    set;
}

}

4 个答案:

答案 0 :(得分:0)

究竟是什么?我敢打赌GetFullStudentProfile方法返回null。

答案 1 :(得分:0)

是的,这正是发生的事情。未找到匹配传递ID的学生。我会在您的客户端代码中执行以下操作:

StudentProfile sp=ca.GetFullStudentProfile(ddlStudents.SelectedIndex);
lblEmail.Text = sp != null ? sp.UserEmail : string.Empty;

另外,我会将GetFullStudentProfile方法的阅读器操作重写为:

conn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);

if (reader.Read())
{
    sp = new StudentProfile((int)reader["UserID"], (string)reader["UserName"], (string)reader["UserFamilyName"], (DateTime)reader["UserBirthDay"], (string)reader["UserTelephone"], (string)reader["UserEmail"], (DateTime)reader["UserRegDate"], (string)reader["UserComment"], (bool)reader["UserActive"]);
}

reader.Close();

这只是对日常工作的改头换面,看起来更漂亮了imho

答案 2 :(得分:0)

此外:

HiddenID.Value = ddlStudents.SelectedValue;
lblName.Text = HiddenID.Value;

我在你的Autos中注意到这个名字被设置为1.所以我认为你的意思是把它设置为:

lblName.Text = ddlStudents.SelectedValue;

我不相信将价值设定为等于你将以任何方式使你受益。检查您的数据库,UserEmail的列是否允许无效?

另外,在您的数据库中,我相信UserID从1开始递增,对吗?我相信SelectedIndex从0开始。您可能需要将selectedvalue项增加1。

我只是看了你的另一个问题(http://stackoverflow.com/questions/5745820/assign-list-member-to-label),我觉得我在这里有更多的信息。

如果您的数据库不允许可以为空的类型,我会改变:

CatalogAccess ca=new CatalogAccess();
lblEmail.Visible = true;
lblName.Visible = true;

lblTelephone.Visible = true;
HiddenID.Value = ddlStudents.SelectedValue;
lblName.Text = HiddenID.Value;
lblEmail.Text = ca.GetFullStudentProfile(ddlStudents.SelectedIndex).UserEmail;
lblFamilyName.Visible = true;
lblBirth.Visible = true;
ddlStudents.Items.Clear();
PopulateStudentsDDL();

为:

CatalogAccess ca=new CatalogAccess();
lblEmail.Visible = true;
lblName.Visible = true;

lblTelephone.Visible = true;

// if your table's ID increments starting at 0, otherwise ddlStudents.SelectedIndex + 1
StudentProfile student = GetFullStudentProfile(ddlStudents.SelectedIndex);

if (student != null)
{
    lblName.Text = student.UserName;
    lblEmail.Text = student.UserEmail;
    //rest of the labels going here (I only see Name and Email)
}

...

您也可以考虑将您的调用设置为在sp!= null条件内可见的标签,这样只有当有效人员显示时才会显示这些标签。否则就把它们放在它面前。

否则我会像Dimitri那样做并说

lblName.Text = student.UserName != null ? student.UserName : String.Empty;
lblEmail.Text = student.UserEmail != null ? student.UserEmail : String.Empty;
// etc.

如果可以使用nullables,实际上可以分配student对象,但属性可能不会在运行时导致应用程序出现问题。

答案 3 :(得分:0)

确定。我做了一个新方法:

public UserDetails GetUser(int userID)
{
    SqlConnection conn = new SqlConnection(Config.DbConnectionString);
    SqlCommand cmd = new SqlCommand("GetFullUserProfile", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int, 4));
    cmd.Parameters["@UserID"].Value = userID;
    try
    {
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
        // Check if the query returned a record.
        if (!reader.HasRows) return null;
        // Get the first row.
        reader.Read();
        UserDetails emp = new UserDetails((int)reader["UserID"], (string)reader["UserName"], (string)reader["UserFamilyName"], (DateTime)reader["UserBirthDate"], (string)reader["UserTelephone"], (string)reader["UserEmail"], (DateTime)reader["RegDate"], (string)reader["UserComment"], (int)reader["UserActive"]);
        reader.Close();
        return emp;
    }
    catch (SqlException err)
    {
        throw new ApplicationException("Data error.");
    }
    finally
    {
        conn.Close();
    }
}

新课程:

using System;

public class UserDetails
{
    public UserDetails(int userID, string userName, string userFamilyName, DateTime userBirthDate, string userTelephone, string userEmail, DateTime regDate, string userComment, int userActive)
    {
        UserID = userID;
        UserName = userName;
        UserFamilyName = userFamilyName;
        UserBirthDay = userBirthDate;
        UserTelephone = userTelephone;
        UserEmail = userEmail;
        RegDate = regDate;
        UserComment = userComment;
        userActive = UserActive;
    }

    public int UserID
    { get; set; }

    public string UserName
    { get; set; }

    public string UserFamilyName
    { get; set; }

    public DateTime UserBirthDay
    { get; set; }

    public string UserTelephone
    { get; set; }

    public string UserEmail
    { get; set; }

    public DateTime RegDate
    { get; set; }

    public string UserComment
    { get; set; }

    public int UserActive
    { get; set; }
}

之后在我的WebUserControl的后台代码中:

UserDetails ud = ca.GetUser(24);
    //HiddenID.Value = String.Format(ud.UserID);
    lblName.Text = ud.UserName;
    lblEmail.Text = ud.UserEmail;
    lblFamilyName.Text = ud.UserFamilyName;
    lblBirth.Text=String.Format("{0:dd/MM/yyy}",ud.UserBirthDay);
    lblTelephone.Text = ud.UserTelephone;

让方法奏效了。所以,这个阶段已经准备就绪。感谢所有帮助过我的人。线程已关闭。