473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

nested statement firing more than desired...

Preface: I'm building an intranet site to build custom queries against
our SQL Server (2000) db;
The page is developed in ASP.net (2.0) with C# Codebehind.

I dynamically generate and populate checkboxlists for each table that
the user selects to query.
I disable selecting non-related tables, and am in the process of
creating inner joins when tables are related.... This is where I am
getting stuck, here is the logic which builds the join statements...

selectedTables is an array of (strings) which are the table names the
user wants to query.
So I query the database for each one of those, and get related tables,
and make sure those checkboxes are enabled, the rest get disabled.

protected void enabledRelated2()
{
string connStr =
ConfigurationManager.ConnectionStrings["myConnStr"].ToString();

using (SqlConnection myCon = new SqlConnection(connStr))
{
myCon.Open();
foreach (string selTables in selectedTables)
{
using (SqlCommand command = new SqlCommand("SELECT
u.column_name as PrimaryColumn,tc.TABLE_NAME AS
PrimaryKeyTable,u2.column_name as ForeignColumn,
COALESCE(tc2.TABLE_NAME,'N/A') AS ForeignKeyTable FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc join
INFORMATION_SCHEMA.KEY_COLUMN_USAGE u on tc.CONSTRAINT_NAME
=u.CONSTRAINT_NAME JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1
ON tc.CONSTRAINT_NAME =rc1.UNIQUE_CONSTRAINT_NAME JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME
=rc1.CONSTRAINT_NAME join INFORMATION_SCHEMA.KEY_COLUMN_USAGE u2 on
tc2.CONSTRAINT_NAME =u2.CONSTRAINT_NAME WHERE TC.CONSTRAINT_TYPE
='PRIMARY KEY' AND TC2.CONSTRAINT_TYPE ='FOREIGN KEY' AND
(tc.table_name = '" + selTables + "'" + " OR tc2.TABLE_NAME = '" +
selTables + "')", myCon))
{
using (SqlDataReader reader =
command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i <
CheckBoxListTables.Items.Count; i++)
{
// If an item is the primarykeytable or
foreignkeytable to a selected table
if (CheckBoxListTables.Items[i].Text ==
reader[1].ToString() || CheckBoxListTables.Items[i].Text ==
reader[3].ToString())
{
// For each selected checkbox,
enable the checkboxes for related tables
CheckBoxListTables.Items[i].Enabled
= true;
}
//If a table is selected and that table
is the primarykeytable
if
(CheckBoxListTables.Items[i].Selected &&
CheckBoxListTables.Items[i].Text == reader[1].ToString())
{
for (int x = 0; x <
CheckBoxListTables.Items.Count; x++)
{
// For each table selected,
cycle through and check for related tables, if those tables are
selected, build the JOIN statement
if
(CheckBoxListTables.Items[x].Selected && x != i &&
CheckBoxListTables.Items[x].Text == reader[3].ToString())
{
// Here is the part that fires more than I want it to, this occurs
twice for every relation. So if i select two tables, it generates two
inner join statements which are identical. If i select 3 related
tables, it creates 4 joins, 2 sets of identical ones

queryJoins += " INNER JOIN
" + reader[1].ToString() + " ON " + reader[3].ToString() + "." +
reader[2].ToString() + "=" + reader[1].ToString() + "." +
reader[0].ToString();
}
}
}
}
}
}
}
}
myCon.Close();
}
}

I understand that I'll be very lucky if anyone sees the issue from this
segment of code, without much more knowledge about the project than
I've included, so please feel free to ask questions, I can even supply
a screen shot of the interface etc...

Apr 17 '06 #1
1 1431
fixed it.

Apr 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Joshua Beall | last post by:
Hi All, I remember reading that both nested classes and namespaces would be available in PHP5. I know that namespaces got canceled (much sadness...), however, I *thought* that nested classes...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
25
by: GY2 | last post by:
I writing some documentation and I want to describe a common code structure which is used to step through all the items in a collection (e.g. each file in a subdirectory) while applying more and...
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
2
by: Johannes Bauer | last post by:
Nick Keighley schrieb: Why is there actually a *need* for nested functions? If functionality of subfunctions which are only locally visible is desired, why not put the nesting function parent...
3
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.