I am trying to set the value of TrackingCategory on an invoice Line Item. My code is as follows:
LineItem li = new LineItem();
li.LineAmount = inv.LineItems[i].LineAmount;
li.ItemCode = inv.LineItems[i].ItemCode;
li.Description = inv.LineItems[i].Description;
li.AccountCode = inv.LineItems[i].AccountCode;
li.UnitAmount = inv.LineItems[i].UnitAmount;
var categories = inv.Categories.Where(c => c.TicketChargeLineId == inv.LineItems[i].TicketChargeLineId);
foreach(var c in categories)
{
if(c.Value != "")
{
ItemTrackingCategory tc = new ItemTrackingCategory();
tc.Name = c.Name;
tc.Id = c.Id;
tc.Option = c.Value;
li.Tracking.Add(tc);
}
}
lineItems.Add(li);
It errors at li.Tracking.Add(tc). It gives the error "Object reference not set to an instance of an object.", however tc is not null. It does show an empty Guid for OptionId. Any assistance is greatly appreciated.
Related
Codes inside Weekly returns a value. But in Monthly it returns null.
I tried to run the query in my SQL Server Management Studio and it returns the correct result. But in my code, it returns null.
I tried to breakpoint and the item.JobId is = 2.
Here is my codes.
if (item.Frequency == "Weekly")
{
if (item.StartDate.DayOfWeek.ToString() == DateTime.Now.DayOfWeek.ToString())
{
var jobname = _context.Jobs.Where(x => x.Id == item.JobId).FirstOrDefault();
if (ModelState.IsValid)
{
jobs.Id = 0;
jobs.Job = jobname.Job;
jobs.ClientName = jobname.ClientName;
jobs.ClientId = jobname.ClientId;
jobs.JobRate = jobname.JobRate;
jobs.Status = "Active";
}
}
}
else if (item.Frequency == "Monthly")
{
if (item.StartDate.Day.ToString() == DateTime.Now.Day.ToString())
{
var jobname = _context.Jobs.Where(x => x.Id == item.JobId).FirstOrDefault();
var a = jobname.Job;
if (ModelState.IsValid)
{
jobs.Id = 0;
jobs.Job = jobname.Job;
jobs.ClientName = jobname.ClientName;
jobs.ClientId = jobname.ClientId;
jobs.JobRate = jobname.JobRate;
jobs.Status = "Active";
}
}
}
And here is my SQL Query
SELECT TOP (200) Id, Job, ClientName, ClientId, JobRate, Status
FROM Jobs
WHERE (Id = 2)
It should return some value in it.
Both inner expressions of the ifs(ones controlling item.StartDate) are identical, so your problem lies in these controls. Can you make sure your code evaluates true for below condition?
if (item.StartDate.Day.ToString() == DateTime.Now.Day.ToString())
What I have done wrong in this code ? (I am using MVC4 and EF)
As an example: Please clear this am fresher to use MVC4
EditResponse response = new EditResponse();
try
{
using (WeMatchContext db = new WeMatchContext())
{
B_MEMBER_REGISTER update = new B_MEMBER_REGISTER();
var output = db.B_MEMBER_REGISTER.Where(x => x.MEMBER_ID == model.MEMBER_ID).FirstOrDefault();
if(output != null )
{
update.FIRST_NAME = model.FIRST_NAME;
update.LAST_NAME = model.LAST_NAME;
update.GENDER = model.GENDER;
update.DOB = model.DOB;
int resultcount = db.SaveChanges();
if (resultcount > 0)
{
response.MEMBER_ID = update.MEMBER_ID;
response.ResultCode = 0;
response.Message = "Updated Successfully";
}
You have to attach updated data with the db entity. please try this,
using (WeMatchContext db = new WeMatchContext())
{
var update = db.B_MEMBER_REGISTER.Where(x => x.MEMBER_ID == model.MEMBER_ID).FirstOrDefault();
if(update != null )
{
update.FIRST_NAME = model.FIRST_NAME;
update.LAST_NAME = model.LAST_NAME;
update.GENDER = model.GENDER;
update.DOB = model.DOB;
//below line of code is very important.
db.B_MEMBER_REGISTER.Attach(update);
db.Entry(update).State = EntityState.Modified;
int resultcount = db.SaveChanges();
if (resultcount > 0)
{
response.MEMBER_ID = update.MEMBER_ID;
response.ResultCode = 0;
response.Message = "Updated Successfully";
}
}
}
I have the following action
public ActionResult ViewModelProducts()
{
var listOfProductsBuy = db.ProductsBuy.Select(x => new ProductBuyViewModel
{
Id = x.Id,
Title = x.Title,
ProductBuyId = x.Id,
Author = x.Author,
MasterImageUrl = x.Images.FirstOrDefault().Url,
Price = x.Price,
Values = x.Value,
}).ToList();
var listOfProductsRent = db.ProductsRent.Select(y => new ProductRentViewModel
{
Id = y.Id,
Title = y.Title,
MasterImageUrl = y.ImagesRent.FirstOrDefault().Url,
ProductRentId = y.Id,
Author = y.Author,
Period = y.Period.PeriodOfTime,
Price = y.Price,
Values = y.Value,
}).ToList();
var listOfProductsSearch = db.ProductSearches.Select(z => new ProductSearchViewModel
{
Id = z.Id,
Title = z.Title,
ProductSearchId = z.Id,
Author = z.Author,
MasterImageUrl = z.ImagesSearch.FirstOrDefault().Url,
Price = z.Price,
Values = z.Value,
}).ToList();
var viewModel = new AllProductsViewModel { ProductBuy = listOfProductsBuy, ProductRent = listOfProductsRent, ProductSearch = listOfProductsSearch};
return View(viewModel);
}
I want to implement paging for more than one viewmodel. If there is way with PagedList it will be better, but if there is not it will be ok too.
In order to add paging, you can install PagedList.MVC Nuget package. You can see the exact step by step procedure to add it on Microsoft website here. Check out the section named: "Add Paging to the Students Index Page". Hope this helps.
I have the code below, run it in LinqPad 4.40.03 + Sql Server 2008 R2 + NorthWind.
LinqPad return exception: "ArgumentNullException: Value cannot be null. Parameter name: httpContext".
Please give me the Final Fixed code, I want it populate a chart ( Using System.Web.Helpers.Chart ) in linqpad output screen.
void Main()
{
var q = from p in Products
let s = p.OrderDetails.Sum(o => o.Quantity) * p.UnitPrice
orderby s
select new { ProductName = p.ProductName, Sales = s};
var basicChart = new System.Web.Helpers.Chart(width: 600, height: 400)
.AddTitle("Product Sales")
.DataBindTable(dataSource: q, xField: "ProductName")
.Write();
basicChart.Dump();
}
The ASP.NET charting control relies on HttpContext.Current which is present only when running an ASP.NET application.
You could try mocking an HttpContext, or else use the charting control in System.Windows.Forms.DataVisualization.Charting instead:
var q = from p in Products
let s = p.OrderDetails.Sum(o => o.Quantity) * p.UnitPrice
orderby s
select new { ProductName = p.ProductName, Sales = s};
var chart = new Chart();
var ca = new ChartArea();
ca.AxisX.LabelStyle.Interval = 1;
chart.ChartAreas.Add (ca);
var series = new Series { ChartType = SeriesChartType.Bar};
series.Points.DataBind (q.Take(20), "ProductName", "Sales", null);
chart.Series.Add (series);
chart.Dump ("Chart");
I have created a code to find out the current logged in user belongs to a particular group or not but I am getting an error saying that
IsCurrentUserMemberOfGroup(gName1); contains some invalid arguments.
Please help ?
Here is my code
SPSite oSite = (SPSite)properties.OpenWeb().Site;
SPWeb oWeb = oSite.OpenWeb();
SPGroupCollection myGroup = oWeb.Groups;
foreach (SPGroup gName in myGroup)
{
//string gname1 = gName.ToString();
SPGroup gName1 = gName;
if (gName1.Name == "Final Approval Group")
{
//SPGroup lGroup = oWeb.Groups["Final Approval Group"];
bool answer = oWeb.IsCurrentUserMemberOfGroup(gName1);
fValue = 1;
break;
}
}
Try using gName1.ID since IsCurrentUserMemberOfGroup takes an int argument.
SPGroupCollection myGroup = oWeb.Groups;
foreach (SPGroup gName in myGroup) {
//string gname1 = gName.ToString();
SPGroup gName1 = gName;
if (gName1.Name == "Final Approval Group") {
//SPGroup lGroup = oWeb.Groups["Final Approval Group"];
bool answer = oWeb.IsCurrentUserMemberOfGroup(gName1.ID);
fValue = 1; break;
}
}