I have a Kendo Grid which have a datetime columns, while fetching the date from database we are getting correct date but during display Date gets changed. For ex:
DB date is 07/06/2017 but while display on website it gets changed into 06/06/2017, considering MM/DD/YYYY. Could anyone please help me on this. Our DB and Website both located in new york region.
The Kendo UI DataSource uses JavaScript Date objects for dates. These objects are always in the client's time zone, which may lead to changes in the date. A possible option is to use UTC dates:
http://docs.telerik.com/aspnet-mvc/helpers/grid/how-to/editing/utc-time-on-both-server-and-client
Edit, just copy the content of the link to here, because SO does not like the link the only answer
Use a ViewModel with a setter and a getter that explicitly set the DateTime Kind to UTC.
private DateTime birthDate;
public DateTime BirthDate
{
get { return this.birthDate; }
set {
this.birthDate = new DateTime(value.Ticks, DateTimeKind.Utc);
}
}
Use the requestEnd event of the DataSource to intercept and replace the incoming Date field with the time difference.
#(Html.Kendo().Grid<KendoUIMVC5.Models.Person>().Name("persons")
.DataSource(dataSource => dataSource
.Ajax()
.Events(ev=>ev.RequestEnd("onRequestEnd"))
)
// ...
)
<script>
var onRequestEnd = function(e) {
if (e.response.Data && e.response.Data.length) {
var data = e.response.Data;
if (this.group().length && e.type == "read") {
handleGroups(data);
} else {
loopRecords(data);
}
}
}
function handleGroups(groups) {
for (var i = 0; i < groups.length; i++) {
var gr = groups[i];
offsetDateFields(gr); //handle the Key variable as well
if (gr.HasSubgroups) {
handleGroups(gr.Items)
} else {
loopRecords(gr.Items);
}
}
}
function loopRecords(persons) {
for (var i = 0; i < persons.length; i++) {
var person = persons[i];
offsetDateFields(person);
}
}
function offsetDateFields(obj) {
for (var name in obj) {
var prop = obj[name];
if (typeof (prop) === "string" && prop.indexOf("/Date(") == 0) {
obj[name] = prop.replace(/\d+/, function (n) {
var offsetMiliseconds = new Date(parseInt(n)).getTimezoneOffset() * 60000;
return parseInt(n) + offsetMiliseconds
});
}
}
}
</script>
Related
I have the below requirement.
I need to perform the sum of each field across multiple records of the same entity However while performing the sum, I also need to check the type and cast them accrodingly. For eg, For whole number cast to Int, For Decimal cast to decimal. Also some of the values are aliased value too. I am looking for a generic function which I can call for both alias fields and direct fields and it will return me the value based on the type
Background on the code written below -
Attribute List is the list of all attributes that belong to the
entity.
Format in which the field values are stored in AttributeList-
AttributeList = { "price ", "quantity", "contact.revenue", "opportunity.sales"}
price, quantity - fields of main entity on which we are querying
contact.revenue, opportunity.sales - fields of the aliased entities,
entity name is appended to understand which entity's field it is
Below is the code which i have tried so far -
I only have decimal and whole number fields in my attributeList.
private void calculate(List<string> attributeList,List<Entity> mainEntityList,Guid targetId,Guid oppId,Guid contactId)
{
var mainentity = new mainEntity();
mainentity.Id = targetId;
var opportunity = new Opportunity();
opportunity.Id = oppId;
var contact = new Contact();
contact.Id = contactId;
foreach (var attribute in attributeList)
{
var fieldSum = new decimal(0);
int intFieldSum = 0;
bool attributeFound = false;
foreach (var entity in mainEntityList)
{
if (entity.Contains(attribute))
{
var type = entity[attribute].GetType().Name;
attributeFound = true;
switch (type)
{
case "AliasedValue":
var aliasedFieldValue = entity.GetAttributeValue<AliasedValue>(attribute);
if (aliasedFieldValue.Value.GetType().Name == "Decimal")
{
decimalFieldSum += (decimal)aliasedFieldValue.Value;
}
else
{
intFieldSum += (int)aliasedFieldValue.Value;
}
break;
case "Decimal":
decimalFieldSum += entity.GetAttributeValue<decimal>(attribute);
break;
case "Int32":
intFieldSum += entity.GetAttributeValue<int>(attribute);
break;
default:
break;
}
}
}
if (attributeFound)
{
if (attribute.Contains("opportunity"))
{
opportunity[attribute] = decimalFieldSum != 0 ? decimalFieldSum : intFieldSum;
}
else if (attribute.Contains("contact"))
{
contact[attribute] = decimalFieldSum != 0 ? decimalFieldSum : intFieldSum;
}
else
{
mainentity[attribute] = decimalFieldSum != 0 ? decimalFieldSum : intFieldSum;
}
}
}
service.update(opportunity);
service.update(contact);
service.update(mainentity);
}
Any help would be appreciated.
Just a little bit edited your code.
...
var fieldSum = new decimal(0);
foreach (var entity in mainEntityList)
{
fieldSum += GetAttrValue(entity, attribute);
}
...
You can use this function to calculate fieldSum variable which is of decimal type.
private decimal GetAttrValue(Entity entity, string attribute)
{
var attrValue = new decimal(0);
if (!entity.Contains(attribute) || entity.Attributes[attribute] == null)
{
return attrValue;
}
var type = entity.Attributes[attribute].GetType().Name;
switch (type)
{
case "AliasedValue":
var aliasedFieldValue = entity.GetAttributeValue<AliasedValue>(attribute);
attrValue = type == "Decimal" ? (decimal)aliasedFieldValue.Value : (int)aliasedFieldValue.Value;
break;
case "Decimal":
attrValue = entity.GetAttributeValue<decimal>(attribute);
break;
case "Int32":
attrValue = entity.GetAttributeValue<int>(attribute);
break;
default:
break;
}
return attrValue;
}
On the other hand if you just need a generic function which will return decimal or int value for an attribute you can use this
private T GetAttrValue<T>(Entity entity, string attribute)
{
if (!entity.Contains(attribute) || entity.Attributes[attribute] == null)
{
return default(T);
}
T result;
var type = entity.Attributes[attribute].GetType().Name;
if (type == "AliasedValue")
{
var aliasedFieldValue = entity.GetAttributeValue<AliasedValue>(attribute);
result = (T)aliasedFieldValue.Value;
}
else
{
result = entity.GetAttributeValue<T>(attribute);
}
return result;
}
--Update--
So, here is the whole code if I understand you requirements right.
First of all add this class.
public class AttributeInfo
{
public string Name { get; set; }
public Type Type { get; set; }
public decimal DecimalSum { get; set; } = new decimal(0);
public int IntSum { get; set; } = 0;
}
And add this function
private void SetValue(Entity entity, AttributeInfo attributeInfo)
{
if (entity.Contains(attributeInfo.Name))
{
switch (attributeInfo.Type.Name)
{
case "Decimal":
entity[attributeInfo.Name] = attributeInfo.DecimalSum;
break;
case "Int32":
entity[attributeInfo.Name] = attributeInfo.IntSum;
break;
default:
break;
}
}
}
Then this is you Calculate function
private void Calculate(List<string> attributeList, List<Entity> mainEntityList, Guid targetId, Guid oppId, Guid contactId)
{
var mainentity = new mainEntity();
mainentity.Id = targetId;
var opportunity = new Opportunity();
opportunity.Id = oppId;
var contact = new Contact();
contact.Id = contactId;
var attributesInfo = new List<AttributeInfo>();
foreach (var attribute in attributeList)
{
var attributeInfo = new AttributeInfo
{
Name = attribute
};
foreach (var entity in mainEntityList)
{
if (entity.Contains(attribute))
{
attributeInfo.Type = entity[attribute].GetType();
switch (attributeInfo.Type.Name)
{
case "AliasedValue":
var aliasedFieldValue = entity.GetAttributeValue<AliasedValue>(attribute);
if (aliasedFieldValue.Value.GetType().Name == "Decimal")
{
attributeInfo.DecimalSum += (decimal)aliasedFieldValue.Value;
}
else
{
attributeInfo.IntSum += (int)aliasedFieldValue.Value;
}
break;
case "Decimal":
attributeInfo.DecimalSum += entity.GetAttributeValue<decimal>(attribute);
break;
case "Int32":
attributeInfo.IntSum += entity.GetAttributeValue<int>(attribute);
break;
default:
break;
}
}
}
attributesInfo.Add(attributeInfo);
}
foreach (var attributeInfo in attributesInfo)
{
if (attributeInfo.Type != null)
{
SetValue(mainentity, attributeInfo);
SetValue(opportunity, attributeInfo);
SetValue(contact, attributeInfo);
}
}
service.update(mainentity);
service.update(opportunity);
service.update(contact);
}
I should say that the structure of the calculate function still seems weird for me. However, here I tried to keep the main structure.
I Added the 2 Sync Times to my DB as 2 new columns and inserted values as below:
USE [DB]
ALTER TABLE [dbo].[TableName]
ADD ColumnName2 time, ColumnName3 time
This was for adding the columns.
For inserting the row values I did:
USE DB
INSERT INTO TableName (ColumnName2, ColumnName3)
VALUES ('20:30:00', '23:30:00')
This was the data for the fixed times in the rows of those columns.
I also went through all the layers of the application such as (controller, models, views, queries, services, interfaces, and so forth. NOW when I try to update any of the new times added they default to the first time that already existed on the table as a COLUMN with time in the row.
I could not post the image for the time fields from the application because it is not permitted. However, the image is in a little panel and consists of 3 fields (textboxfor) with a time picker for each one.
Any help would be greatly appreciated.
Thanks
Now I thought I would post some of the example code to see if this helps
// My controller method for those sync times
[HttpPost]
[Page(PageName.UpdateSystemConfigTime)]
public ActionResult UpdateTime(SystemMaintenanceViewModel model)
{
var dateTime = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime, "h:mm tt", CultureInfo.InvariantCulture);
var dateTime2 = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime2, "h:mm tt", CultureInfo.InvariantCulture);
var dateTime3 = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime3, "h:mm tt", CultureInfo.InvariantCulture);
//model.ProcessTime
if (model.SystemConfiguration.SynchronizationTime != null &&
model.SystemConfiguration.SynchronizationTime2 != null &&
model.SystemConfiguration.SynchronizationTime3 != null);
{
var sysConfig = new DTO.SystemSync.SystemConfiguration
{
SyncTime = dateTime.TimeOfDay,
SyncTime2 = dateTime2.TimeOfDay,
SyncTime3 = dateTime3.TimeOfDay
};
configService.UpdateSyncTime(sysConfig);
configService.UpdateSyncTime2(sysConfig);
configService.UpdateSyncTime3(sysConfig);
}
return RedirectToAction("Index");
}
////My Private method
private SystemConfiguration GetSystemConfig()
{
var model = new SystemConfiguration();
var config = configService.GetSyncTime();
configService.GetSyncTime2();
configService.GetSyncTime3();
if (config == null) return model;
var ts = config.SyncTime;
if (ts != null)
{
model.SynchronizationTime = ts.ToString();
}
var ts2 = config.SyncTime2;
if (ts2 != null)
{
model.SynchronizationTime2 = ts2.ToString();
}
var ts3 = config.SyncTime3;
if (ts3 != null)
{
model.SynchronizationTime3 = ts3.ToString();
}
return model;
============================================================================
/// My configuration command
namespace --.--.Commands
{
public class ConfigurationCommand : CommandBase, IConfigurationCommand
{
static ConfigurationCommand()
{
ConfigureAutoMapper();
}
private static void ConfigureAutoMapper()
{
Mapper.CreateMap<SystemConfiguration, entity.SystemConfiguration>()
.ForMember(dest => dest.SyncTime, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>())
.ForMember(dest => dest.SyncTime2, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>())
.ForMember(dest => dest.SyncTime3, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>());
}
public void UpdateSyncTime(SystemConfiguration timeOfDay)
{
Guard.NotNull(timeOfDay);
var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);
var config = Context.SystemConfigurations.SingleOrDefault();
//if this is the first time, then we need to insert
if (config == null)
{
var newConfig = new entity.SystemConfiguration
{
SyncTime = mapped.SyncTime
};
Context.SystemConfigurations.Add(newConfig);
}
else
{
config.SyncTime = mapped.SyncTime;
}
SaveChanges();
}
public void UpdateSyncTime2(SystemConfiguration timeOfDay)
{
Guard.NotNull(timeOfDay);
var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);
var config = Context.SystemConfigurations.SingleOrDefault();
if (config == null)
{
var newConfig = new entity.SystemConfiguration
{
SyncTime2 = mapped.SyncTime2
};
Context.SystemConfigurations.Add(newConfig);
}
else
{
config.SyncTime2 = mapped.SyncTime2;
}
SaveChanges();
}
public void UpdateSyncTime3(SystemConfiguration timeOfDay)
{
Guard.NotNull(timeOfDay);
var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);
var config = Context.SystemConfigurations.SingleOrDefault();
if (config == null)
{
var newConfig = new entity.SystemConfiguration
{
SyncTime3 = mapped.SyncTime3
};
Context.SystemConfigurations.Add(newConfig);
}
else
{
config.SyncTime3 = mapped.SyncTime3;
}
SaveChanges();
}
}
}
=========================================================================================================
// My configuration service
namespace --.--.--.SystemSync
{
public class ConfigurationService : IConfigurationService
{
private IConfigurationQuery query;
private IConfigurationCommand command;
public ConfigurationService(IConfigurationQuery query,IConfigurationCommand command)
{
this.query = query;
this.command = command;
}
public void UpdateSyncTime(SystemConfiguration timeOfDay)
{
command.UpdateSyncTime(timeOfDay);
}
public void UpdateSyncTime2(SystemConfiguration timeOfDay)
{
command.UpdateSyncTime2(timeOfDay);
}
public void UpdateSyncTime3(SystemConfiguration timeOfDay)
{
command.UpdateSyncTime3(timeOfDay);
}
public SystemConfiguration GetSyncTime()
{
return query.GetSyncTime();
}
public SystemConfiguration GetSyncTime2()
{
return query.GetSyncTime2();
}
public SystemConfiguration GetSyncTime3()
{
return query.GetSyncTime3();
}
public List<PageResource> GetPages()
{
return query.GetPages().ToList();
}
}
}
You made a comment about fixed times, Are you looking for something like this?
CREATE TABLE [dbo].[Zamen](
[Id] [int] IDENTITY(1,1) NOT NULL,
[time1] [time](3) NOT NULL,
[time2] [time](3) NOT NULL,
[Content] [varchar](100) NULL,
CONSTRAINT [PK_Zamen] PRIMARY KEY CLUSTERED
(
[Id] ASC
))
GO
ALTER TABLE [dbo].[Zamen] ADD CONSTRAINT [DF_Zamen_time1] DEFAULT (getdate()) FOR [time1]
GO
ALTER TABLE [dbo].[Zamen] ADD CONSTRAINT [DF_Zamen_time2] DEFAULT (getdate()) FOR [time2]
GO
Those alter table statements allow the time to be automatically inserted. So when you do this:
INSERT INTO Zamen (Content) VALUES ('demo')
The current times are placed into the values.
*After seeing the code you added, some input:
In your UpdateTime Action Method, a problem that stands out is you are calling UpdateTimeSync three times, but passing it all three variables each time. I would suggest to refactor your update method -- instead of three update methods, use one update method for all time variables.
I have following program that fetches the data from DB and sends it to Main. I am able to iterate through the result in the function but not in Main.
Program is below :
void Main()
{
var data = GetAllCountry();
// foreach( var t in data)
// {
// Console.WriteLine("{0}", t.country.ID); //fails here; says country not found
// }
}
// Define other methods and classes here
public IEnumerable GetAllCountry()
{
var countries = COUNTRY.Select(c => new
{
country = new
{
ID = c.ID,
Description = c.DESCRIPTION,
CountryPhoneCode = c.COUNTRY_PHONE_CODE,
Currency = c.CURRENCY.CURRENCY_SYMBOL,
}
});
foreach( var t in countries)
{
Console.WriteLine("{0}", t.country.ID);//works here and I am able to access t.country.ID here...
}
return countries;
}
What wrong with this ? what are required modifications ?
I believe as you are returning IEnumerable rather than IEnumerable<T>, it is not able to get the object type.
If you create a class for Country and the method returns IEnumerable<Country> it would work
public IEnumerable<Country> GetAllCountry()
{
var countries = COUNTRY.Select(c => new
{
country = new Country
{
ID = c.ID,
Description = c.DESCRIPTION,
CountryPhoneCode = c.COUNTRY_PHONE_CODE,
Currency = c.CURRENCY.CURRENCY_SYMBOL,
}
});
foreach( var t in countries)
{
Console.WriteLine("{0}", t.country.ID);//works here and I am able to access t.country.ID here...
}
return countries;
}
I am using RadControls for WinForms 2011 Q3
The datasource for a RadGridView is dynamically generated based on users' input/selection
Everytime when a datasource is generated, I will call SetDatasource2KeyValuesGrid()
What I expect to see is columns generated and values filled in gridview.
However what I see is columns generated but no value filled even though the number of rows in gridview match the number of items in its datasource(keyValuesList)
I must have missed something simple. Please help. thanks
Edit:
I create a DataTable from the list keyValueList, and then assign it to DataSource, then it works
Just wonder if there's a better way. thanks
private void CreateTableSetDatasource(List<FeedKeyValueOneSet>) keyValueList)
{
if(keyValueList==null) return;
var table = new DataTable();
table.Columns.Add("Check");
foreach (var feedKeyValueOneSet in keyValueList)
{
var oneset = feedKeyValueOneSet.KeyValueOneSet;
foreach (var oneKey in oneset)
{
table.Columns.Add(oneKey.key);
}
break;
}
foreach (var feedKeyValueOneSet in keyValueList)
{
var oneset = feedKeyValueOneSet.KeyValueOneSet;
var numOfCol = oneset.Length + 1;
var obj = new object[numOfCol];
obj[0] = "false";
int idx = 1;
foreach (var oneKey in oneset)
{
obj[idx] = oneKey.value;
idx++;
}
table.Rows.Add(obj);
}
radGridKeyValues.DataSource = table;
}
private void SetDatasource2KeyValuesGrid()
{
if (radGridKeyValues.Columns != null) radGridKeyValues.Columns.Clear();
radGridKeyValues.AutoGenerateColumns = false;
radGridKeyValues.EnableFiltering = false;
radGridKeyValues.ShowFilteringRow = false;
radGridKeyValues.ShowHeaderCellButtons = false;
radGridKeyValues.AllowDragToGroup = false;
radGridKeyValues.AllowAddNewRow = false;
radGridKeyValues.EnableGrouping = false;
var keyValueList = (List<FeedKeyValueOneSet>)TimeSeries.FeedValuesCache[m_strFeedName + "_KEYVALUES"];
if(keyValueList==null) return;
GridViewDataColumn checkBoxColumn = new GridViewCheckBoxColumn("columnState", "columnState");
checkBoxColumn.HeaderText = string.Empty;
if (radGridKeyValues.Columns != null) radGridKeyValues.Columns.Add(checkBoxColumn);
foreach (var feedKeyValueOneSet in keyValueList)
{
var oneset = feedKeyValueOneSet.KeyValueOneSet;
foreach (var oneKey in oneset)
{
var textboxCol = new GridViewTextBoxColumn(oneKey.key, oneKey.key);
textboxCol.Width = 150;
textboxCol.ReadOnly = true;
if (radGridKeyValues.Columns != null) radGridKeyValues.Columns.Add(textboxCol);
}
break;
}
radGridKeyValues.DataSource = keyValueList;
}
public class FeedKeyValueOneSet
{
public FeedFieldValues[] KeyValueOneSet;
}
public class FeedFieldValues
{
public string key { get; set; }
public string value { get; set; }
}
I create a DataTable from the list keyValueList, and then assign it to DataSource, then it works
see code in edit to the question
I am using the following code to get a record from my database table tblDatabases. I then populate Controls on a Form based on the value. I have used some functions to get the values needed for displaying in a text box (e.g. the display value is different than the value.
The DetailData is an object in my base form class. Initially I just got the record from the table as is and I was able to cast the DetailData to the tblDatabases and use reflection to get all of the values for the data and populate the controls on my form.
I am no longer able to cast the DetailData to my table because of the anonymous types.
I would like to be able to use reflection on the DetailData to get the values.
Thanks,
Brad
DetailData = (from db in priorityDataContext.tblDatabases
where db.DatabaseID == Id
select new
{
db.DatabaseID,
db.DatabaseName,
db.Purpose,
db.BackEnd,
db.FrontEnd,
db.Version,
db.ProducesReports,
db.MultiUser,
db.UserDescription,
Developer = priorityDataContext.func_get_employee_name(db.Developer),
DeptOwner = priorityDataContext.func_get_dept_name(db.DeptOwner),
db.Source_Code_Path,
db.Notes,
db.Active,
db.row_entry_time_stamp,
row_oper_name = priorityDataContext.func_get_employee_name(db.Developer),
db.row_last_chng_time_stamp,
row_last_chng_oper_name = priorityDataContext.func_get_employee_name(db.Developer)
}).SingleOrDefault();
protected virtual void PopulateDetailControlsA(List<Control> controlContainers, string srcDataTableName)
{
switch (srcDataTableName)
{
case "tblDatabase" :
break;
}
var database = (tblDatabase) DetailData;
var type = typeof(tblDatabase);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var controlContainer in controlContainers)
{
foreach (var propertyInfo in properties)
{
if (!ControlExists(controlContainer, propertyInfo.Name)) continue;
var txtExtControl = controlContainer.Controls[propertyInfo.Name] as ExtendedTextBox;
if (txtExtControl != null)
{
try
{
var value = propertyInfo.GetValue(database, null).ToString();
txtExtControl.Text = value;
}
catch (NullReferenceException)
{
}
continue;
}
var lnklblControl = controlContainer.Controls[propertyInfo.Name] as ExtendedLinkLabel;
if (lnklblControl != null)
{
try
{
var value = propertyInfo.GetValue(database, null).ToString();
lnklblControl.Text = value;
}
catch (NullReferenceException)
{
}
continue;
}
var chkControl = controlContainer.Controls[propertyInfo.Name] as ExtendedCheckBox;
if (chkControl != null)
{
try
{
var value = propertyInfo.GetValue(database, null).ToString();
switch (value)
{
case "True":
chkControl.CheckState = CheckState.Checked;
break;
case "False":
chkControl.CheckState = CheckState.Unchecked;
break;
}
}
catch (NullReferenceException)
{
chkControl.CheckState = CheckState.Indeterminate;
}
continue;
}
var cmbControl = controlContainer.Controls[propertyInfo.Name] as ExtendedComboBox;
if (cmbControl != null)
{
try
{
var value = propertyInfo.GetValue(database, null).ToString();
cmbControl.ValueMember = value;
}
catch (Exception ex)
{
}
continue;
}
}
}
}
What technology are you using for your UI? If you could use binding you would not need to worry about reflection on the anonymous type and then could also use converters if you need to format/calculate values from this.
From your response can't you just use the connection to the linq and then bind this to the combo box?
private void Form1_Load(object sender, System.EventArgs e)
{
var item = new DataClassesDataContext();
var stuff = item.Entity.Where(c => c.Property.Contains("something"));
comboBox1.DataSource = stuff;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "PIN";
}