HighTechTalks DotNet Forums  

Part-2: Code works in form constructor but fails to relocate elsewhere

Dotnet Framework (WinForms DataBinding) microsoft.public.dotnet.framework.windowsforms.databinding


Discuss Part-2: Code works in form constructor but fails to relocate elsewhere in the Dotnet Framework (WinForms DataBinding) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Mansoor A. Karim
 
Posts: n/a

Default Part-2: Code works in form constructor but fails to relocate elsewhere - 07-10-2008 , 01:42 AM






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;
}
}




Reply With Quote
  #2  
Old   
Morten Wennevik [C# MVP]
 
Posts: n/a

Default RE: Part-2: Code works in form constructor but fails to relocate elsew - 07-10-2008 , 02:23 AM






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:

Quote:
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;
}
}





Reply With Quote
  #3  
Old   
Morten Wennevik [C# MVP]
 
Posts: n/a

Default RE: Part-2: Code works in form constructor but fails to relocate elsew - 07-10-2008 , 02:23 AM



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:

Quote:
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;
}
}





Reply With Quote
  #4  
Old   
Mansoor A. Karim
 
Posts: n/a

Default Re: Part-2: Code works in form constructor but fails to relocate elsew - 07-11-2008 , 03:17 AM



Thanks again Morten.

I now realize the very obvious inefficiency in my logic. Having ported all
string manipulations to StringBuilder and consequent one time textBox1.Text
(again a String data type) property assignment, performance is now
consistent throughout for all code relocations. I had repeatedly come across
cautions in the msdn against inefficiency of repetitive String assignments
but then reading is no substitute to practical experience.

I am just upgrading from VB6 to C#/.Net and am grappling with both grammar
and symantics, the latter being more challenging for me.


"Morten Wennevik [C# MVP]" <MortenWennevik (AT) hotmail (DOT) com> wrote

Quote:
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;
}
}







Reply With Quote
  #5  
Old   
Mansoor A. Karim
 
Posts: n/a

Default Re: Part-2: Code works in form constructor but fails to relocate elsew - 07-11-2008 , 03:17 AM



Thanks again Morten.

I now realize the very obvious inefficiency in my logic. Having ported all
string manipulations to StringBuilder and consequent one time textBox1.Text
(again a String data type) property assignment, performance is now
consistent throughout for all code relocations. I had repeatedly come across
cautions in the msdn against inefficiency of repetitive String assignments
but then reading is no substitute to practical experience.

I am just upgrading from VB6 to C#/.Net and am grappling with both grammar
and symantics, the latter being more challenging for me.


"Morten Wennevik [C# MVP]" <MortenWennevik (AT) hotmail (DOT) com> wrote

Quote:
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;
}
}







Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.