how to query 2 tables in asp.net

windows

Guest
wonder how i can query 2 diffrent tables with int <script > </script > in asp.net(aspx).(for example two select statments for 2 diffrent tables. I be happy if some one help me here. thanksYou can, just have two statements. If they are in the same db you do not even need a second connection. You just need to dbcommands with different names.Originally posted by PeOfEo
You can, just have two statements. If they are in the same db you do not even need a second connection. You just need to dbcommands with different names.

well yes i want to query diffrent tables with in same database.(for example 2 select statements and it prints the result.I be happy if u show me some code as i am new to asp. thanksfor access, sql server, mysql, what? Do you want to update, write, delete, read, what?Originally posted by PeOfEo
for access, sql server, mysql, what? Do you want to update, write, delete, read, what?

Thank u for your reply. i want it for access db and i want to do select for two diffrent tables with in same db.I be happy if u show me some code. ThanksOkay, here is some code I have

<%@ Page enableViewState="False" Language="VB" debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
'populate the blog
Dim blogconn as OleDbConnection
blogconn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& Server.MapPath("/news.mdb;"))
Dim blogfillcommand As oledbdataadapter
dim blogposts as new dataset
blogfillcommand = new oledbdataadapter ("Select title, postedby, posttime, comments, id, posts from tblposts order by id desc", blogconn)
blogfillcommand.fill(blogposts, "theposts")
newscol.datasource=blogposts.tables("theposts").defaultview
newscol.databind()
end sub
</script>


All I have to do is duplicated the variables and change the name and change the table.


<%@ Page enableViewState="False" Language="VB" debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
'populate the blog
Dim blogconn as OleDbConnection
blogconn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& Server.MapPath("/news.mdb;"))
Dim blogfillcommand As oledbdataadapter
dim blogposts as new dataset
blogfillcommand = new oledbdataadapter ("Select title, postedby, posttime, comments, id, posts from tblposts order by id desc", blogconn)
blogfillcommand.fill(blogposts, "theposts")
newscol.datasource=blogposts.tables("theposts").defaultview
newscol.databind()
'second select
Dim blogfillcommand2 As oledbdataadapter
dim blogposts2 as new dataset
blogfillcommand2 = new oledbdataadapter ("Select title, postedby, posttime, comments, id, posts from tblanothertable order by id desc", blogconn)
blogfillcommand2.fill(blogposts2, "theposts")
newscol2.datasource=blogposts2.tables("theposts").defaultview
newscol2.databind()
end sub
</script>

blogfillcommand2 is just another adapter, a more descriptive name might serve you better though.
blogposts2 would just be a second data set
newscol2 would just be a second repeater within the page.

As you can see I am just duplicating the sql statement but using another table, same connection.

You can even keep the same dataset if you want, just drop the new data from the second select into a different table. Doing that would be more efficient.Thank u for reply . I tried to run the above code but
i get this error:

Server Error in '/asp' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'newscol' is not declared.

Source Error:



Line 13: blogfillcommand = new oledbdataadapter ("SELECT ProductName, UnitPrice FROM products", blogconn)
Line 14: blogfillcommand.fill(blogposts, "theposts")
Line 15: newscol.datasource=blogposts.tables("theposts").defaultview
Line 16: newscol.databind()
Line 17: 'second select

Source File: C:\aspscripts\aspx\multiquery2.aspx Line: 15

--------------------
i used the datagrid names to print the resutl after the <script</script> block but still get same error. I be happy if u help me here. Thanksnewscol would be the name of a datagrid or repeater. <asp:repeater is an element, there is no declaration.
 
Back
Top