JAB79
asked on
Filling a dataset with more than one table
I'm sure this is very easy, but try as I might, I can't get it to work. My code is as follows:
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
Dim Connect As OleDBConnection = New OleDBConnection
Dim Adapter As OleDBDataAdapter = New OleDBDataAdapter
Dim WhatsOnDS As DataSet = New DataSet
Dim ConnectString, SelectStatement1, SelectStatement2 As String
SelectStatement1 = "SELECT * FROM Events ORDER BY Date"
SelectStatement2 = "SELECT * FROM News ORDER BY Date"
ConnectString = "Provider=Microsoft.Jet.OL EDB.4.0; Data Source=C:\inetpub\wwwroot\ youth\db\y outhdb.mdb ;"
Connect.ConnectionString = ConnectString
Adapter.SelectCommand = New OleDbCommand(SelectStateme nt1, Connect)
Adapter.SelectCommand.Conn ection.Ope n
Adapter.Fill(WhatsOnDS, "Events")
Adapter.SelectCommand = New OleDbCommand(SelectStateme nt2, Connect)
Adapter.Fill(WhatsOnDS, "News")
EventsGrid.DataSource = WhatsOnDS.Tables("Events")
Page.DataBind
If WhatsOnDS.Tables("Events") .Rows.Coun t < 1 Then
Results.Text = "Unfortunately there are currently no events listed. If you you" & _
" are an organisation and would like to submit one, please <a href='Contact-Us.htm'>" & _
" contact us</a>."
EventsPanel.Visible = False
Else
Results.Text = "Not sure what events are taking place? Then look no further!" & _
" <p>The events listed below have all been entered by registered youth organisations" & _
" and are open to everyone. If you are involved with an organisation and want to" & _
" advertise a forth coming event, please <a href='Contact-Us.htm'>cont act us</a>.<p>"
End If
End If
End Sub
I get an error when I try and use the second Adapter.Fill statement;
Adapter.Fill(WhatsOnDS, "News")
which is:
Exception Details: System.Data.OleDb.OleDbExc eption: No value given for one or more required parameters.
Any ideas?
Cheers,
James
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
Dim Connect As OleDBConnection = New OleDBConnection
Dim Adapter As OleDBDataAdapter = New OleDBDataAdapter
Dim WhatsOnDS As DataSet = New DataSet
Dim ConnectString, SelectStatement1, SelectStatement2 As String
SelectStatement1 = "SELECT * FROM Events ORDER BY Date"
SelectStatement2 = "SELECT * FROM News ORDER BY Date"
ConnectString = "Provider=Microsoft.Jet.OL
Connect.ConnectionString = ConnectString
Adapter.SelectCommand = New OleDbCommand(SelectStateme
Adapter.SelectCommand.Conn
Adapter.Fill(WhatsOnDS, "Events")
Adapter.SelectCommand = New OleDbCommand(SelectStateme
Adapter.Fill(WhatsOnDS, "News")
EventsGrid.DataSource = WhatsOnDS.Tables("Events")
Page.DataBind
If WhatsOnDS.Tables("Events")
Results.Text = "Unfortunately there are currently no events listed. If you you" & _
" are an organisation and would like to submit one, please <a href='Contact-Us.htm'>" & _
" contact us</a>."
EventsPanel.Visible = False
Else
Results.Text = "Not sure what events are taking place? Then look no further!" & _
" <p>The events listed below have all been entered by registered youth organisations" & _
" and are open to everyone. If you are involved with an organisation and want to" & _
" advertise a forth coming event, please <a href='Contact-Us.htm'>cont
End If
End If
End Sub
I get an error when I try and use the second Adapter.Fill statement;
Adapter.Fill(WhatsOnDS, "News")
which is:
Exception Details: System.Data.OleDb.OleDbExc
Any ideas?
Cheers,
James
I am also thinking that what you're doing here, if you get past the syntax issues, is that the news will overwrite the events.
FtB
FtB
ASKER
The two tables are not related. I just thought it would be easier to deal with the information if they were in separate tables. The events bit goes like this:
<asp:label id="Results" runat="server"/>
<asp:panel id="EventsPanel" runat="server">
<asp:datagrid id="EventsGrid" runat="server" autogeneratecolumns="false " border="0" align="center" cellspacing="5" font-bold="true" font-name="Verdana, Arial, Helvetica, sans-serif" font-size="10" forecolor="#999999">
<headerstyle font-bold="true" font-name="Verdana, Arial, Helvetica, sans-serif" font-size="10" forecolor="#CC0000"/>
<columns>
<asp:hyperlinkcolumn headertext="EVENT" datanavigateurlfield="Even tID" datanavigateurlformatstrin g="javascr ipt:var w=window.open('eventdetail s.aspx?Eve ntID={0}', 'popup','s crollbars= 1,width=40 0,height=3 50');" datatextfield="Title"/>
<asp:boundcolumn headertext="DATE" DataFormatString="{0:d}" datafield="Date"/>
</columns>
</asp:datagrid>
</asp:panel>
(sorry for the way the line breaks occur!)
I then wanted to take the most up-to-date entry in the News table and place that in my page somewhere else.
Initially I wanted to have two datasets and deal with it like that, but I don't think thats possible. (I'm quite new to this!)
Does that make sense?
James
<asp:label id="Results" runat="server"/>
<asp:panel id="EventsPanel" runat="server">
<asp:datagrid id="EventsGrid" runat="server" autogeneratecolumns="false
<headerstyle font-bold="true" font-name="Verdana, Arial, Helvetica, sans-serif" font-size="10" forecolor="#CC0000"/>
<columns>
<asp:hyperlinkcolumn headertext="EVENT" datanavigateurlfield="Even
<asp:boundcolumn headertext="DATE" DataFormatString="{0:d}" datafield="Date"/>
</columns>
</asp:datagrid>
</asp:panel>
(sorry for the way the line breaks occur!)
I then wanted to take the most up-to-date entry in the News table and place that in my page somewhere else.
Initially I wanted to have two datasets and deal with it like that, but I don't think thats possible. (I'm quite new to this!)
Does that make sense?
James
I see a few options:
1) fill the data grid with the Events with one adaptor, and then fill a field with the top event from news in another
2) create separate adaptors for Events and News creating a data grid for each.
FtB
1) fill the data grid with the Events with one adaptor, and then fill a field with the top event from news in another
2) create separate adaptors for Events and News creating a data grid for each.
FtB
ASKER
The latter option sounds better. How do I achieve this. Is my initial code close?
Cheers
James
Cheers
James
This should do it as long as you have two separate datagrids, one called events and the other called news:
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider= Microsoft. Jet.OLEDB. 4.0; Data Source=C:\inetpub\wwwroot\ youth\db\y outhdb.mdb ;")
dbconn.Open()
sql="SELECT * FROM Events ORDER BY Date"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReade r()
Events.DataSource=dbread
Events.DataBind()
sql="SELECT * FROM News ORDER BY Date"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReade r()
News.DataSource=dbread
News.DataBind()
dbread.Close()
dbconn.Close()
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=
dbconn.Open()
sql="SELECT * FROM Events ORDER BY Date"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReade
Events.DataSource=dbread
Events.DataBind()
sql="SELECT * FROM News ORDER BY Date"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReade
News.DataSource=dbread
News.DataBind()
dbread.Close()
dbconn.Close()
ASKER
Thanks for that FtB. I've tried to implement your suggested code (see below), but I have an error using the DataReader object in the instant when I declare it;
Compiler Error Message: BC30390: 'System.Data.OleDb.OleDbDa taReader.P rivate Sub New(connection As System.Data.OleDb.OleDbCon nection, command As System.Data.OleDb.OleDbCom mand, depth As Integer, chapter As System.IntPtr)' is not accessible in this context because it is 'Private'.
Any ideas where I may be going wrong?
James
====================
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
Dim objConnection As OleDbConnection = New OleDbConnection
Dim objCommand As OleDbCommand = New OleDbCommand
Dim objDataReader As OleDbDataReader = New OleDbDataReader
Dim connString, SQL1, SQL2 As String
connString = "Provider=Microsoft.Jet.OL EDB.4.0; Data Source=C:\inetpub\wwwroot\ youth\db\y outhdb.mdb ;"
objConnection = New OleDbConnection(connString )
objConnection.Open()
SQL1 = "SELECT * FROM Events ORDER BY Date"
SQL2 = "SELECT * FROM News ORDER BY Date"
objCommand = New OleDbCommand(SQL1, objConnection)
objDataReader = objCommand.ExecuteReader()
EventsGrid.DataSource = objDataReader
EventsGrid.DataBind()
objCommand = New OleDbCommand(SQL2, objConnection)
objDataReader = objCommand.ExecuteReader()
NewsGrid.DataSource = objDataReader
NewsGrid.DataBind()
objConnection.Close()
If EventsGrid.Rows.Count < 1 Then
Results.Text = "Unfortunately there are currently no events listed. If you you" & _
" are an organisation and would like to submit one, please <a href='Contact-Us.htm'>" & _
" contact us</a>."
EventsPanel.Visible = False
Else
Results.Text = "Not sure what events are taking place? Then look no further!" & _
" <p>The events listed below have all been entered by registered youth organisations" & _
" and are open to everyone. If you are involved with an organisation and want to" & _
" advertise a forth coming event, please <a href='Contact-Us.htm'>cont act us</a>.<p>"
End If
End If
End Sub
Compiler Error Message: BC30390: 'System.Data.OleDb.OleDbDa
Any ideas where I may be going wrong?
James
====================
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
Dim objConnection As OleDbConnection = New OleDbConnection
Dim objCommand As OleDbCommand = New OleDbCommand
Dim objDataReader As OleDbDataReader = New OleDbDataReader
Dim connString, SQL1, SQL2 As String
connString = "Provider=Microsoft.Jet.OL
objConnection = New OleDbConnection(connString
objConnection.Open()
SQL1 = "SELECT * FROM Events ORDER BY Date"
SQL2 = "SELECT * FROM News ORDER BY Date"
objCommand = New OleDbCommand(SQL1, objConnection)
objDataReader = objCommand.ExecuteReader()
EventsGrid.DataSource = objDataReader
EventsGrid.DataBind()
objCommand = New OleDbCommand(SQL2, objConnection)
objDataReader = objCommand.ExecuteReader()
NewsGrid.DataSource = objDataReader
NewsGrid.DataBind()
objConnection.Close()
If EventsGrid.Rows.Count < 1 Then
Results.Text = "Unfortunately there are currently no events listed. If you you" & _
" are an organisation and would like to submit one, please <a href='Contact-Us.htm'>" & _
" contact us</a>."
EventsPanel.Visible = False
Else
Results.Text = "Not sure what events are taking place? Then look no further!" & _
" <p>The events listed below have all been entered by registered youth organisations" & _
" and are open to everyone. If you are involved with an organisation and want to" & _
" advertise a forth coming event, please <a href='Contact-Us.htm'>cont
End If
End If
End Sub
Take a look here for an example/tutorial--perhaps you can model your code after that?
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_dbconn_datalist
FtB
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_dbconn_datalist
FtB
ASKER
Certainly a good guide. I think yours/my code is more or less the same, with the exception being that I have Explicit set to true:
<%@ Page Explicit="True" Language="VB" Debug="True" %>
and in turn;
Dim objConnection As OleDbConnection = New OleDbConnection
Dim objCommand As OleDbCommand = New OleDbCommand
Dim objDataReader As OleDbDataReader = New OleDbDataReader
Dim connString, SQL1, SQL2 As String
Compared to;
dim dbconn,sql,dbcomm,dbread
In the above, what is dbread? A string?
Why is OleDbDataReader private?
<%@ Page Explicit="True" Language="VB" Debug="True" %>
and in turn;
Dim objConnection As OleDbConnection = New OleDbConnection
Dim objCommand As OleDbCommand = New OleDbCommand
Dim objDataReader As OleDbDataReader = New OleDbDataReader
Dim connString, SQL1, SQL2 As String
Compared to;
dim dbconn,sql,dbcomm,dbread
In the above, what is dbread? A string?
Why is OleDbDataReader private?
I am afraid that I can't help much further--I am more of an ASP rather than ASP.net person, so I haven't played with this much. I knew just enough to spot a few problems in your original code.
FtB
FtB
This Question is now classified abandoned as no comment has been added to this question in more than 21 days.
I will leave the following recommendation on this question to the moderators in the Cleanup topic area:
PAQ with points refunded
Any objections should be posted here in the next 4 days. After that time, the question will be closed at the discretion of the moderator.
vnvk
EE Cleanup Volunteer
I will leave the following recommendation on this question to the moderators in the Cleanup topic area:
PAQ with points refunded
Any objections should be posted here in the next 4 days. After that time, the question will be closed at the discretion of the moderator.
vnvk
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
FtB