![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Not that I am obssessed with running code in the constructor but I am unable to make this following piece of simple code run anywhere other than the form constructor. While this code completes (for total 91 customer records in Customers table) in about 5 secs in the form constructor, it takes infinitely long (more than 20 mins or sometimes runs endlessly) when relocated in forms load event or button click event. No error is thrown, the process just hogs the system processor and runs infinitely. The performance difference is too great to be ascribed to any inherent constructor code optimization. This code creates, fills and relates NorthWindTraders tables Customers, Orders, OrdersDetails, Products and cycles through related records for each customer appending selected fields from each related record to a textBox1. Please crack this problem for me. Thanks Mansoor DataSet dataSet; DataRelation relation; SqlConnection objConnection; SqlDataAdapter objDataAdapter; //Create and open connection objConnection = new SqlConnection(ConnectionString); objConnection.Open(); //Create dataset dataSet = new DataSet("NorthWindDatabase"); //Create and fill Customers, Orders, OrdersDetails and Products table objDataAdapter = new SqlDataAdapter("SELECT * FROM Customers", objConnection); objDataAdapter.Fill(dataSet, "Customers"); objDataAdapter = new SqlDataAdapter("SELECT * FROM Orders", objConnection); objDataAdapter.Fill(dataSet, "Orders"); objDataAdapter = new SqlDataAdapter("SELECT * FROM [Order Details]", objConnection); objDataAdapter.Fill(dataSet, "Order_Details"); objDataAdapter = new SqlDataAdapter("SELECT * FROM Products", objConnection); objDataAdapter.Fill(dataSet, "Products"); //Close connection objConnection.Close(); //Create constraints and relations between tables relation = new DataRelation("CustomerOrders", dataSet.Tables["Customers"].Columns["CustomerID"], dataSet.Tables["Orders"].Columns["CustomerID"]); dataSet.Relations.Add(relation); relation = new DataRelation("OrderDetails", dataSet.Tables["Orders"].Columns["OrderID"], dataSet.Tables["Order_Details"].Columns["OrderID"]); dataSet.Relations.Add(relation); relation = new DataRelation("ProductDetails", dataSet.Tables["Products"].Columns["ProductID"], dataSet.Tables["Order_Details"].Columns["ProductID"]); dataSet.Relations.Add(relation); //Proceed to display records in the text box textBox1.Text = string.Empty; int CustomerCount = 0; foreach (DataRow customerRow in dataSet.Tables["Customers"].Rows) { textBox1.Text += ++CustomerCount + ") Customer: " + (string)customerRow["CompanyName"] + Environment.NewLine; foreach (DataRow orderRow in customerRow.GetChildRows("CustomerOrders")) { textBox1.Text += "\tOrderDate: " + (DateTime)orderRow["OrderDate"] + Environment.NewLine; foreach (DataRow detailRow in orderRow.GetChildRows("OrderDetails")) { textBox1.Text += "\t\tProduct: " + (string)detailRow.GetParentRow("ProductDetails")["ProductName"] + Environment.NewLine; textBox1.Text += "\t\tQuantity: " + (short)detailRow["Quantity"] + Environment.NewLine; } } //Add blank line after every customer record textBox1.Text += Environment.NewLine; } } |
#3
| |||
| |||
|
|
Not that I am obssessed with running code in the constructor but I am unable to make this following piece of simple code run anywhere other than the form constructor. While this code completes (for total 91 customer records in Customers table) in about 5 secs in the form constructor, it takes infinitely long (more than 20 mins or sometimes runs endlessly) when relocated in forms load event or button click event. No error is thrown, the process just hogs the system processor and runs infinitely. The performance difference is too great to be ascribed to any inherent constructor code optimization. This code creates, fills and relates NorthWindTraders tables Customers, Orders, OrdersDetails, Products and cycles through related records for each customer appending selected fields from each related record to a textBox1. Please crack this problem for me. Thanks Mansoor DataSet dataSet; DataRelation relation; SqlConnection objConnection; SqlDataAdapter objDataAdapter; //Create and open connection objConnection = new SqlConnection(ConnectionString); objConnection.Open(); //Create dataset dataSet = new DataSet("NorthWindDatabase"); //Create and fill Customers, Orders, OrdersDetails and Products table objDataAdapter = new SqlDataAdapter("SELECT * FROM Customers", objConnection); objDataAdapter.Fill(dataSet, "Customers"); objDataAdapter = new SqlDataAdapter("SELECT * FROM Orders", objConnection); objDataAdapter.Fill(dataSet, "Orders"); objDataAdapter = new SqlDataAdapter("SELECT * FROM [Order Details]", objConnection); objDataAdapter.Fill(dataSet, "Order_Details"); objDataAdapter = new SqlDataAdapter("SELECT * FROM Products", objConnection); objDataAdapter.Fill(dataSet, "Products"); //Close connection objConnection.Close(); //Create constraints and relations between tables relation = new DataRelation("CustomerOrders", dataSet.Tables["Customers"].Columns["CustomerID"], dataSet.Tables["Orders"].Columns["CustomerID"]); dataSet.Relations.Add(relation); relation = new DataRelation("OrderDetails", dataSet.Tables["Orders"].Columns["OrderID"], dataSet.Tables["Order_Details"].Columns["OrderID"]); dataSet.Relations.Add(relation); relation = new DataRelation("ProductDetails", dataSet.Tables["Products"].Columns["ProductID"], dataSet.Tables["Order_Details"].Columns["ProductID"]); dataSet.Relations.Add(relation); //Proceed to display records in the text box textBox1.Text = string.Empty; int CustomerCount = 0; foreach (DataRow customerRow in dataSet.Tables["Customers"].Rows) { textBox1.Text += ++CustomerCount + ") Customer: " + (string)customerRow["CompanyName"] + Environment.NewLine; foreach (DataRow orderRow in customerRow.GetChildRows("CustomerOrders")) { textBox1.Text += "\tOrderDate: " + (DateTime)orderRow["OrderDate"] + Environment.NewLine; foreach (DataRow detailRow in orderRow.GetChildRows("OrderDetails")) { textBox1.Text += "\t\tProduct: " + (string)detailRow.GetParentRow("ProductDetails")["ProductName"] + Environment.NewLine; textBox1.Text += "\t\tQuantity: " + (short)detailRow["Quantity"] + Environment.NewLine; } } //Add blank line after every customer record textBox1.Text += Environment.NewLine; } } |
#4
| |||
| |||
|
|
Hi Mansoor, When you set the Text property of a TextBox, a whole lot of stuff happens, and probably more than a thousand lines of code is running. When you run this in the constructor much of the code isn't running simply because the TextBox isn't fully created at that time. Furthermore, when doing heavy looping and string manipulation you should use a StringBuilder. Change all textBox.Text += ... + Environment.NewLine to something like StringBuilder sb = new StringBuilder() foreach() { sb.AppendLine(...); } textBox1.Text = sb.ToString(); This would cause the heavy TextBox.Text code to run only once. -- Happy Coding! Morten Wennevik [C# MVP] "Mansoor A. Karim" wrote: Not that I am obssessed with running code in the constructor but I am unable to make this following piece of simple code run anywhere other than the form constructor. While this code completes (for total 91 customer records in Customers table) in about 5 secs in the form constructor, it takes infinitely long (more than 20 mins or sometimes runs endlessly) when relocated in forms load event or button click event. No error is thrown, the process just hogs the system processor and runs infinitely. The performance difference is too great to be ascribed to any inherent constructor code optimization. This code creates, fills and relates NorthWindTraders tables Customers, Orders, OrdersDetails, Products and cycles through related records for each customer appending selected fields from each related record to a textBox1. Please crack this problem for me. Thanks Mansoor DataSet dataSet; DataRelation relation; SqlConnection objConnection; SqlDataAdapter objDataAdapter; //Create and open connection objConnection = new SqlConnection(ConnectionString); objConnection.Open(); //Create dataset dataSet = new DataSet("NorthWindDatabase"); //Create and fill Customers, Orders, OrdersDetails and Products table objDataAdapter = new SqlDataAdapter("SELECT * FROM Customers", objConnection); objDataAdapter.Fill(dataSet, "Customers"); objDataAdapter = new SqlDataAdapter("SELECT * FROM Orders", objConnection); objDataAdapter.Fill(dataSet, "Orders"); objDataAdapter = new SqlDataAdapter("SELECT * FROM [Order Details]", objConnection); objDataAdapter.Fill(dataSet, "Order_Details"); objDataAdapter = new SqlDataAdapter("SELECT * FROM Products", objConnection); objDataAdapter.Fill(dataSet, "Products"); //Close connection objConnection.Close(); //Create constraints and relations between tables relation = new DataRelation("CustomerOrders", dataSet.Tables["Customers"].Columns["CustomerID"], dataSet.Tables["Orders"].Columns["CustomerID"]); dataSet.Relations.Add(relation); relation = new DataRelation("OrderDetails", dataSet.Tables["Orders"].Columns["OrderID"], dataSet.Tables["Order_Details"].Columns["OrderID"]); dataSet.Relations.Add(relation); relation = new DataRelation("ProductDetails", dataSet.Tables["Products"].Columns["ProductID"], dataSet.Tables["Order_Details"].Columns["ProductID"]); dataSet.Relations.Add(relation); //Proceed to display records in the text box textBox1.Text = string.Empty; int CustomerCount = 0; foreach (DataRow customerRow in dataSet.Tables["Customers"].Rows) { textBox1.Text += ++CustomerCount + ") Customer: " + (string)customerRow["CompanyName"] + Environment.NewLine; foreach (DataRow orderRow in customerRow.GetChildRows("CustomerOrders")) { textBox1.Text += "\tOrderDate: " + (DateTime)orderRow["OrderDate"] + Environment.NewLine; foreach (DataRow detailRow in orderRow.GetChildRows("OrderDetails")) { textBox1.Text += "\t\tProduct: " + (string)detailRow.GetParentRow("ProductDetails")["ProductName"] + Environment.NewLine; textBox1.Text += "\t\tQuantity: " + (short)detailRow["Quantity"] + Environment.NewLine; } } //Add blank line after every customer record textBox1.Text += Environment.NewLine; } } |
#5
| |||
| |||
|
|
Hi Mansoor, When you set the Text property of a TextBox, a whole lot of stuff happens, and probably more than a thousand lines of code is running. When you run this in the constructor much of the code isn't running simply because the TextBox isn't fully created at that time. Furthermore, when doing heavy looping and string manipulation you should use a StringBuilder. Change all textBox.Text += ... + Environment.NewLine to something like StringBuilder sb = new StringBuilder() foreach() { sb.AppendLine(...); } textBox1.Text = sb.ToString(); This would cause the heavy TextBox.Text code to run only once. -- Happy Coding! Morten Wennevik [C# MVP] "Mansoor A. Karim" wrote: Not that I am obssessed with running code in the constructor but I am unable to make this following piece of simple code run anywhere other than the form constructor. While this code completes (for total 91 customer records in Customers table) in about 5 secs in the form constructor, it takes infinitely long (more than 20 mins or sometimes runs endlessly) when relocated in forms load event or button click event. No error is thrown, the process just hogs the system processor and runs infinitely. The performance difference is too great to be ascribed to any inherent constructor code optimization. This code creates, fills and relates NorthWindTraders tables Customers, Orders, OrdersDetails, Products and cycles through related records for each customer appending selected fields from each related record to a textBox1. Please crack this problem for me. Thanks Mansoor DataSet dataSet; DataRelation relation; SqlConnection objConnection; SqlDataAdapter objDataAdapter; //Create and open connection objConnection = new SqlConnection(ConnectionString); objConnection.Open(); //Create dataset dataSet = new DataSet("NorthWindDatabase"); //Create and fill Customers, Orders, OrdersDetails and Products table objDataAdapter = new SqlDataAdapter("SELECT * FROM Customers", objConnection); objDataAdapter.Fill(dataSet, "Customers"); objDataAdapter = new SqlDataAdapter("SELECT * FROM Orders", objConnection); objDataAdapter.Fill(dataSet, "Orders"); objDataAdapter = new SqlDataAdapter("SELECT * FROM [Order Details]", objConnection); objDataAdapter.Fill(dataSet, "Order_Details"); objDataAdapter = new SqlDataAdapter("SELECT * FROM Products", objConnection); objDataAdapter.Fill(dataSet, "Products"); //Close connection objConnection.Close(); //Create constraints and relations between tables relation = new DataRelation("CustomerOrders", dataSet.Tables["Customers"].Columns["CustomerID"], dataSet.Tables["Orders"].Columns["CustomerID"]); dataSet.Relations.Add(relation); relation = new DataRelation("OrderDetails", dataSet.Tables["Orders"].Columns["OrderID"], dataSet.Tables["Order_Details"].Columns["OrderID"]); dataSet.Relations.Add(relation); relation = new DataRelation("ProductDetails", dataSet.Tables["Products"].Columns["ProductID"], dataSet.Tables["Order_Details"].Columns["ProductID"]); dataSet.Relations.Add(relation); //Proceed to display records in the text box textBox1.Text = string.Empty; int CustomerCount = 0; foreach (DataRow customerRow in dataSet.Tables["Customers"].Rows) { textBox1.Text += ++CustomerCount + ") Customer: " + (string)customerRow["CompanyName"] + Environment.NewLine; foreach (DataRow orderRow in customerRow.GetChildRows("CustomerOrders")) { textBox1.Text += "\tOrderDate: " + (DateTime)orderRow["OrderDate"] + Environment.NewLine; foreach (DataRow detailRow in orderRow.GetChildRows("OrderDetails")) { textBox1.Text += "\t\tProduct: " + (string)detailRow.GetParentRow("ProductDetails")["ProductName"] + Environment.NewLine; textBox1.Text += "\t\tQuantity: " + (short)detailRow["Quantity"] + Environment.NewLine; } } //Add blank line after every customer record textBox1.Text += Environment.NewLine; } } |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |