I am creating this simple video game ordering application. I have create an userControl named productControl. Code:
public partial class productControl : UserControl
{
private string pName;
private float pPrice;
private string pDesc;
private string pImgUrl;
public productControl() => InitializeComponent();
public string getName
{
get => pName;
set
{
pName = value;
productName.Text = pName;
}
}
public float getPrice
{
get => pPrice;
set
{
pPrice = value;
productPrice.Text = pPrice.ToString("c");
}
}
public string getDescription
{
get => pDesc;
set
{
pDesc = value;
descriptionText.Text = pDesc;
}
}
public string getImage
{
get => pImgUrl;
set
{
pImgUrl = value;
prodImage.Load(pImgUrl);
prodImage.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
public int getProdId { get; set; }
private void buyButton_Click(object sender, EventArgs e)
{
MessageBox.Show(pPrice.ToString("c"));
orderConfirmation o = new orderConfirmation();
o.Show();
}
}
And a Windows Forms named accountMain. Which retrieves data from SQL and calls the productControl to fill it up.
private string productRetriever = "SELECT prodId, prodName, prodPrice, prodImg, prodDesc FROM [dbo].[products]";
private void accountMain_Load(object sender, EventArgs e)
{
createFunctions();
}
private void createFunctions()
{
connection.Open();
SqlCommand retProduct = new SqlCommand(productRetriever, connection);
panel1.Controls.Clear()
using (var reader = retProduct.ExecuteReader())
{
int count = 0;
while(reader.Read())
{
p[count] = new productControl();
p[count].Name = count.ToString();
p[count].getProdId = (int)reader[0];
p[count].getName = (string)reader[1];
p[count].getPrice = (float)reader[2];
p[count].getImage = (string)reader[3];
p[count].getDescription = (string)reader[4];
panel1.Controls.Add(p[count]);
count++;
}
}
connection.Close();
}
Above I am using a while loop to populate the panel1 with productControl inside of the accountMain Form. How can I get userControl to hide the accountForm and show another form (passing the values into the new form like title and price) when I click the "Buy" button which is inside the userControl.
Also I am new to implementing SQL into C# is it the correct way to populate the panel1 in accountMain using executeReader() or is there a better approach of retrieving data from SQL and showing it in userControl?