I have an issue that when accessing and displaying variables from my class named Members
into labels upon form load the labels just display blank data.
My program uses a login form to do this and this is when it gets and sets the members details into the class. It uses the code below:
public class Member
{
public int MemberID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string CourseTitle { get; set; }
public string Password { get; set; }
}
private IList<Member> GetMembers()
{
OleDbConnection conn = null;
OleDbDataReader reader = null;
try
{
conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Rhys\Documents\Visual Studio 2013\Projects\AssignmentTrackerV2\AssignmentTrackerV2\bin\Debug\ATDatabase.accdb");
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM MemberDetails WHERE [Name] = @Name AND [Password] = @Password", conn);
cmd.Parameters.AddWithValue("@Name", txtFirstNameLogin.Text);
cmd.Parameters.AddWithValue("@Password", txtPasswordLogin.Text);
reader = cmd.ExecuteReader();
var members = new List<Member>();
while (reader.Read())
{
var member = new Member();
member.MemberID = reader.GetInt32(reader.GetOrdinal("MemberID"));
member.Name = reader["Name"].ToString();
member.Surname = reader["Surname"].ToString();
member.CourseTitle = reader["CourseTitle"].ToString();
members.Add(member);
Form2 frm2 = new Form2();
this.Hide();
frm2.ShowDialog();
}
return members;
}
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return null;
}
This basically just checks the users name and password entries against the database data and if it is successful then it sets the name, password, member ID and course title into the variables stored in the class.
However when I use this code under the form 2 load event
var member = new Member();
lblName.Text = member.Name;
lblSurname.Text = member.Surname;
lblCourseTitle.Text = member.CourseTitle;
All labels just appear blank. Any ideas?
Ignoring for a moment the questionable logic of storing plaintext passwords in your database…
It seems to me that you are failing to pass the retrieved member data to the Form2
instance you've created, and instead are initializing your form using an uninitialized instance of Member
.
Without a good, minimal , complete code example that reliably reproduces the problem, it's not possible to say for sure what the correct approach would be. But broadly, you should probably be passing the member
reference from GetMembers()
to the Form2
instance, eg to its constructor.
That might look something like this:
Form2 frm2 = new Form2(member);
where:
class Form2 : Form
{
private readonly Member _member;
public Form2(Member member)
{
InitializeComponent();
_member = member;
}
}
Then change your Load
event code to look like this:
lblName.Text = _member.Name;
lblSurname.Text = _member.Surname;
lblCourseTitle.Text = _member.CourseTitle;
Ie remove the local member
variable altogether, and instead use the class's _member
field.
Also as a suggestion, while checking your code, Form2
is inside the while-loop
(during data collection), what you can do is collect first all the data and put it inside the members
collection, then you can display each member
to the form by passing the whole collection to Form2
or if you want to pass each member you can do it after collecting all the data ( not during the collection ).
As a pseudocode:
Collect each Member
-> Add to Members Collection
Form2
to display each Member
by passing the Member
to it OR pass all the Members Collection
to Form2
then traverse to each member via Next
or Previous
button
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.