bernie2024
bernie2024
CC#
Created by bernie2024 on 2/27/2023 in #help
❔ ✅ How to call a specific attribute from a getter class to another class
Hey there, I am struggling to call this attribute from another class. The two relevant code pieces are:
public List<CompletedAssignment> GetCompletedAssignments(int studentID, int assignmentID)
{
List<CompletedAssignment> completedAssignments = new List<CompletedAssignment>();

using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT StudentID, AssignmentID, EarnedPoints FROM CompletedAssignments WHERE StudentID = @StudentID AND AssignmentID = @AssignmentID";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@StudentID", studentID);
command.Parameters.AddWithValue("@AssignmentID", assignmentID);

connection.Open();

using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
CompletedAssignment completedAssignment = new CompletedAssignment();

completedAssignment.StudentID = reader.GetInt32(0);
completedAssignment.AssignmentID = reader.GetInt32(1);
completedAssignment.EarnedPoints = reader.GetInt32(2);

completedAssignments.Add(completedAssignment);
}
}
}

return completedAssignments;
}
public List<CompletedAssignment> GetCompletedAssignments(int studentID, int assignmentID)
{
List<CompletedAssignment> completedAssignments = new List<CompletedAssignment>();

using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT StudentID, AssignmentID, EarnedPoints FROM CompletedAssignments WHERE StudentID = @StudentID AND AssignmentID = @AssignmentID";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@StudentID", studentID);
command.Parameters.AddWithValue("@AssignmentID", assignmentID);

connection.Open();

using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
CompletedAssignment completedAssignment = new CompletedAssignment();

completedAssignment.StudentID = reader.GetInt32(0);
completedAssignment.AssignmentID = reader.GetInt32(1);
completedAssignment.EarnedPoints = reader.GetInt32(2);

completedAssignments.Add(completedAssignment);
}
}
}

return completedAssignments;
}
and
28 replies
CC#
Created by bernie2024 on 2/27/2023 in #help
❔ ✅ Cant select an attribute of an object for display in a combobox or textbox
6 replies
CC#
Created by bernie2024 on 1/27/2023 in #help
✅ Strange error when trying to retrun a class type variable through a public method
Hi there, I am new to C# and while working on this method:
namespace InventoryMaintenance
{
public partial class frmNewInventoryItem : Form
{
InventoryItem invItem = null;

public frmNewInventoryItem()
{
InitializeComponent();
}

public InventoryItem GetNewItem()
{
this.ShowDialog();

return invItem;
}
private void BtnSave_Click(object sender, EventArgs e)
{

}

private void BtnCancel_Click(object sender, EventArgs e)
{

}
}
}
namespace InventoryMaintenance
{
public partial class frmNewInventoryItem : Form
{
InventoryItem invItem = null;

public frmNewInventoryItem()
{
InitializeComponent();
}

public InventoryItem GetNewItem()
{
this.ShowDialog();

return invItem;
}
private void BtnSave_Click(object sender, EventArgs e)
{

}

private void BtnCancel_Click(object sender, EventArgs e)
{

}
}
}
The method public InventoryItem GetNewItem(){} is displaying a build error which says "Inconsistent accessibility: return type 'inventoryitem' is less accessible than method 'GetNewItem()" I dont understand what it means, the class must be public for later use. Am I doing this wrong?
8 replies
CC#
Created by bernie2024 on 1/27/2023 in #help
✅ Cant convert a list to list of a class type?
I am new to C# and am stuck on this strange issue, I cant seem to send a list to a different class, as its only accepting a list of type "Customer" (class name), how would I convert or send this information over? Method which collects info and sends it to save method
public partial class frmCustomers : Form
{
List<Customer> customers = new List<Customer>();

private void btnAdd_Click(object sender, EventArgs e)
{
frmAddCustomer addCustomerForm = new frmAddCustomer();
Customer customer = addCustomerForm.GetNewCustomer();
if (customer != null)
{
CustomerDB.SaveCustomers(customer); /// This is the line giving me an error
MessageBox.Show(customer.GetDisplayText().ToString(), "BTNAddClick2");
CustomerListBox();

}
}
public partial class frmCustomers : Form
{
List<Customer> customers = new List<Customer>();

private void btnAdd_Click(object sender, EventArgs e)
{
frmAddCustomer addCustomerForm = new frmAddCustomer();
Customer customer = addCustomerForm.GetNewCustomer();
if (customer != null)
{
CustomerDB.SaveCustomers(customer); /// This is the line giving me an error
MessageBox.Show(customer.GetDisplayText().ToString(), "BTNAddClick2");
CustomerListBox();

}
}
Save Method: will be posted as a comment so I dont run out of available words.
12 replies
CC#
Created by bernie2024 on 1/27/2023 in #help
❔ ✅ Struggling with function showing error, "Object reference not set to an instance of an object"
have been trying to solve this issue, not sure what the problem is. public static class CustomerDB { private const string dir = @"C:\C#\Files"; private const string path = dir + "Customers.txt"; public static void SaveCustomers(List<Customer> customers) { // create the output stream for a text file that exists StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); // write each customer foreach (Customer customer in customers) { textOut.Write(customer.FirstName + "|"); textOut.Write(customer.LastName + "|"); textOut.WriteLine(customer.Email); } // write the end of the document textOut.Close(); } I get the error "Exception Thrown System.NullReferenceException: 'Object reference not set to an instance of an object. customers was null." If any additional reference code is needed for context I can provide it, the point of this is to add a "Customer" to a database of customers What am I doing wrong here? it only has the issue once reaching the 'Foreach (Customer customer in customers)' statement.
63 replies