I'm a beginner with dhtmlx scheduler, I use dbfirst with mvc4 razor syntax.
So please can anybody help me to set the start time and endtime values I'm getting from the database:
here is my code:
controller:
sched.Config.first_hour = 8;
sched.Config.last_hour = 19;
public string GetDbValues()
{
string strState = "";
List<tblSettings> settings = new List<tblSettings>();
settings = _config.GetSettings();
var defaultState = from setting in settings
where (setting.Desc == "start time"
|| setting.Desc == "end time")
// starttime as 08 and end time as 20 from database
select setting.Settings;
foreach (var oState in defaultState)
{strState += oState + "|";
}
strState = strState.Remove(strState.Length - 1);
return strState;
}
I get the values for first_hour and last_hour as string from the output.
How to assign this string value to the first_hour and last_hour?
//I use linq to get the db values because of the table structure:
ID(int), Settings(nvarchar) ,Desc (nvarchar)
why do you concatenate the settings into string? More comprehensive data format will simplify the task. You could return List as it is and then just iterate it with foreach, applying needed settings. If for some reason you don't want to pass tblSettings objects, you can select data into Dictionary and then again, apply the values
var data = _config.GetSettings().ToDictionary(
s => s.Desc,
s => s.Setting,
StringComparer.Ordinal);
if(data.ContainsKey("start time"))
scheduler.Config.first_hour = int.Parse(data["start time"]);
if (data.ContainsKey("end time"))
scheduler.Config.first_hour = int.Parse(data["end time"]);
Related
So in my application I extract data from a database and then extract it to a csv file and I say what I want under each columns like so:
public List<ExtractDocument> GetExtractDocuments(List<Guid>ItemDetailIds)
{
var items = GetExtractData(ItemDetailIds);
return items.Select().Select(item => new ExtractDocument
{
ItemDetailId = item.Field<Guid>("ItemDetailID"),
ItemNumber = item.Field<string>("Number"),
ItemTitle = item.Field<string>("Title"),
ItemRevision = item.Field<string>("RevisionNumber"),
ActionType = item.Field<string>("Action"),
ActionedBy = item.Field<string>("ActionedBy"),
ActionedDate = item.Field<DateTime?>("ActionedDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
Comment = item.Field<string>("Comment"),
TaskType = item.Field<string>("TaskType"),
StartDate = item.Field<DateTime?>("StartDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
CompletedDate = item.Field<DateTime?>("CompletedDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
Status = item.Field<string>("Status"),
Outcome = item.Field<string>("Outcome"),
ActionerName = item.Field<string>("TaskActionedBy"),
DateActioned = item.Field<DateTime?>("ActionDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
ActionTaken = item.Field<string>("TaskAction"),
TaskComment = item.Field<string>("TaskComment"),
Link = item.Field<string>("URL")
}).ToList();
}
This is working but when I open up the csv file and look under the date columns such as "ActionedDate" the date is showing up in a weird way, for example one of the values should be showing as: 03/03/2022 09:07:46 but it is showing in the field as:07:46.3, why is it doing this and how I can prevent that from happening? it doesn't do this for all the fields but it does for the majority
Yeah this was a face palm moment, just realised I had the format written as: "dd/MM/yyyy HH:mm:ss.fff" when it should be "dd/MM/yyyy HH:mm:ss:fff", changing it from ss.fff to ss:fff, fixed the issue
I currently have a project that I'm working on, which has a database connected to it. In said database I need to query some tables that don't have a relationship. I need to get a specific set of data in order to display it on my user interface. However I need to be able to reference the returned data put it into a list and convert it into json. I have a stored procedure that needs to just be executed against the context because it's retrieving data from many different tables.
I've tried using ExecuteSqlCommand but that doesn't work, because it returns -1 and can't put it into a list.
I've tried using linq to select the columns I want however it's really messy and I cannot retrieve the data as easily.
I've tried using FromSql, however that needs a model to execute against the context which is exactly what I don't want.
public string GetUserSessions(Guid memberId)
{
string sql = $"EXECUTE dbo.GetUserTrackByMemberID #p0";
var session = _context.Database.ExecuteSqlCommand(sql, memberId);
var json = JsonConvert.SerializeObject(session);
return json;
}
This is the ExecuteSqlCommand example, this returns -1 and cannot be put into a list as there will be more than one session.
public string GetUserSessions(Guid memberId)
{
var session = _context.MemberSession.Where(ms => ms.MemberId == memberId).Select(s => new Session() { SessionId =
s.SessionId, EventId = s.Session.EventId, CarCategory = s.Session.CarCategory, AirTemp = s.Session.AirTemp,
TrackTemp = s.Session.TrackTemp, Weather = s.Session.Weather, NumberOfLaps = s.Session.NumberOfLaps, SessionLength = s.Session.SessionLength,
Event = new Event() { EventId = s.Session.Event.EventId, TrackId = s.Session.Event.TrackId, Name = s.Session.Event.Name, NumberOfSessions =
s.Session.Event.NumberOfSessions, DateStart = s.Session.Event.DateStart, DateFinish = s.Session.Event.DateFinish, TyreSet = s.Session.Event.TyreSet,
Track = new Track() { TrackId = s.Session.Event.Track.TrackId, Name = s.Session.Event.Track.Name, Location = s.Session.Event.Track.Location, TrackLength
= s.Session.Event.Track.TrackLength, NumberOfCorners = s.Session.Event.Track.NumberOfCorners} } });
var json = JsonConvert.SerializeObject(session);
return json;
}
This is using Linq, however it's really messy and I feel there's probably a better way to do this, and then when retrieving the data from json it's a lot bigger pain.
public string GetUserSessions(Guid memberId)
{
var session = _context.MemberSession.FromSql($"EXECUTE dbo.GetUserSessionByMemberID {memberId}").ToList();
var json = JsonConvert.SerializeObject(session);
return json;
}
This is the ideal way I would like to do it, however since I'm using the MemberSession model it will only retrieve that data from the stored procedure which is in the MemberSession table, however I want data that is in other tables as well....
public string GetUserSessions(Guid memberId)
{
var session = _context.MemberSession.Where(ms => ms.MemberId == memberId).Include("Session").Include("Event").ToList();
var json = JsonConvert.SerializeObject(session);
return json;
}
I tried this way but because the Event table has no reference / relationship to MemberSession it returns an error.
As I've previously stated in the RawSql example I'm only getting the table data that is in the MemberSession table, no other tables.
There are no error messages.
using (var context = new DBEntities())
{
string query = $"Exec [dbo].[YOUR_SP]";
List<ResponseList> obj = context.Database.SqlQuery<ResponseList>(query).ToList();
string JSONString = JsonConvert.SerializeObject(obj);
}
I have used entity framework with code first approach.
when I am trying to pass record one by one Fromdate to Todate, 1st time its work, after it gives error like: "The property 'ID' is part of the object's key information and cannot be modified."
var fd = todaycooked.CookDate; // 2016-07-01
var td = todaycooked.ToCookDate; //2016-11-01
for (var date = fd; date <= td; date = date.AddDays(1))
{
var product = db.Products.Find(todaycooked.ProductID);
product.Qty = product.Qty + todaycooked.QTY;
todaycooked.Product = product;
todaycooked.CookDate = date;
db.TodayCookeds.Add(todaycooked);
db.SaveChanges();
}
Thanks in Advance.
You are setting the Product and CookDate once per day, so I assume you want one record per day - which means you mean one object per day. I suspect you actually want something like:
var fd = todaycooked.CookDate; // 2016-07-01
var td = todaycooked.ToCookDate; //2016-11-01
// this doesn't change per day, so only fetch it once
var product = db.Products.Find(todaycooked.ProductID);
for (var date = fd; date <= td; date = date.AddDays(1))
{
var toAdd = todaycooked.Clone(); // TODO: add a suitable clone method
toAdd.Product = product;
toAdd.CookDate = date;
db.TodayCookeds.Add(toAdd);
product.Qty = product.Qty + todaycooked.QTY;
db.SaveChanges();
}
However, you can probably also get away with moving the db.SaveChanges() to outside of the loop, which would make the whole thing atomic (rather than risking getting the first 4 of 8 days saved, then an error):
...
for (var date = fd; date <= td; date = date.AddDays(1))
{
...
}
db.SaveChanges();
I have a list in the form-
FieldName- DataType-
Date DateTime
DateString String
Unit double
Price double
I want to perform an operation such that if weakday on Date is not Monday then update DateString with empty string otherwise keep the value of DateString as it is.
Data is present in List.
UPDATE
I have applied aggregate function on DataTable dtGas as follows-
var qGas = from x in dtGas.AsEnumerable()
group x by new
{
Date = x.Field<DateTime>("Date"),
DateString = x.Field<string>("DateString")
} into egroup
select new
{
Date = egroup.Key.Date,
DateString = egroup.Key.DateString,
Unit = egroup.Sum(r => r.Field<double>("Unit")),
Price = egroup.Sum(r => r.Field<double>("Price"))
};
Now I need to show this result into Chart. Due to large amount of data values are overlapping on X-axis.
That is why I need to remove some values from DateString and show only few of them.
You can simply use conditional operator like this:-
group x by new
{
Date = x.Field<DateTime>("Date"),
DateString = x.Field<string>("DateString")
} into egroup
let isMonday = egroup.Key.Date.DayOfWeek.ToString() == "Monday"
select new
{
Date = egroup.Key.Date,
DateString = isMonday ? egroup.Key.DateString : "",
..other properties
I'm using the VSTA C# on an infopath 2010 form, whereby by using cascading drop downs (Course Title & Course Details) information is displayed.
So when a user selects the Course Title drop down, the Course details is populated with the StartTime, EndTime, Location and Development Category information from a Sharepoint 2010 list.
Now the problem I have is that I want the user to only view the course details for today and onwards, and not view course details for the past. This is the code whereby I display the coursedetails. I've tried declaring a dateTime variable and using it to compare with a string that converts to DateTime with Today, to make it later than the DateTime variable, but it gives me an error after I select a course title, it says "Object Reference not set to an instance of an object". With the troubleshooting tips: "Use the new keyword to create an object instance. Check to determine if the object is null before calling the method. Get gengeral help for this exception"
using (web = site.OpenWeb())
{
try
{
//SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(delegate()
//{
SPList lstDocs = web.Lists["Training Calander"] as SPList;
string sTitle = "";
string sSDate = "";
string sEDate = "";
string sLocation = "";
string SDCategory = "";
string CourseDetails = "";
//DateTime TodayDate = DateTime.Today;
//DateTime dt1 = Convert.ToDateTime(sEDate);
if (lstDocs != null)
{
SortedList<string, string> lstDetails = new SortedList<string, string>();
foreach (SPListItem item in lstDocs.Items)
{
try
{
sTitle = item["Title"].ToString();
sSDate = item["StartTime"].ToString();
sEDate = item["EndTime"].ToString();
sLocation = item["Location"].ToString();
SDCategory = item["Development Category"].ToString();
}
catch { }
if (sTitle == nValue) //&& (dt >= TodayDate))
{
try
{
CourseDetails = sSDate + " - " + sEDate + " | " + sLocation + " | " + SDCategory;
lstDetails.Add(CourseDetails,CourseDetails);
}
catch { }
}
}
I believe the problem is best solved before you execute your foreach loop. You need to create a query that will select only the Items that meet your criteria using a Where clause. They you can iterate through your loop without having to test the date on each pass, which is going to be slower.
Assuming Startdate is stored as a date variable, this should be a trivial query to write.
Apologies if I have misunderstood your issue.
foreach (SPListItem item in lstDocs.Items.Where(item => item.StartTime.Date >= DateTime.Now.Date))
This is assuming there is a property called StartTime in the SPListItem class and that you are using .NET 3+ and have access to Linq.