Retrieve ADO Recordset Field names (Classic ASP) - asp-classic

I wonder if someone can help:
Long story short, I'm using MSSQL2005 to build a Pivot table. The data being examined is limited by date range (All data for 1 week starting from the nearest Monday to the date selected)
When I run the Stored Proc and pass it a date, I get The correct table back eg:
Time
1 Jan 09
2 Jan 09
3 Jan 09
...
09:00
0
9
25
...
09:30
8
27
65
...
10:00
20
44
112
...
The only problem I have is that the column headers will vary based on both the date passed in to the SP (The desired view date) and the logic inside the SP (which forces the left-hand column to be the nearest Monday to the date specified).
This means that when I display the results to the user, I (currently) need to duplicate the date-checking logic in classic ASP [easy but a maintainability fail]
What I really need is a way of retrieving the column names from the recordset itself.
Can someone please point me in the right direction?
I've Googled but all the results I get seem to relate to reading a Table Schema - which doesn't help in this case as my table is being generated on the fly in memory.
Many thanks in advance for any help you can provide

Given an ado record set you could do roughly the following (This is in psuedo code):
foreach (field in rs.Fields)
{
alert(field.Name);
}
This will give you the name of the field check out this documentation.

Something like this ought to do it:-
<table>
<thead>
<tr>
<%For Each fld in rst.Fields%>
<th><span><%=Server.HTMLEncode(fld.Name)%></span></th>
<%Next %>
</tr>
</thead>
<tbody>
<%
Do Until rst.EOF
OutputRow rst.Fields
rst.MoveNext
Loop
%>
</tbody>
</table>
Sub OutputRow(fields)
%>
<tr>
<%For Each fld in fields%>
<td><span><%=Server.HTMLEncode(fld.Name)%></span></td>
<%Next %>
</tr>
<%
End Sub
%>

Related

How can I get the rows for each table without picking up the embedded tables rows?

I'm using Nokogiri and ruby, and I'm trying to get the information from the table at the following link: roster.
In the past I've used the "id" field for the table I want to use, but since I want to protect against them changing that id in the future and breaking my code I want to identify the table by assuming things about it's attributes. Such as, the table has an unordinary number of rows (<tr>), and assuming it would almost always have 4 or more columns.
The problem I'm having here is when I use the following code to pull back all tables in the web page and then look for the one with 80+ rows, it counts the child table's rows, when I'd like to only count the immediate children's rows.
tables = document.css("table")
tables.each do |table|
tables.each { |table| puts "#{table.css('tr').count}" }
end
Output for this would be...
202
135
109
22
19
1
0
There should actually be only one table here that is 100+ rows. But because the document embeds tables within tables it's returning counts for all the <tr> elements within the table including the embedded tables. How can I change my selector to only return the immediate children <tr> count from each table?

SQL Query to retrieve date adds time to value?

Having a bit of trouble with my SQL query.
I am retrieving a value from my SQL database, where it has the date of when a person was added as a life member. The column type is "date" rather than varchar as i couldnt organise by date if it was just a string.
The value of "DateInducted" is just a simple date such as "01/05/1963"
In the query builder for Visual Studio, I have tested my SQL Statement (which is below) and appears exactly as i want
"SELECT [DateInducted], [Name] FROM [LifeMembers] ORDER BY [DateInducted] DESC"
However when the Website is loaded, the value is
5/26/2012 12:00:00 AM
Where is it getting the time from? Is there a way to stop this? All i need is the date.
Thinking this is just a simple oversight on my part, but Google searches didnt provide the correct answer. According to W3 schools site, Date type should just give me the date?
Thanks in Advance
Edit:
Page Code now attached
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h2>Lifemembers</h2>
<table class="DataTable">
<asp:Repeater ID="rptLifemembers" runat="server" DataSourceID="dsLifeMembers">
<HeaderTemplate>
<tr class="DataTableHeader">
<td style="min-width:40%"><strong>Date Inducted</strong></td>
<td style="min-width:60%"><strong>Member</strong></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="DataTableRow">
<td style="min-width:40%"><strong><%#Eval("DateInducted")%></strong></td>
<td style="min-width:60%"><%#Eval("Name")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:SqlDataSource ID="dsLifeMembers" runat="server"
ConnectionString="<%$ ConnectionStrings:SPEEDWAYConnectionString %>"
SelectCommand="SELECT [DateInducted], [Name] FROM [LifeMembers] ORDER BY [DateInducted1] DESC">
</asp:SqlDataSource>
You can either convert the date to varchar in the query or format the output in asp.net.
If you choose to do it in the query you can use the function Convert:
SELECT CONVERT(varchar(50),[DateInducted], 101) DateInducted, [Name] FROM [LifeMembers] ORDER BY [DateInducted] DESC
Add a format string to your binding expression:
<%#Eval("DateInducted", "{0:d}")%>
The datetime data type in SQL and the Date type in CLR always hold both a date and a time. In order to display one or the other and not both when converting to string, you need to use a format string or Date built-in string conversion functions such as ToShortDateString().

Out of 10, 3 times I get this error : Microsoft JET Database Engine error '80040e10' No value given for one or more required parameters

I really don't understand this. I have a simple ASP with 3 div in it.
Each div has some HTML with list boxes fills values when page loads.
When ever I run this page, Most of the times it works ok but 3 times out of approx 10, i get this.
Microsoft JET Database Engine error '80040e10'
No value given for one or more required parameters.
/order_tracking/order_admin.asp, line 269
This line has this query written: THIS IS THE FIRST QUERY ON THE PAGE Error It say on the line where rsRecordSet.Open is written
<%Set rsRecordSet = Server.CreateObject("ADODB.Recordset")
rsRecordSet.Open "select top 1 * from orders order by id desc", cn
While not rsRecordSet.EOF
%>
<input type="text" size="15" id="last_order_no" name="last_order_no" value="<%=rsRecordSet("order_no")%>" disabled="disabled"/>
<%rsRecordSet.MoveNext
Wend
If rsRecordSet.State = adStateOpen Then rsRecordSet.Close
Set rsRecordSet= nothing
%>
Please suggest whats wrong...i would appreciate.
Sounds like internal database problem if it sometimes work and sometimes it doesn't work.
Do you have lots of traffic? If so, you better move to modern database like SQL Server or MySQL.
Anyway, I will try those things first and see what happens:
Change the code to be:
Set rsRecordSet = cn.Execute("select top 1 * from orders order by id desc")
Why it might work? Because the Open method is using cursors and locks that might be "heavy". Using Execute of the connection itself should be as "light weighted" as possible.
Change the SQL statement to be:
"select top 1 [order_no] from [orders] order by [id] desc"
Maybe it will help - Access is sensitive about field names sometimes.
Change the code to:
rsRecordSet.Open "select * from orders order by id desc", cn
IF not rsRecordSet.EOF Then %>
<input type="text" size="15" id="last_order_no" name="last_order_no" value="<%=rsRecordSet("order_no")%>" disabled="disabled"/> <%
End If
Maybe the Select Top is doing some nasty stuff behind the scenes.
As last resort before giving up, write Stored Procedure returning that single record and use it instead of raw SQL statement.

Gridview row having multiple record

I have a stored procedure that retrieves employee information - I don’t want to edit this stored procedure because it is globally use with in the website.
Here is now my dilemma, how can I put multiple row records in a column in the grid view. Is Gridview powerful enough to my scenario below or I really need to edit the stored procedure or my copy of it. Thanks
Records retrieve by stored procedure
Employee_ID Purchase ID Amount
1 0123456 100
1 012356 560
1 012446 560
1 012126 560
2 011122 100
2 051122 200
I want to achieve this in gridview
Employee ID Purchase ID Total Amount
-----------------------------------------
1 012345 1780
012346
012446
012126
2 011122 3000
051122
You could create a new stored procedure for use with this gridview only, leaving your existing stored procedure as-is so it doesn't break other parts of your code.
Alternatively, if you don't want to create a new stored procedure, you could sum the totals in your vb code, although it will be more work than doing it at the DB level. You'd do this using the gridview's datasource, not the gridview itself.
You can add a template column, and start it with (I believe) an </td></tr><tr><td>
In any case, each gridview template column implicitly (you can't see it) renders open/close td tags around whatever you put in it.

how to insert record into database on single button click from date to todate?

I have three textbox ... textbox1 and textbox2 and textbox3
I want when I choose from date in textbox1 say 1-May-2011 and to date in textbox2 say 30-May-2011 and in textbox I type 1,2,3,4,5
I want on button click ... event the values will be entererd in database from 1-May-2011 to 30-May2011 as : mentioned below :
DATABASE STRUCTURE
ID Date Items
1 1-May-2011 1,2,3,4,5
2 2-May-2011 1,2,3,4,5
3 3-May-2011 1,2,3,4,5
so on till 30_may-2011
Record will be inserted in database according to from date and to date choosen in textbox1 and textbox2 respectively ...
How to resolve this ?
If it is acceptable for your solution, then the easiest way is to construct all the data for insertion and then insert them all (it will cause a number of inserts). This is what Ranhiru Cooray has suggested in his answer - just iterate through all dates, construct each row and call insert for each of them.
BUT, if you want to do it by one DB call, then I would suggest creating a stored procedure in database, which gets these 3 parameters and then executes inserts inside it - by constructing insertion rows inside itself and executing each of them - still remaining in bounds of a single database call for your code or application.
I hope this helps!
DateTime start = DateTime.Parse("1/1/2010");
DateTime end = DateTime.Parse("1/30/2010");
while(start <= end)
{
Console.WriteLine("Date : " + start.ToShortDateString());
start = start.AddDays(1);
}
This will help you get started :)

Resources