Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Referencing Another Table (Relationships) in ASP.NET(C#)
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00733557
Message ID:
00733719
Views:
18
Hi Tarran,

I would recommend using one select statement that pulls the information from both SQL Server tables and creates on ADP.NET DataTable. I would also recommend using the DataReader if you are only going to display the data on the page. You will get better performance by doing that verses using a DataSet. Here is some sample code to get you started:
string connStr = "your connection string";
SqlConnection  cnn = new SqlConnection(connStr);
string  strSQL = "SELECT Articles.*, Categories.CatName FROM Articles " +
	"LEFT OUTER JOIN Categories ON Articles.ArticleCatID = Categories.CatID";
SqlCommand  cmd = new SqlCommand(strSQL, cnn);
SqlDataReader dr;

cnn.Open();
dr = cmd.ExecuteReader(	CommandBehavior.CloseConnection);

while ( dr.Read())
{
	// Bind to your controls on the page like an example below
	lstDemo.Items.Add(string .Format("{0}: {1}, {2}",dr["Field1"], dr["Field2"], dr["CatName"]));
}

dr.Close();
>Hi All,
>
>I have two tables (Categories, Articles) in my database.
>
>When I display my Article Search Results (Thanks to Paul and Cathi) From the Table Articles I have a Field Call Articles.ArticleCatID which holds the same number as the PrimaryKey Field Categories.CatID from the Categories Table.
>
>Instead of showing the number "2" from Articles.ArticleCatID I need to access Categories.CatName and display this on the page.
>
>I just quickly wrote this ASP(VB) so you have a Idea what I mean.
>
>Very Ugly and will eventually lead to the death of the database(Not as pretty as the death of a star but almost as fatal)
>
>Query = "SELECT * FROM Articles"
>Execute Here(objRS)
>
>SQL = "SELECT * FROM Articles, Categories WHERE Articles.ArticleCatID = Categories.CatID AND Categories.CatID = '" & objRS("ArticleCatID") & "'"
>Execute Here(objRec)
>
>And have this looping through each printed record.
>
>So hopefully you see my problem.
>
>Any ASP.NET(C#) Guru, you r help will be greatly appreciated.
>
>Kind Regards,
>Tarran Walker
>tarran.walker@milkit.net
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Reply
Map
View

Click here to load this message in the networking platform