Reputation: 7
I had a question about retrieving data from MS Access database using timestamps i.e. starting and ending date on a user form. Kindly let me know if and what's the process to do that. I'm using Microsoft Visual Studio 2015. Website user interface will be in Asp.net with VB. Although I have tried passing parameters I am not understanding why it is not reflecting on the Grid View
ASPX.VB Code
Partial Class _Default
Inherits System.Web.UI.Page
Public dad As OleDbDataAdapter
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
Protected Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Protected Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim connectionString As [String] = "Provider=Microsoft.ACE.OLEDB.12.0;Data" + " Source=App_Data/VQT_GL Testing.mdb"
Dim ds As New DataSet()
Dim conn As New OleDbConnection(connectionString)
conn.Open()
Dim cmd As New OleDbCommand("SELECT AVG(POLQA_Score) AS MEAN FROM VQTPOLQA WHERE VQT_Timestamp BETWEEN ? AND ?", conn)
cmd.Parameters.AddWithValue("@StartDate", (TextBox3.Text))
cmd.Parameters.AddWithValue("@EndDate", (TextBox2.Text))
GridView1.DataSource = ds
End Sub
End Class
ASPX CODE
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>MOS FORM HOME</title>
<script src="//code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"type="text/javascript"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"/>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript" ></script>
<script src="Scripts/jquery.dynDateTime.min.js" type="text/javascript"></script>
<script src="Scripts/calendar-en.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-timepicker-addon.js"></script>
<link href="Styles/calendar-blue.css" rel="stylesheet" type="text/css" />
<link href="Styles/jquery-ui-timepicker-addon.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
$(document).ready(function () {
$("#<%=TextBox3.ClientID %>").dynDateTime({
showsTime: true,
ifFormat: "%m/%d/%Y %H:%M:%S",
daFormat: "%l;%M %p, %e %m, %Y",
align: "BR",
electric: false,
singleClick: false,
displayArea: ".siblings('.dtcDisplayArea')",
button: ".next()"
});
$("#<%=TextBox2.ClientID %>").dynDateTime({
showsTime: true,
ifFormat: "%m/%d/%Y %H:%M:%S",
daFormat: "%l;%M %p, %e %m, %Y",
align: "BR",
electric: false,
singleClick: false,
displayArea: ".siblings('.dtcDisplayArea')",
button: ".next()"
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 74px">
MOS FORM HOME PAGE</div>
<p>
START DATE
<asp:TextBox ID="TextBox3" runat="server" TextMode="DateTime" Width="181px" BorderColor="Black" BorderStyle="Solid"></asp:TextBox><img src="calender.png" />
</p>
<p>
END DATE <asp:TextBox ID="TextBox2" runat="server" TextMode="DateTime" Width="188px" BorderColor="Black" BorderStyle="Solid"></asp:TextBox><img src="calender.png" />
</p>
<p>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="btnSave_Click"/>
</p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" DataSourceID="SqlDataSource2" GridLines="Horizontal" Height="146px" Width="53px">
<Columns>
<asp:BoundField DataField="MEAN" HeaderText="MEAN" ReadOnly="True" SortExpression="MEAN" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:VQT_GL TestingConnectionString %>" ProviderName="<%$ ConnectionStrings:VQT_GL TestingConnectionString.ProviderName %>" SelectCommand="SELECT AVG(POLQA_Score) AS MEAN FROM VQTPOLQA WHERE VQT_Timestamp BETWEEN ? AND ?">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox3" DefaultValue="" Name="?" PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" DefaultValue="" Name="?" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:AccessDataSource runat="server" DataFile="~/App_Data/VQT_GL Testing.mdb" SelectCommand="SELECT AVG(POLQA_Score) AS MEANPOLQA FROM VQTPOLQA WHERE (VQT_Timestamp BETWEEN ? AND ?)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox3" DefaultValue="" Name="?" PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" Name="?" PropertyName="Text" />
</SelectParameters>
</asp:AccessDataSource>
</form>
</body>
</html>
Upvotes: 0
Views: 60
Reputation: 5235
At first sight, your problem seems to be with your parameters to your OleDbCommand. You are using cmd.Parameters.AddWithValue. This is something I tend to avoid, as the datatype of the Parameter is inferred from the value passed. In this case you are passing text. This means that the parameter is treated as a string parameter not a DateTime. This is almost certainly going to fail! Instead you should set the type of the parameter yourself. To do this use something like:
Dim param as OleDbParameter
param = cmd.CreateParameter
param.OleDbType = OleDbType.Date
param.Value = DateTime.Parse(TextBox1.Text)
param.ParameterName = "@myDate"
cmd.Parameters.Add(param)
Note for Access OleDbType needs to be Date even though the field is DateTime.
It may be that I have a syntax error in the above - it is a very long time since I used VB, and I cannot remember when you need round brackets and when you don't, but it should be something like this (I could give it to you exactly in c# ). I am sure you can correct my VB.
Secondly you are not actually executing your OleDbCommand! Here there are different possiblilities. You can use an OleDbDataAdapter or an OleDbDataReader. Personally I prefer to use OleDbDataReader and I then build my own DataTable from the result to use as the DataSource, but you may find an Adapter easier. Please google for examples of how to do this! It should be something like:
Dim adapter as New OleDbDataAdapter(cmd)
adapter.Fill(ds)
Again my VB is not upto date!
Hope this helps
Upvotes: 1