I want to add the content of the combobbox into my database, but it doesn't work. The content of my combobox is from the table 'Categories' who's joined with the table 'Products'. I've tried many things I have errors of conversion :
Here's my code :
Product p = new Product();
p.ProductName = txtNom.Text.Trim();
p.ProductDescription = txtDesc.Text.Trim();
p.ProductQuantityUsed = Convert.ToInt32(numQteUsed.Value);
p.ProductQuantityNew = Convert.ToInt32(numQteNew.Value);
p.CategoryID = cboCat.SelectedText.ToString();
db.Products.Add(p);
db.SaveChanges();
//Combobox
public void FillCbCategories()
{
SamsonEntities db = new SamsonEntities();
cboCat.Items.Clear();
var listCat = (from cats in db.Categories
select new CategoryDisplay()
{
CategoryID = cats.CategoryID,
CategoryName = cats.CategoryName
}).ToList();
for(var i=0;i<listCat.Count;i++)
{
cboCat.Items.Add(listCat[i]);
}
}
Judging by your comments, your combobox is not binded correctly to data you send to it.
You could try setting ValueMember and DisplayMember:
cboCat.ValueMember = "CategoryID";
cboCat.DisplayMember = "CategoryName";
in your method, like this:
public void FillCbCategories()
{
SamsonEntities db = new SamsonEntities();
cboCat.Items.Clear();
var listCat = (from cats in db.Categories
select new CategoryDisplay()
{
CategoryID = cats.CategoryID,
CategoryName = cats.CategoryName
}).ToList();
for(var i=0;i<listCat.Count;i++)
{
cboCat.Items.Add(listCat[i]);
}
cboCat.ValueMember = "CategoryID";
cboCat.DisplayMember = "CategoryName";
}
Related
while filling a combobx, I need to convert a Linq-result to a viewmodel.
Actually, I query the records and then I fill a list of the viewmodel in a loop, but that seems to be a bit strange:
public static IEnumerable<ComboBoxActivities> GetActivitySelectList()
{
using(ApplicationDbContext db = new ApplicationDbContext())
{
var result = from activity in db.Activities
where activity.Available
select new
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
List<ComboBoxActivities> list = new List<ComboBoxActivities>();
foreach(var res in result)
{
ComboBoxActivities listItem = new ComboBoxActivities()
{
ActivityId= res.ActivityId,
ActivityName= res.ActivityName,
Available= res.Available
};
list.Add(listItem);
}
return list;
}
}
Is this really the right way?
I also tried:
var result = from activity in db.Activities
where activity.Available
select new ComboBoxActivities()
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
But then my razorview crashes with the message that direct binding to a quers (DbSet, DbQuery...) is not supported.
You can convert the IEnumerable<T> to a List<T> by using ToList()
public static List<ComboBoxActivities> GetActivitySelectList()
{
using(ApplicationDbContext db = new ApplicationDbContext())
{
var result = from activity in db.Activities
where activity.Available
select new ComboBoxActivities()
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
return result.ToList();
}
}
As for loading a ComboBox from a table query, ComboBox has a DataSource property which you can assign the List to.
i need to populate my articles ViewModel with a model that has the database data in it, but i have a method that i need to assign to one of my properties
The list of images is the property that needs the method on it.
The method is called once for every item in the list of articles.
Here is my code:
public ActionResult ArticleTypes(string at)
{
articleViewModel.Images = new List<ImageInfo>();
var query = (from a in db.Articles
where a.SelectedArticleType == at
select new ArticlesViewModel
{
Id = a.Id,
Body = a.Body,
Headline = a.Headline,
PostedDate = a.PostedDate,
SelectedArticleType = a.SelectedArticleType,
UserName = a.UserName,
}).ToList();
articleViewModel.Images = imageService.GetImagesForArticle(articlemodel.Id.ToString());
return View(query);
}
I have also tried putting the method inside the linq:
public ActionResult ArticleTypes(string at)
{
articleViewModel.Images = new List<ImageInfo>();
var query = (from a in db.Articles
where a.SelectedArticleType == at
select new ArticlesViewModel
{
Id = a.Id,
Body = a.Body,
Headline = a.Headline,
PostedDate = a.PostedDate,
SelectedArticleType = a.SelectedArticleType,
UserName = a.UserName,
Images = imageService.GetImagesForArticle(a.Id.ToString())
}).ToList();
return View(query);
}
it throws an exception of:
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[New_MinecraftNews_Webiste_MVC.Models.ImageInfo] GetImagesForArticle
I added a foreach loop at the end insted of anything else and it works:
public ActionResult ArticleTypes(string at)
{
articleViewModel.Images = new List<ImageInfo>();
var modelList = (from a in db.Articles
where a.SelectedArticleType == at
select new ArticlesViewModel
{
Id = a.Id,
Body = a.Body,
Headline = a.Headline,
PostedDate = a.PostedDate,
SelectedArticleType = a.SelectedArticleType,
UserName = a.UserName
}).ToList();
foreach (var model in modelList)
{
model.Images = imageService.GetImagesForArticle(model.Id.ToString());
}
return View(modelList);
}
I have anonymous type list. I want to get data from this list
My code is as bellow
var data = from EP in DbContext.EmployeeProfesionalDtls
join DM in DbContext.DesignationMasters on EP.DesigId equals DM.DesigId
join DP in DbContext.DepartmentMasters on new { DepId = EP.DepId } equals new { DepId = DP.DeptId }
select new
{
ProfId = EP.ProfId,
EmpId = EP.EmpId,
EntryDate = DM.EntryDate,
DeptName = DP.DeptName,
DepartmentUnitID = DP.UnitId,
DepartmentEntryDate = DP.EntryDate
}
"data" have anonymous type list. I wants to get data from this anonymous type list.
You have to take a view model i.e. a class to send it to view :
var data = from EP in DbContext.EmployeeProfesionalDtls
join DM in DbContext.DesignationMasters on EP.DesigId equals DM.DesigId
join DP in DbContext.DepartmentMasters on new { DepId = EP.DepId } equals new { DepId = DP.DeptId }
select new Test()
{
ProfId = EP.ProfId,
EmpId = EP.EmpId,
EntryDate = DM.EntryDate,
DeptName = DP.DeptName,
DepartmentUnitID = DP.UnitId,
DepartmentEntryDate = DP.EntryDate
}
where Test is a class with these properties.
I have a need to display a list of departments and sub-departments. I am loading the list of departments properly.
At this time, I have:
var deparmentList = Departments.LoadList();
var departments = new List<ListItem>(
from department in departmentList
select new ListItem {
Text = department.Name,
Value = department.Id.ToString(),
IsSelected = department.IsActive
}
);
I now need to load the list of sub-departments. Each sub-department has an Id, DepartmentId, and Name. However, I only want to get the sub-departments associated with departments that are selected. Currently, I have the following:
var subDepartmentList = SubDepartment.LoadList();
var subdepartments = new List<ListItem>(
from subdepartment in subDepartmentList
// where ?
select new ListItem {
Text = subdepartment.Name,
Value = subdepartment.Id.ToString(),
IsSelected = false
}
);
I'm not sure how to do the join or filter between the two. How do I do this in LINQ?
var selectedDepartmentSubDepartments =
from dep in departments
join subDep in subDepartmentList
on dep.Value equals subDep.Id.ToString()
where dep.IsSelected
select new ListItem {
Text = subDep.Name,
Value = subDep.Id.ToString(),
IsSelected = false
};
var subdepartments = new List<ListItem>(selectedDepartmentSubDepartments);
You can add a where clause that checks if at least one associated and 'selected' department exists:
var subDepartmentList = SubDepartment.LoadList();
var subdepartments = new List<ListItem>(
from subdepartment in subDepartmentList
where departments.Any(x => x.IsSelected &&
x.Value == subdepartment.DepartmentId.ToString())
select new ListItem {
Text = subdepartment.Name,
Value = subdepartment.Id.ToString(),
IsSelected = false
}
);
I have a method:
public IEnumerable<Company> Select()
{
DataClassesCompanyDataContext dc = new DataClassesCompanyDataContext();
System.Data.Linq.Table<Company> company = dc.GetTable<Company>();
Company com = new Company();
return dc.Companies.OrderBy(e => e.company_id);
}
By this I want to bind dropdown.
I am using this method:
private void fillCompanyNameDropdown()
{
PioneerDataAccess.CompanyClass comp = new PioneerDataAccess.CompanyClass();
var objcomp = comp.Select().ToList();
ddlCompanyOff.DataSource = objcomp[0].company_name;
ddlCompanyOff.DataBind();
}
But the problem is that if the company name is pulil then it shows
p
u
l
i
l
So what can I do??
Code for you...this will might resolve your issue
var objcomp = comp.Select().ToList();
ddlCompanyOff.DataSource = objcomp ;
ddlCompanyOff.DataTextField = "company_name";
ddlCompanyOff.DataValueField = "company_name";
ddlCompanyOff.DataBind();