Hi iam try to Get Database data and showed in RadTreeview,
here i got one sample,that sample like default sample,every Root and Child has to be enter manually,
but i want bind from database,values are filled automatically(root/child)
i will show that example code,
List<SiteDataItem> siteData = new List<SiteDataItem>();
siteData.Add(new SiteDataItem(1, 0, "Products"));
siteData.Add(new SiteDataItem(2, 1, "RadControls for ASP.NET Ajax"));
siteData.Add(new SiteDataItem(3, 1, "RadControls for Silverlight"));
siteData.Add(new SiteDataItem(4, 2, "RadGrid"));
siteData.Add(new SiteDataItem(5, 2, "RadScheduler"));
siteData.Add(new SiteDataItem(6, 2, "RadEditor"));
siteData.Add(new SiteDataItem(7, 3, "RadGrid"));
siteData.Add(new SiteDataItem(8, 3, "RadMenu"));
siteData.Add(new SiteDataItem(9, 3, "RadEditor"));
RadTreeView1 .DataTextField = "Text";
RadTreeView1.DataFieldID = "ID";
RadTreeView1.DataFieldParentID = "ParentID";
RadTreeView1.DataSource = siteData;
RadTreeView1.DataBind();
Please help me to do as i want
Please try with the below code snippet.
protected void Page_Load(object sender, System.EventArgs e)
{
List<TestModels> models = new List<TestModels>();
models.Add(new TestModels() { ID = 1, ParentID = null, Text = "a1" });
models.Add(new TestModels() { ID = 2, ParentID = 1, Text = "a2" });
models.Add(new TestModels() { ID = 3, ParentID = 2, Text = "a3" });
models.Add(new TestModels() { ID = 4, ParentID = null, Text = "a4" });
models.Add(new TestModels() { ID = 5, ParentID = 4, Text = "a5" });
RadTreeView1.DataTextField = "Text";
RadTreeView1.DataFieldID = "ID";
RadTreeView1.DataFieldParentID = "ParentID";
RadTreeView1.DataSource = models;
RadTreeView1.DataBind();
}
Class
public class TestModels
{
public int ID { get; set; }
public int? ParentID { get; set; }
public string Text { get; set; }
}
Please Set NULL in ParentID in place-of 0.
Because It consider 0 as value. RadTreeView is not able to found any ID field which have 0 value.
You can try something like this.
DataTable data= GetDataFromDatabase()
RadTreeView1.DataTextField = "TextFieldwhichyouwantshow";
RadTreeView1.DataFieldID = "IDFromdatabase";
RadTreeView1.DataFieldParentID = "ParentIDFromDatabase";
RadTreeView1.DataSource = data;
RadTreeView1.DataBind();
Related
Ok I have the following extensions I am checking in my code.
FileAttachments.FileAttachmentTypes fileAttachmentType = FileAttachments.FileAttachmentTypes.None;
The strings below in the conts are helled in a constants file
public const string wordExtensions = "docx;docm;doc";
public const string imageExtensions = "gif;tiff;tif;png;eps;raw;bmp;jpeg;jpg";
public const string excelExtensions = "csv;xml;xls;xlsb;xlsm;xlsx";
Which is then parsed down here
I am getting my extension from the File Info class as such
foreach (var fileAttachments in FormFile) {
if (fileAttachments.Length > 0) {
string filePath = Path.Combine(hostingEnvironment.WebRootPath, "Uploads");
var tennantId = GetCurrentTennantId().Result;
var settings = _context.SystemSetup.FirstOrDefault().UploadFolderPath;
string uniqueFilename = Guid.NewGuid().ToString() + "_" + fileAttachments.FileName;
string savedFileName = Path.Combine(filePath, uniqueFilename);
FileInfo infoFile = new FileInfo(savedFileName);
string extension = infoFile.Extension.Replace(".","");
Here I check the file extension Parameter above in the semi colan separated strings
if (extension.Contains(Constants.imageExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Image;
} else if (extension.Contains("pdf")) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.PDF;
} else if (extension.Contains(Constants.videoFormats)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Video;
} else if (extension.Contains(Constants.excelExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Excel;
} else if (extension.Contains(Constants.wordExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Word;
} else if (extension.Contains(Constants.audioExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Voice;
}
}
Types is just an area for categorizing them into tabs for front end display
public enum FileAttachmentTypes {
[Display(Name = "Microsoft Word")]
Word = 1,
[Display(Name = "Microsoft Excel")]
Excel = 2,
[Display(Name = "Image")]
Image = 3,
[Display(Name = "Voice Recordings")]
Voice = 4,
[Display(Name = "PDF")]
PDF = 5,
[Display(Name = "Video")]
Video = 6,
[Display(Name = "Evidence")]
Evidence = 7,
[Display(Name = "None")]
None = 8,
[Display(Name = "Notes")]
NOTES = 9,
[Display(Name = "Shared Case")]
SharedCase = 10,
[Display(Name = "Created Case")]
CreatedCase = 11
}
My Question is why is it not finding it for a word document of extension docx when its clearly there
I have written a test for edit model of a class in EF core.
public async Task<Expense> EditExpense(Expense model)
{
var expense = _dataRepository.Where<Expense>(x => x.Id == model.Id).ToList();
if(expense != null)
{
expense.ForEach(x =>
{
x.Name = model.Name;
x.AddedOn = model.AddedOn;
x.Amount = model.Amount;
x.Type = model.Type;
x.SplitOption = model.SplitOption;
x.Notes = model.Notes;
x.IsGroupExpense = model.IsGroupExpense;
});
return expense.FirstOrDefault();
}
else
{
return null;
}
}
I want to test this method using xUnit and Moq and I have also written a method for it as below.
[Fact]
public async void UnitTest_IsExpenseEdited_ReturnsTrue()
{
var expenseCurrent = new Expense()
{
Id = 5,
Name = "Test Expense",
Amount = 1500,
AddedBy = "44db32c3-ad6a-4d68-a683-862be363f59c",
AddedOn = DateTime.Now,
Notes = "",
IsDeleted = false,
IsGroupExpense = false,
SplitOption = 1,
Type = 0
};
var expenseList = (new List<Expense>{ expenseCurrent });
var expenseAfterEdit = new Expense()
{
Id = 5,
Name = "Test Expense Edited",
Amount = 2000,
AddedBy = "44db32c3-ad6a-4d68-a683-862be363f59c",
AddedOn = DateTime.Now,
Notes = "Edited !!!",
IsDeleted = false,
IsGroupExpense = true,
SplitOption = 2,
Type = 0
};
_dataRepositoryMock.Setup(x => x.Where<Expense>(a => a.Id == expenseCurrent.Id)).Returns(expenseList as IQueryable<Expense>);
var expenseEdited = await _exepenseRepository.EditExpense(expenseAfterEdit);
Assert.Equal(expenseEdited, expenseAfterEdit);
}
But here, the Where method is returning null
public async Task<Expense> EditExpense(Expense model)
{
var expense = _dataRepository.Where<Expense>(x => x.Id == model.Id).ToList(); in repository
}
I am getting var expense null in above code.
Please suggest what should I include in the code to get the value in above var expense?
To test this method, I want to create a fake data which is going to be updated. Please suggest how this thing needs to be written properly?
I want to insert 3 value of radio button into Database using MVC.
User need to select one material for each categories(Walls,Roof,Floor)
Currently the user can only select one value (may need to do grouping).But when I do grouping only the structInfo value is inserted into database. I need all the 3 value inserted into database.
This is how the database design look like
the struct inf(walls,roof,floor) the materialinfo is (bricks.concrete,woods, etc)
So can I make all the 3 value choosed by user save into database?
This is my view
#foreach (var structIN in Model.structInfo)
{
if (structIN.structId.Equals(1))
{
#Html.Label(structIN.structNm) #:
foreach (var material in Model.materialInfo)
{
if (material.materialId.Equals(1) || material.materialId.Equals(2) || material.materialId.Equals(3))
{
#Html.RadioButtonFor(model => model.buildInfo.materialId, material.materialId)#Html.Label(material.materialNm)
#Html.HiddenFor(model => model.buildInfo.structId, new { Value = structIN.structId })
}
}
}
else if(structIN.structId.Equals(2))
{
<br />
#Html.Label(structIN.structNm) #:
foreach (var material2 in Model.materialInfo)
{
if (material2.materialId.Equals(2) || material2.materialId.Equals(4) || material2.materialId.Equals(5))
{
#Html.RadioButtonFor(model2 => model2.buildInfo.materialId, material2.materialId)#Html.Label(material2.materialNm)
#Html.HiddenFor(model2 => model2.buildInfo.structId, new { Value = structIN.structId })
}
}
}
else if (structIN.structId.Equals(3))
{
<br />
#Html.Label(structIN.structNm) #:
foreach (var material3 in Model.materialInfo)
{
if (material3.materialId.Equals(6) || material3.materialId.Equals(3))
{
#Html.RadioButtonFor(model3 => model3.buildInfo.materialId, material3.materialId) #Html.Label(material3.materialNm)
#Html.HiddenFor(model3 => model3.buildInfo.structId, new { Value = structIN.structId })
}
}
}
}
my GET method
[HttpGet]
public ActionResult RegisterForm()
{
PopulateStructMaterialData();
using (var dataBase = new TMXEntities())
{
var model = new RegisterInfoPA()
{
//OTHER CODES
};
return View(model);
}
}
Populating Data
private void PopulateStructMaterialData()
{
var list = new List<strucMaterial>
{
new strucMaterial{ifOthers = "", materialId = 1, materialNm = "Bricks", structId = 1, structNm = "Walls", insuranceReqId = 0, isSelected = false},
new strucMaterial{ifOthers = "", materialId = 2, materialNm = "Concrete", structId = 1, structNm = "Walls", insuranceReqId = 0, isSelected = false},
new strucMaterial{ifOthers = "", materialId = 3, materialNm = "Woods", structId = 1, structNm = "Walls", insuranceReqId = 0, isSelected = false},
new strucMaterial{ifOthers = "", materialId = 2, materialNm = "Concrete", structId = 2, structNm = "Roof", insuranceReqId = 0, isSelected = false},
new strucMaterial{ifOthers = "", materialId = 4, materialNm = "Tiles", structId = 2, structNm = "Roof", insuranceReqId = 0, isSelected = false},
new strucMaterial{ifOthers = "", materialId = 5, materialNm = "Zinc", structId = 2, structNm = "Roof", insuranceReqId = 0, isSelected = false},
new strucMaterial{ifOthers = "", materialId = 3, materialNm = "Woods", structId = 3, structNm = "Floor", insuranceReqId = 0, isSelected = false},
new strucMaterial{ifOthers = "", materialId = 6, materialNm = "Reinforced Concrete", structId = 3, structNm = "Floor", insuranceReqId = 0, isSelected = false},
};
ViewBag.populatebuilding = list;
}
In View
List<Insurance.ViewModels.strucMaterial> viewModelSM = ViewBag.populatebuilding;
for(int i=0; i<viewModelSM.Count; i++)
{
#Html.DisplayFor(m => viewModelSM[i].structNm)
#Html.HiddenFor(m => viewModelSM[i].structId)
#Html.CheckBoxFor(m => viewModelSM[i].isSelected)
#Html.HiddenFor(m => viewModelSM[i].materialId)
#Html.Label(viewModelSM[i].materialNm)
}
My POST method
[HttpPost]
public ActionResult RegisterForm(RegisterInfoPA viewmodel, List<strucMaterial> list)
{
using (var dataBase = new TMXEntities())
{
var model = new RegisterInfoPA()
{
//OTHER CODES
};
if (ModelState.IsValid)
{
//OTHER CODES
var register = viewmodel.reg;
var personalinfo = viewmodel.pinfo;
//Save Register
db.registers.Add(register);
db.SaveChanges();
//Retriving required Id's
var getid = register.registrationId;
var getRegTypeID = register.regisTypeId;
//SAVE PERSONAL INFO
personalinfo.registrationId = getid;
db.personalInfoes.Add(personalinfo);
db.SaveChanges();
//---HOW SHOULD I SAVE THE CHECKBOX HERE?-----------
**> tHIS IS MY CODE, BUT IT IS NOT WORKING**
foreach(var item in list) (<< error starts here)
{
buildingInfo.materialId = item.materialId;
buildingInfo.structId = item.structId;
buildingInfo.insuranceReqId = item.insuranceReqId;
db.buildingInfoes.Add(buildingInfo);
}
db.SaveChanges();
}
}
}
I am always getting this error
System.NullReferenceException: Object reference not set to an instance of an object.
How the proper code should look like? Thank you.
I have following data & need to know if this can be displayed with line chart or not.
Data :
VerNo | Start Date | End Date
1.1 | 01-Jan-2013 | 31-Jan-2013
1.2 | 01-Feb-2013 | 31-Dec-2099
2.1 | 10-Jan-2013 | 25-Jan-2013
2.2 | 26-Jan-2013 | 16-Feb-2013
3.1 | 16-Mar-2013 | 30-Apr-2013
I need a line chart with dates in X-axis & VerNo in Y-axis & horizontal line should display start & end date of each version.
Thanks!!!
With my little knowledge of the Chart control I tried something.
First I bind the data with Id=0 on Y-axis to get the dates (can probably done better)
Then I looped over the data and made a serie per row.
Every serie I made a random color, but sometimes the color is too white, so it doesn't show.
public class VersionData
{
public int Id { get; set; }
public double VersionNo { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
string dateFormat = "yyyy MMM dd";
List<VersionData> version = new List<VersionData>();
version.Add(new VersionData() { Id=0, VersionNo = 1.1, StartDate = new DateTime(2013, 1, 1), EndDate = new DateTime(2013, 1, 31) });
version.Add(new VersionData() { Id=0, VersionNo = 1.2, StartDate = new DateTime(2013, 2, 1), EndDate = new DateTime(2013, 12, 31) });
version.Add(new VersionData() {Id=0, VersionNo = 2.1, StartDate = new DateTime(2013, 1, 10), EndDate = new DateTime(2013, 1, 25) });
version.Add(new VersionData() {Id=0, VersionNo = 2.2, StartDate = new DateTime(2013, 1, 26), EndDate = new DateTime(2013, 2, 16) });
version.Add(new VersionData() { Id=0, VersionNo = 3.1, StartDate = new DateTime(2013, 3, 16), EndDate = new DateTime(2013, 4, 30) });
Chart1.Series[0].YValueMembers = "Id";
Chart1.DataSource = version;
Random randomGen = new Random();
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
for (int i = 0; i < version.Count; i++)
{
Series s = new Series("s" + i.ToString());
s.ChartType = SeriesChartType.Line;
s.Color = Color.FromKnownColor(names[randomGen.Next(names.Length)]);
s.BorderWidth = 4;
Chart1.Series.Add(s);
DataPoint p = new DataPoint();
p.SetValueXY(version[i].StartDate, version[i].VersionNo);
s.Points.Add(p);
DataPoint p2 = new DataPoint();
p2.SetValueXY(version[i].EndDate, version[i].VersionNo);
s.Points.Add(p2);
}
Chart1.Series[0].XValueMember = "StartDate";
Chart1.ChartAreas[0].AxisX.Interval = 1;
Chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months;
Chart1.ChartAreas[0].AxisX.LabelStyle.Format = dateFormat;
Chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
Chart1.ChartAreas[0].AxisY.Interval = 0.5;
Chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0.0";
}
I'm trying to create a self referencing object using linqTOsql mapping. So far, I am definitely in over my head. Here's the code I have:
[Table]
public class Category
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 catID { get; set; }
public Int64 parentCatID { get; set; }
public string catName { get; set; }
public string catDescription { get; set; }
internal EntityRef<IEnumerable<Category>> _category;
[Association(ThisKey = "parentCatID", Storage = "_category")]
public IEnumerable<Category> category {
get { return _category.Entity; }
set { _category.Entity = value; }
}
}
My fakeRepository is defined like this:
// Fake hardcoded list of categories
private static IQueryable<Category> fakeCategories = new List<Category> {
new Category { catID = 1, parentCatID = 0, catName = "Root", catDescription = "" },
new Category { catID = 2, parentCatID = 1, catName = "Category w/subs", catDescription = "" },
new Category { catID = 3, parentCatID = 1, catName = "Category no subs but now has subs", catDescription = "" },
new Category { catID = 4, parentCatID = 2, catName = "Zub Cat", catDescription = "" },
new Category { catID = 5, parentCatID = 2, catName = "Sub Cat", catDescription = "" },
new Category { catID = 6, parentCatID = 0, catName = "Another Root", catDescription = "" },
new Category { catID = 7, parentCatID = 0, catName = "Ze German Root", catDescription = "" },
new Category { catID = 8, parentCatID = 3, catName = "Brand new cats", catDescription = "" },
new Category { catID = 9, parentCatID = 8, catName = "Brand new cats sub", catDescription = "" },
}.AsQueryable();
I pass Category to the view like this:
public ActionResult CategoryTree()
{
IQueryable<Category> cats = genesisRepository.Category
.Where(x => x.parentCatID == 0)
.OrderBy(x => x.catName);
return View(cats);
}
The problem that I'm running into is that all of this compiles, but I don't get anything beyond the root categories. Model[0].category is returning null.
What is wrong with my self-referencing object?
Edit
I wonder if it's not working because I don't have a real linq-to-sql data context in my fakeRepository. If that's the case, is there a way around that? Can I can get this to work without a connection to a database?
Yeah, you hit the nail on the head. It's not working because you're using a fake repository.
Linq-to-Sql does all the wiring up for you and sets the related collections based on the properties (& their attributes) that you setup in your model.
I don't know how to accomplish this without a connection to the database because internal EntityRef<IEnumerable<Category>> _category; is completely foreign to me - I'm more of a POCO model type of guy.
After a quick google, I found this - How to: Map Database Relationships (LINQ to SQL)
Could you change your model to read:
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 CatId { get; set; }
[Column]
public Int64 ParentCatId { get; set; }
[Column]
public string CatName { get; set; }
[Column]
public string CatDescription { get; set; }
private EntitySet<Category> _ChildCategories;
[Association(Storage = "_ChildCategories", OtherKey = "ParentCatId")]
public EntitySet<Category> ChildCategories
{
get { return this._ChildCategories; }
set { this._ChildCategories.Assign(value); }
}
private EntityRef<Category> _ParentCategory;
[Association(Storage = "_ParentCategory", ThisKey = "ParentCatId")]
public Category ParentCategory
{
get { return this._ParentCategory.Entity; }
set { this._ParentCategory.Entity = value; }
}
Now because your ChildCategories is of type EntitySet<Category> (which inherits from IList<T>) you should be able to wire fake relationships up yourself.
So you could do something like this:
private static IQueryable<Category> GetFakeCategories()
{
var categories = new List<Category> {
new Category { CatId = 1, ParentCatId = 0, CatName = "Root", CatDescription = "" },
new Category { CatId = 2, ParentCatId = 1, CatName = "Category w/subs", CatDescription = "" },
//Blah
new Category { CatId = 8, ParentCatId = 3, CatName = "Brand new cats", CatDescription = "" },
new Category { CatId = 9, ParentCatId = 8, CatName = "Brand new cats sub", CatDescription = "" }
};
//Loop over the categories to fake the relationships
foreach (var category in categories)
{
category.ChildCategories = new EntitySet<Category>(); //new up the collection
foreach (var subLoopCategory in categories)
{
if (category.ParentCatId == subLoopCategory.CatId)
category.ParentCategory = subLoopCategory;
if (category.Id == subLoopCategory.ParentCatId)
category.ChildCategories.Add(subLoopCategory);
}
}
return categoies.AsQueryable();
}
It works in my head at least... :-)
HTHs,
Charles
EDIT: Re: Comment below about a null reference on _childCategories.
You could change the model to look like:
private EntitySet<Category> _ChildCategories = new EntitySet<Category>();
It is supposed to be null. You are getting all categories where the ParentId = 0 ... and you don't have a child with an Id of 0. So that seems right to me.
It is not showing any subcategories because it has no subcategories to show. Try this:
IQueryable<Category> cats = genesisRepository.Category
.Where(x => x.parentCatID != 0)
.OrderBy(x => x.catName);
The parentCatId needs to point to a valid CatId for it to be a subcategory. This query should get you all the categories that are subcategories.