Cannot implicitly convert type 'double?' to 'double' - asp.net-mvc-2

i am using asp.net MVC2 and i am getting an error on the follwoing line of code
<% double RunningTotal = 0; %>
<%foreach (var item in Model)
{%>
<%var allowedHours = item.tblPerson.tblPersonAllowedHours.Where(x=>x.Semester == item.Semester).Sum(x=>x.AllowedHoursPerWeek); %>
<tr>
<td><%: item.Semester %></td>
<td><%: allowedHours.ToString() %></td>
<td><%: item.tblPerson.tblCourseWantedHours.Where(x => x.Semester == item.Semester).Sum(x => x.WantedHoursPerWeek+x.UL+x.UT) %></td>
<td><%: item.tblPerson.tblCourseWantedHours.Where(x => x.Semester == item.Semester).Sum(x => x.WantedHoursPerWeek+x.UL+x.UT) - allowedHours %></td>
<%: RunningTotal += (item.tblPerson.tblCourseWantedHours.Where(x => x.Semester == item.Semester).Sum(x => x.WantedHoursPerWeek + x.UL + x.UT) - allowedHours) %>
<td></td>
the error i am getting is on this line
<%: RunningTotal += (item.tblPerson.tblCourseWantedHours.Where(x => x.Semester == item.Semester).Sum(x => x.WantedHoursPerWeek + x.UL + x.UT) - allowedHours) %>
error description is
CS0266: Cannot implicitly convert type 'double?' to 'double'. An explicit conversion exists (are you missing a cast?)
any idea what i am missing here?

It looks like one of the things you are trying to add might be a Nullable<Double>
to go from double? to double you need to do
double? nullableDouble = 3.0;
if (nullableDouble.HasValue)
double notNullableDouble = nullableDouble.Value;
More info on Nullables: http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Related

Querying postgres database with rails

I'm trying to search my database based on params from a search form. I had it working perfectly fine in SQLlite, but when I migrated to postgres, several things broke. in app/views/index.html.erb I defined a form:
<tr>
<%= form_tag(guidelines_path, :method => "get", id: "search-form") do %>
<td><%= label_tag(:invprogsearch, "Search by Investor and Program")%></td>
<td><%= text_field_tag :investor, nil, placeholder: "Enter investor" %></td>
<td><%= text_field_tag :program_code, nil, placeholder: "Enter program" %></td>
<td><%= submit_tag "Search", :name => nil %></td>
<% end %>
</tr>
and in my controller (app/controllers/guidelines_controller.rb) I defined the index action as such
def index
if params[:investor].present? && params[:program_code].present?
#guidelines = Guideline.where(investor: params[:investor], program: params[:program_code])
else
#guidelines = Guideline.all
end
end
Any idea why this wouldn't work? I'm out of ideas. Thanks!
app/models/guideline.rb:
class Guideline < ActiveRecord::Base
validates :guide_desc, presence: true
belongs_to :parent, class_name: 'Guideline'
has_many :children, class_name: 'Guideline', foreign_key: 'parent_id'
scope :top_level, where(:parent_id => nil)
def descendants
self_and_descendants - [self]
end
def self_and_descendants
self.class.tree_for(self)
end
def self.tree_for(instance)
where("#{table_name}.id IN (#{tree_sql_for(instance)})").order("#{table_name}.id")
end
def self.tree_sql_for(instance)
tree_sql = <<-SQL
WITH RECURSIVE search_tree(id, path) AS (
SELECT id, ARRAY[id]
FROM #{table_name}
WHERE id = #{instance.id}
UNION ALL
SELECT #{table_name}.id, path || #{table_name}.id
FROM search_tree
JOIN #{table_name} ON #{table_name}.parent_id = search_tree.id
WHERE NOT #{table_name}.id = ANY(path)
)
SELECT id FROM search_tree ORDER BY path
SQL
end
end

how to check null or empty value in mvc view page

I want to check null or empty in view page. i gave code. but it is showing the error. i don't know how to give?
my code is
<% foreach (Models.JobPreferredIndustry jpi in Model.JobPreferredIndustries)
{ %>
<% if (jpi != null) { %>
<%:jpi.Industry.Name %>
<%} %>
<%if (string.IsNullOrEmpty(jpi))
{ %>
----------------
<%} %>
<%}%>
I know this is a bit late... Is the error Object reference not set to an instance of an object? Maybe the problem is that your Model or Model.JobPreferredIndustries is null.
You can add check for null before the foreach statement:
if(Model != null)
{
...
or
if(Model.JobPreferredIndustries != null)
{
...

Rails: how to save few elements from string

I have rails form_tag helper to save data provided by do..each loop. This is my form:
<%= form_tag (customers_path) do |f| %>
< #contacts.each do |c|%>
<%= check_box_tag "accept[]", c %><%= c[:email] %>
<% end %>
<%= submit_tag ("Save") %>
<%end%>
This form saves the contacts whose checkbox is checked. Here is what, c has:
"accept"=>["{:id=>\"2f310f1d9b8f\",
:first_name=>\"San\",
:last_name=>\"Jori\",
:name=>\"Jori,San\",
:email=>\"abc#qwe.com\",
:gender=>nil,
:birthday=>nil,
:profile_picture=>nil,
:relation=>nil}"],
"commit"=>"Save"
I want to save only :name and :email from above hash.
This is my create action of controller:
if params[:accept].present?
params[:accept].each do |customer|
#customer = current_user.customers.new(:name => customer[:name], :email => customer[:email])
#customer.save
end
redirect_to customers_path
end
But it is giving error of :
no implicit conversion of Symbol into Integer
Can anyone tell me how to make it work?
Thank you!
Instead of using
#customer = current_user.customers.new(:name => customer[:name], :email => customer[:email])
Try
#customer = current_user.customers.build(:name => customer[:name], :email => customer[:email])

How to preselect item in Html.DropDownlListFor()

How can i preselect item in Html.DromDownListFor() ?
i have code in view which inserts items to DropDownListFor
<div class="editor-field">
<%var mesta = new List<SelectListItem>();
SelectListItem aa = new SelectListItem();
aa.Text = "---------VYBER MESTO---------";
aa.Value = "0";
mesta.Add(aa);
foreach (var item in Model.MestoTbl)
{
SelectListItem a = new SelectListItem();
a.Text = item.Mesto;
a.Value = item.MestoId.ToString();
mesta.Add(a);}%>
<%: Html.DropDownListFor(model => model.Mesto.MestoId, mesta)%>
<%: Html.ValidationMessageFor(model => model.Mesto.MestoId)%>
</div>
this inserts 2 values MestoId & Mesto ....when i click on some database record (edit field)
example =>
Name Surname Mesto
--------------------
Peter Malik Snina
Snina => Mestoid = 2
I wanna get ....
if i click to edit record of Peter Malik the Html.DropDownListFor automatically preselect item Snina in list.
You could use the SelectList constructor. See here.
thanks now it is working.
<%: Html.DropDownListFor(model => model.Mesto.MestoId, new SelectList(mesta, "Value", "Text", Model.Ziak.MestoId))%>

how do I generate a tag cloud in acts_as_taggable_on?

I can't debug why I am getting an error:
class VendorsController < ApplicationController
def tag_cloud
#tags = Vendor.tag_counts_on(:tags)
end
I set this class as Taggable:
class Vendor < ActiveRecord::Base
acts_as_taggable_on :tags, :competitors
I include the TagsHelper:
module VendorsHelper
include TagsHelper
end
This is in my View:
<% tag_cloud(#tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>
This is the error that I get:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.empty?
Each instance of Vendor has at least one Tag.
Got it, I needed to add: #tags = Vendor.tag_counts_on(:tags) in the index controller.