I am trying to show some content from my SQL Server database in my C# console app.
So far I have this class :
public SqlConnection connection()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "DESKTOP-UPVVOJP";
builder.InitialCatalog = "Lagersystem";
builder.IntegratedSecurity = true;
return new SqlConnection(builder.ToString());
}
Product model class :
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductStock { get; set; }
public int ProductCategoryID { get; set; }
public int ProductEmployeeID { get; set; }
public DateTime ProductCreatedDate { get; set; }
// Constructor
public Product(string productname, int productstock, int productcategoryid, int productemployeeid)
{
ProductName = productname;
ProductStock = productstock;
ProductCategoryID = productcategoryid;
ProductEmployeeID = productemployeeid;
ProductCreatedDate = DateTime.Now;
}
}
Display products method in program :
static void DisplayProducts()
{
List<Product> products = new List<Product>();
Database db = new Database();
SqlConnection conn = db.connection();
conn.Open();
using SqlCommand command = new SqlCommand(@"SELECT * FROM Products", conn);
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string productname = reader.GetString(1);
int productstock = reader.GetInt32(2);
int productcategoryid = reader.GetInt32(3);
int productemployeeid = reader.GetInt32(4);
products.Add(new Product() { ProductName = productname, ProductStock = productstock,
ProductCategoryID = productcategoryid, ProductEmployeeID = productemployeeid });
}
}
foreach (Product product in products)
{
Console.WriteLine(product);
}
}
and calling it from my main method :
static void Main(string[] args)
{
DisplayProducts();
}
In my DisplayProducts method, I get an error from doing this :
products.Add(new Product() { ProductName = productname, ProductStock = productstock,
ProductCategoryID = productcategoryid, ProductEmployeeID = productemployeeid });
The error is:
There is no argument given that corresponds to the required formal parameter 'productname' of 'Product.product(string, int, int, int)'
I really don't know what that means, been googling and searching but found nothing so far.
SqlConnectionandSqlDataReaderinusingstatements.