display data to table horizontally asp sql - sql

So I have a data that I got from the database that I want to display horizontally in my asp page. It is like a transpose option in Excel.
Here is how my data is currently displayed.
Call # | Greetings # | Verifying Information # | Hold Protocol # |
Call 1 | Greetings 1 | Verifying Information 1 | Hold Protocol 1 |
Call 2 | Greetings 2 | Verifying Information 2 | Hold Protocol 2 |
Call 3 | Greetings 3 | Verifying Information 3 | Hold Protocol 3 |
But I want to display it like this.
Call # | Call 1 | Call 2 | call 3
Greeting # | Greeting 1 | Greeting 2 | Greeting 3
Verifying# | Verifying 1 | Verifying 2 | Verifying 3
Hold Protocol# | Hold Protocol 1 | Hold Protocol 2 | Hold Protocol 3
And here is my code for displaying the data
<table class="table-striped table-bordered table-scores">
<tr>
<td>Call #</td>
<td>Greetings</td>
<td>Verifying Information</td>
<td>Hold Protocol</td>
</tr>
<%
if not RsAgent.eof then
do while not RsAgent.eof
%>
<tr>
<td> <% response.write("Call ") %>> </td>
<td> <% response.write(greeting) %> </td>
<td> <% response.write(verify) %> </td>
</tr>
<%
ctr =ctr+1
RsAgent.movenext
loop
end if
end if
%>
</table>

You can use the GetRows Recordset's method to retrieve the records into an array and then print the data as you like.
This is an example:
<table>
<%
titles = Array("Call #", "Greetings #", "Verifying Information #", "Hold Protocol #")
data = rsAgent.GetRows()
For i=0 To 3
%>
<tr>
<th><%=titles(i)%></th>
<% For j=0 To UBound(data,2) %>
<td><%=data(i,j)%></td>
<% Next j %>
</tr>
<% Next i %>
</table>

What you might want to do is something like this.
<tr>
<td>Call#</td>
<% while not RsAgent.eof %>
<td><% response.write("call") %></td>
<% RsAgent.movenext %>
<% wend %>
</tr>
<% RsAgent.movefirst %>
<tr>
<td>Greetings</td>
<% while not RsAgent.eof %>
<td><% response.write("greeting") %></td>
<% RsAgent.movenext %>
<% wend %>
</tr>
and so on...

Related

Half of Classic ASP page works great, 2nd half gives SQL connection error

Please help!
I am moving a Classic ASP & ASP.Net website from one server to another, and one of the reports that works fine on the old server is only partially working on the new server.
Can someone please tell me:
If there is anything that might be preventing a successful connection to SQL server.
If there is anything else involved in the this than:
Connection String
ASP Page
DLL
My server environment:
Windows Server 2008 R2
SQL EXPRESS 2008
IIS7
Connection Details:
The Classic ASP pages of this website connect using a SQL connection string located in the registry. The other Classic ASP pages on the website are working just fine....and the first half of this report is also working just fine.
I have tested the connection string individually, and it connects properly.
Report Structure Details:
The first half of the report (that is working correctly) loads data directly from a table in the database to display each item in a list format. It also uses the EMPLOYEES table to populate the employee name for each item in the list.
The second half of the report (that is NOT working) loads data from the same table as the first half. However, it groups them by manager (Manager Details are stored in the EMPLOYEES table).
So, both half of the reports are accessing the same tables for information....the only difference is how they are displayed.
Error Details
Microsoft OLE DB Provider for SQL Server->DLL.Shape_DB.ShapeCMdWithRS(SHAPE {select * from employees where username in (select mgr_username from employees where empID in (select empID from time_off_requests where mgr_code=0)) order by last_name, first_name} APPEND ([select time_off_requests_view.*, employees.mgr_username as username from time_off_requests_view inner join employees on employees.empID = time_off_requests_view.empID where mgr_code=0} relate username to username) as timeoff, ...)#WEBSITENAME[version 4.0.480] error '80004005'
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
Code Details
Here is the CLASSIC ASP code for the part of the website that DOES work:
set time_off_requests = DLL.TimeOffAdminPending()
%>
<CENTER>
<%
if time_off_requests.EOF then %>
<font class="headerNoBold">No Pending Time Off Requests<BR>
<% else %>
<font class="headerNoBold">Pending Time Off Requests</font>
<table cellspacing="0" cellpadding="3" width="500" align=center border=0 bordercolorlight=#000000 bordercolordark=#000000>
<TR bgcolor=#ffffcc>
<TD width=33% align=center><B>Date</B></TD>
<TD width=33% align=center><nobr><B>Requested By</B></nobr></TD>
<TD width=33% align=center><nobr><B>Type</B></nobr></TD>
</TR>
<%
dim theDate
do while not time_off_requests.EOF
if (time_off_requests.AbsolutePosition mod 2) = 0 then %>
<TR bgcolor=#ffffcc>
<% else %>
<TR bgcolor=#ffffff>
<% end if %>
<TD class="smallText" align=center>
<A HREF="time_off_request_details.asp?timeoffreq_id=<% = time_off_requests("timeoffreq_id") %>">
<% = time_off_requests("dt_created") %>
</A>
</TD>
<TD class="smallText" align=center><% = time_off_requests("last_name") %>, <% = time_off_requests("first_name") %></TD>
<TD class="smallText" align=center><% = time_off_requests("time_off_type") %></TD>
</TR>
<%
time_off_requests.MoveNext
loop %>
</TABLE>
<% end if %>
Details from the DLL
Function TimeOffAdminPending() As ADODB.Recordset
TimeOffAdminPending = RunCmdWithRS("select * from time_off_request_view where mgr_code =0 and empID in (select empID from employees where mgr_username = '" & username & "')")
End Function
Here is the CLASSIC ASP code for the part that DOES NOT work:
<% set managers = DLL.PendingTimeOffMgr()
if not managers.EOF then %>
<font class="headerNoBold">Pending Manager Time Off Approvals</font>
<table cellspacing="0" cellpadding="3" width="500" align=center border=0 bordercolorlight=#000000 bordercolordark=#000000>
<TR bgcolor=#ffffcc>
<TD width=33% align=center><B>Date</B></TD>
<TD width=33% align=center><nobr><B>Requested By</B></nobr></TD>
<TD width=33% align=center><nobr><B>Type</B></nobr></TD>
</TR>
<% do while not managers.EOF
set time_requests = managers.Fields("timeoff").Value %>
<TR><TD colspan=3>
<% = managers("last_name") %>, <% = managers("first_name") %>
</TD><TR>
<%
do while not timeoff_requests.EOF
if (timeoff_requests.AbsolutePosition mod 2) = 0 then %>
<TR bgcolor=#ffffcc>
<% else %>
<TR bgcolor=#ffffff>
<% end if %>
<TD class="smallText" align=center>
<% = timeoff_requests("dt_created") %>
</TD>
<TD class="smallText" align=center><% = timeoff_requests("last_name") %>, <% = timeoff_requests("first_name") %></TD>
<TD class="smallText" align=center><% = timeoff_requests("timeoff_type") %></TD>
</TR>
<%
timeoff_requests.MoveNext
loop
managers.MoveNext
loop %>
</TABLE>
<% end if %>
Details from the DLL
PendingTimeOffMgr() As ADODB.Recordset
PendingTimeOffMgr = ShapeCmdWithRS("SHAPE {select * from employees where username in (select mgr_username from employees where empID in (select empID from time_off_requests where mgr_code = 0)) order by last_name, first_name} APPEND ({select time_off_requests_view.*, employees.mgr_username as username from time_off_requests_view inner join employees on employees.empID=time_off_requests_view.empID where mgr_code = 0} relate username to username) as timeoff")
End function

Fastest `where` request involving three models in Rails

I'm having a little trouble with a slow page load in Rails, I'm certain due to the way I'm querying the database (PostgreSQL).
To summarise the situation, I've three models - let's call them Model A, Model B and Model C - that I'm plugging into a table, and am struggling to find an efficient way to query the data.
Model A provides the table's header row, Model B each row in the tbody; Model C belongs to both Model A and Model B and provides the table data.
Here's a quick example:
Say the model variables are as follows:
#model_a = ModelA.all
#model_b = ModelB.all
#model_c = ModelC.all
Related as follows:
class ModelA
has_many :model_cs
end
class ModelB
has_many :model_cs
end
class ModelC
belongs_to :model_a
belongs_to :model_b
end
And in the view:
<table>
<thead>
<tr>
<th></th>
<!-- Model A provides the table headers -->
<% #model_a.each do |a| %>
<th>
<%= a.name %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<!-- Each row represents a different instance of Model B -->
<% #model_b.each do |b| %>
<tr>
<td>
<%= b.name %>
</td>
<% #model_a.each do |a| %>
<td>
<!-- The actual table data is from Model C, when it belongs to both Model A and Model B -->
<% if (c = #model_c.where(model_a_id: a.id, model_b_id: b.id).first).present? %>
<%= c.name %>
<% end %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Obviously this is really slow and, I'd imagine, rather stinky code. I can cache the fragment, but would much rather speed up the request so it runs smoothly following any changes to the data. Happy to consider different approaches to this - I just can't think of a way to present the data in this manner without iterating through two models to query the third.
I'm sure there's a far, far better way to approach this but have yet to come up with it. Hope you guys can help - thanks in advance, Steve.
In the view, this code will run a new SQL query each table cell:
#model_c.where(model_a_id: a.id, model_b_id: b.id).first
So that's probably what's really hurting you. Remember that where is part of ActiveRecord and goes to the database, whereas select operates on enumerables and does everything in memory.
You should load everything with the right relationships upfront. I would start with this:
#model_a = ModelA.all
#model_b = ModelB.includes(:model_cs)
Then do this:
<tbody>
<!-- Each row represents a different instance of Model B -->
<% #model_b.each do |b| %>
<tr>
<td>
<%= b.name %>
</td>
<% #model_a.each do |a| %>
<td>
<!-- The actual table data is from Model C, when it belongs to both Model A and Model B -->
<% if c = b.model_cs.select{|x| x.model_a_id = a.id}.first %>
<%= c.name %>
<% end %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
That should get you everything in two or three queries.
Note if A is large, the select will still perform poorly because it does a linear search. So you could write a method on your ModelB class like this instead, and use it in the view:
def model_c_for(a)
#model_c_by_a ||= Hash[
model_cs.map{|c| [c.model_a_id, c]}
]
#model_c_by_a[a.id]
end

Rails. how to display the data by column name

I use Rails 4.
I use structure
#users = ActiveRecord::Base.connection.execute("SELECT * FROM users")
#users.each do |row| %>
puts user[0] # id--> 1,2,3,4,5,6
end
#users.fields do |field| %>
puts field.name # --> id, login, password.....
end
how to display the data by column name?
for example
#users.first.field['id']
ActiveRecord::Base.connection.execute( will run the sql but not return anything (or return nil, more accurately). To load the records into memory do
#users = User.all
You don't describe what you want very well. If you literally want "1,2,3,4,5,6" you could do
#users.map(&:id).join(",")
if you wanted, for example, to do a table showing all the user data you could do (in your view)
<% cols = User.column_names %>
<table>
<thead>
<tr>
<% cols.each do |col| %>
<th><%= col %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<% cols.each do |col| %>
<td><%= user.send(col) %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Like i say i don't know if this is what you actually want to do. Your requirements are a very messy and very vague.
EDIT
If you want to load data out of the database without using an AR model then you can do
#users = ActiveRecord::Base.connection.select_all("select * from users")
this will give you an array of hashes like this
[{"id" => "1", "name" => "foo", "bar" => "baz"}, {"id" => "2", "name" => "chunky", "bar" => "bacon"}, .... ]
which you could then access like
user = #users.first #you've got a hash now
aname = user["name"]

SQL that creates a new table, essentially, but with ActiveRecord

def reports
sql = "select username, email, sign_in_count, current_sign_in_at, \
(select count(*) from foo ft where ft.user_id = u.id and ft.state in ('complete', 'active', 'win', 'lose')) as foo_state, \
(select count(*) from bar where creator_id = u.id and m.state not in ('new','cancelled')) as bar_created, \
from users u where sign_in_count > 0 order by foo_state desc;"
#users = ActiveRecord::Base.connection.execute(sql)
end
So, this query is botched a bit for purposes of showing you something, but not bothering you with the details. This query takes a few different tables, queries them, and returns a new table essentially. I'm using postgreSQL, so this returns a PG::Result object. I can call #users.values to get an array of arrays, but I was wondering if there was a more 'rails-y' way do do this?
I'd like to do something like this in the view:
<%= render partial: 'users/shared/user', collection: #users, as: :user %>
but since it's not an actual ActiveRecord object, I can't seem to do that (although it doesn't throw an error if I try, it just doesn't display anything)
Can ARel accomplish a feat like this? Am I asking too much? Thanks!
For anyone else stumbling upon this... this works to get your info, at least:
<table>
<thead>
<tr>
<% #users.fields.each do |f| %>
<th><%= f %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #users.values.each do |v| %>
<tr>
<% v.each do |a| %>
<td><%= a %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>

Date field not saved using rails 3.0?

I have an ActiveRecord and when i click on save all records are saved except the date.
My contorller
class UsersController < ApplicationController
def create
puts params[:user]
#user1 = User.new(params[:user])
if #user1.save
saveduser = User.where("fbid = ?",params[:user][:fbid])
unless saveduser.first.nil?
session[:user] = saveduser.first
end
puts "user saved "
redirect_to "/users/dashboard"
else
puts "error while saving user"
end
end
The view
<h3>User Details</h3>
<%= form_for(#user) do |f| %>
<table>
--some columns
<tr>
<td><%= f.label :state %></td>
<td> <%= f.text_field :state %></td>
</tr>
<tr>
<td><%= f.label :dob %></td>
<td> <%= f.text_field :dob %></td>
</tr>
<%= f.hidden_field :fbid %>
</table>
<%= f.submit %>
<% end %>
<table>
In console when create method is called in UserController . I can see
{"username"=>"xxxx.xx.94", "firstname"=>"xxxx", "lastname"=>"Raxxstogi", "emaild"=>"xx.xxx#gmail.com", "city"=>"Los Angeles", "country"=>"USA", "state"=>"CA", "dob"=>"08/13/1983", "fbid"=>"xxx"}
My DB table column is
dob | date | YES | | NULL |
Where is it going wrong?
Thanks
You are using a text_field in your form to save into a Date type field in the DB. Try instead to use the date helper methods as described here:
http://guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpers
Ruby doesn't understand your date format. You can fix this by explicitly parsing the date with Date::strptime, perhaps in the model as a setter method:
class User < ActiveRecord::Base
def dob=(date)
date = Date.strptime(date, '%m/%d/%Y') if date.is_a?(String)
write_attribute(:dob, date)
end
end
I would, however, second jordanpg's recommendation to use the date helpers in Rails, unless you know that the format will be the same every time.
it could be the problem with your date format. You can either configure active record to accept this format or convert your date param to Date object using strftime or something like it