0

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.

1
  • Wrap SqlConnection and SqlDataReader in using statements. Commented Sep 19, 2020 at 20:06

2 Answers 2

2

It means that the constructor of Product need four arguments and you provide none.

Here's the signature of your constructor :

 // Constructor
    public Product(string productname, int productstock, int productcategoryid, int productemployeeid)

Change the line

products.Add(new Product() { ProductName = productname, ProductStock = productstock, 
                ProductCategoryID = productcategoryid, ProductEmployeeID = productemployeeid });

by this and you'll be fine

products.Add(new Product( productname, productstock, productcategoryid, productemployeeid));
Sign up to request clarification or add additional context in comments.

4 Comments

I get an error from "String productname = reader.GetString(0); Thats says : System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.String'.' But i dont understand this? its not an Int.. and as far as i can see, i am calling it as a string, not as an int?
it's bad practice to use select * , you should always put column name in your query. In your case, we don't know which field is first. Please, change your query to "select productname, productstock, productcategoryid, productemployeeid from table ..."
Thank you, i did not know that. but i see, its because if you do changes in DB later on, the method might fail because i take *. This works, i am getting output.. altho i am getting the output : "Lagersystem.DB.Product Lagersystem.DB.Product Lagersystem.DB.Product Lagersystem.DB.Product Lagersystem.DB.Product Lagersystem.DB.Product" instead of the actual content in the db.
if you feel i answered the initial question, please mark it as answered. For the latter question, this is "Console.WriteLine(product);" you need to change in order to print the correct value. (ie: console.write (product.productname + " " + product. .... );
-1
  1. When you do a select * from products, does the query return any result ?

You also need to start from 0, hence try to put reader.GetString(0)

1 Comment

Yes when i do a query with SELECT * FROM Products in db, it returns all my products :-) thank you, i changed it to start from 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.