开发者

Problem when trying to call Form1`s methods from Form2

开发者 https://www.devze.com 2023-02-19 00:05 出处:网络
I\'m using WinForms in C# I have these methods in Inventory, I use these on Inventory_load event : ConstructTable();GetProducts(\"\");

I'm using WinForms in C#

I have these methods in Inventory, I use these on Inventory_load event :

 ConstructTable();  GetProducts("");

And works fine but when im trying to use this methods from Form2 nothing happens when I call like this :

 Inventory i = new Inventory();  i.ConstructTable();

 i.GetProducts(txtFind.Text.ToString());

These are the methods :

public void ConstructTable()
        {
            ProductTable = new DataTable();

            ProductTable.Columns.Add(new DataColumn("ID", typeof(int)));
            ProductTable.Columns.Add(new DataColumn("BarCode", typeof(string)));
            ProductTable.Colum开发者_JAVA技巧ns.Add(new DataColumn("ArtNumber", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("ProductName", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("Price", typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("SelfPrice", typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("PriceWithOutAWD",typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("TotalSelfPrice",typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("UnitsInStock", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("Comment", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("InputDateTime", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("InputQuantity", typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("Margin", typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("CategoryName", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("TypeName", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("ExpDate", typeof(string)));

            ProductTable.TableName = "Products";
            ProductDataSet = new DataSet();
            ProductDataSet.Tables.Add(ProductTable);

            dgvInventory.DataSource = ProductDataSet;
            dgvInventory.DataMember = "Products";
        }

        public void GetProducts(string find)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand("SELECT ID, BarCode, ArtNumber,ProductName, Price, SelfPrice, PriceWithOutAWD, TotalSelfPrice, UnitsInStock, " +
                                                    " Comment, InputDateTime, InputQuantity, Margin, CategoryName, TypeName, ExpDate FROM GetProducts"+
                                                    " WHERE BarCode LIKE @F OR ArtNumber LIKE @F OR ProductName LIKE @F OR Price LIKE @F OR Comment LIKE @F", 
                                                    new SqlConnection(Program.ConnectionString)))
                {
                    cmd.Parameters.AddWithValue("@F", "%" + find + "%");
                    cmd.Connection.Open();

                    SqlDataReader myReader = cmd.ExecuteReader();
                    while (myReader.Read())
                    {

                        ProductTable.Rows.Add
                            (
                            (int)myReader["ID"],
                            myReader["BarCode"].ToString(),
                            myReader["ArtNumber"].ToString(),
                            myReader["ProductName"].ToString(),
                            (decimal)myReader["Price"],
                            (decimal)myReader["SelfPrice"],
                            (decimal)myReader["PriceWithOutAWD"],
                            myReader["TotalSelfPrice"].ToString(),
                            myReader["UnitsInStock"].ToString(),
                            myReader["Comment"].ToString(),
                            myReader["InputDateTime"].ToString(),
                            myReader["InputQuantity"].ToString(),
                            myReader["Margin"].ToString(),
                            myReader["CategoryName"].ToString(),
                            myReader["TypeName"].ToString(),
                            myReader["ExpDate"].ToString()
                            );
                    }
                    cmd.Connection.Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Program.MsgError1, "Acid", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }


You should declare your methods static :

public static void ConstructTable()

and use them like :

Inventory.ConstructTable()

in Form2.


I cannot seem to post a comment, so I will update my answer, you need to explain what exactly is not working.

For example are you 100% sure that your code is correct? You have a similar question you have asked in the past about this code.

"I asked and get answer my code needed one space. I added it and its work fine on Inventory form but when i trying to use it from other form its doing nothing. thanks"

Your methods are not static currently that is a major design flaw. Of course the design itself is flawed you might want to do research in the correct way to handle the data you are requesting.

I can't tell what your second form ( called Inventory ) is suppose to do. Why are you not create the table before the form is created, and using the query as the source for the table, would be a much better design.

Your current method is extremely flawed and you clearly are having problems I suggest getting some co-worker support.


It makes no architectural or design sense to have the ConstructTable() and GetProducts() methods with their current implementation as the codebehind on a winform.

There is little or no cohesion between these methods and the visual representation of the data in your form, which is clearly illustrated by the fact that you end up calling code in one form from another.

Move this code into a class library and consider using Linq or some less archaic data access logic to retrieve your data.

With respect to the specific problem that you are experiencing I would recommend that you attach a debugger and trace through these lines of code. Pay attention to the value of the parameter being sent to GetProducts()!

0

精彩评论

暂无评论...
验证码 换一张
取 消