Best Practices for a Calendar Database Schema? - c#

I'm a beginner with C#, ASP.NET, SQL Server Express and programming in general. I'm trying to make an online calendar (on a deadline). I'll read all the other posts that are similar to this, but for now I have a question that I hope will set me on my way.
What are the steps to link a calendar to a database? Any examples of where/how this has been done would be greatly appreciated?
I see possible pitfalls when a date has more than one entry by different users, so if anyone knows how this is managed again I'm all ears.

I have wrote you a small example that shows
Connecting to a SQL Express server
Reading data
Selecting multiple dates (i.e. more than one date entry by different users)
Hope this helps!...
// The SQL connection object to connect to the database. Takes connection string.
SqlConnection connection =
new SqlConnection(#"Data Source=localhost\SQLEXPRESS;Initial Catalog=Example");
// Idealy you wouldnt pass direct SQL text for injection reasons etc, but
// for example sake I will enter a simple query to get some dates from a table.
// Notice the command object takes our connection object...
SqlCommand command = new SqlCommand("Select [Date] From Dates", connection);
// Open the connection
connection.Open();
// Execute the command
SqlDataReader reader = command.ExecuteReader();
// A list of dates to store our selected dates...
List<DateTime> dates = new List<DateTime>();
// While the SqlDataReader is reading, add the date to the list.
while (reader.Read())
{
dates.Add(reader.GetDateTime(0));
}
// Close the connection!
connection.Close();
// Use the selected dates property of the ASP.NET calendar control, add every
// date that we read from the database...
foreach (DateTime date in dates)
{
Calendar1.SelectedDates.Add(date);
}
Good luck!

Dunno if this is of any help but CodeProject seems to have some similar solution

I'd recommended finding a couple of free open source calendar applications and looking at their schemas.
MojoPortal and DotNetNuke Events both look like they have calendar portions.
You'll have a huge jump start and will avoid some basic mistakes.

Looks like someone already asked this question. Laying out a database schema for a calendar application
One word of advice is to use integer fields instead of datetime fields. They will run faster. Also don't be afraid to denormalize your tables. So you might have a date column, year column, month column, and day column.

Related

Calculate datediff using mysql in c# winform project

I'm creating a BloodBank application using c# and MySQL and my trouble right now is that I want to create a query with DATEDIFF() function that can calculate the difference in days between a made donation already and a new one from the same person since the same person can only donate blood 60 days after a previous donation. One of the dates is already in my MYSQL database and the other one will be received from a datepicker in a Window form .Net.
I have problem making the connection between the mysql database row for the current person and the info in the new made donation.
At the end I want to check if the difference between donations from a same person(same name and email in my case) is more than 60 days and in that case he can make a donation again.
First thing that comes to mind is that instead of using a DATEDIFF() function in a query, pull the Date information from the database and put it into a DateTime variable, then run a comparison based on DateTime.Now. If it's more than 60 days, it's good to go, otherwise they can't donate.
Additionally, consider using something other than a name/email combo as your check. Between work and personal emails, I probably have like 5 accounts that I would be able to use within your system.

Date inserted to database become 0000

so here's my code
cmd.CommandText = "insert into tbl_project (`sampleField`) values(#sample)";
cmd.Parameters.AddWithValue("#sample", DateTime.Now.ToString());
This is what's happening.
When I run my solution/project on the Unit that have the database through xampp. I can perfectly put the right date on the database.
But when run the solution/project from a different unit and remotely access the database. The date being saved to my database become 0000, not the correct date. How to fix this. Please help.
BTW. I can perfectly remotely insert strings and numbers on the database. the only problem is the date. its becoming null or 0000.
You shouldn't convert the date to string. If you do, the results depend on the culture settings on the computer where you run the code.
Add the parameter as DateTime:
cmd.Parameters.Add("#sample", SqlDbType.DateTime).Value = DateTime.Now;
It's better to use Parameters.Add instead of AddWithValue because it lets you specify the data type of the parameter explicitly.

How to keep all date selection in calendar?

I've created a code that read dates from a database and put them into the Calendar control. All working good, but if the user select a date in the Calendar, after the population, all the date selected through code disappear. There's another way to flag the date that are previously added from the database population?
public void setFixturesControl()
{
Database.m_dbConnection.Open();
string query = "select * from date";
SQLiteCommand input = new SQLiteCommand(query, Database.m_dbConnection);
SQLiteDataReader reader = input.ExecuteReader();
while (reader.Read())
{
if (MainWindow.AppWindow.League.SelectedItem.ToString().Equals(reader["caption"].ToString()))
{
MainWindow.AppWindow.Calendar.SelectedDates.Add(DateTime.Parse(reader["data"].ToString()));
}
}
Database.m_dbConnection.Close();
}
There's a way to prevent the dates disappear after a user click? Or something that allow me to recognize the dates added by database population? Thanks.
After a long search on the net and a large documentation on the Calendar class, I could see two things:
To get what I want I have to take advantage of a custom control
Try to act via code by creating a delegate, but this is not really a
simple thing
I ran then in the first point, in particular, from the post of David Veeneman, it addresses exactly my problem.
The final result will be like this:
Exactly what I want.
In the post it is all documented, so it would be pointless to do a copy paste here. Thank this member that for me at least was able to solve a failure in the control of Microsoft. Unfortunately I am increasingly convinced that WPF is still immature and that still requires a bit 'of years to complete. I hope my answer is help, if anyone manages to solve the problem in a more elegant, well it responds well. Thank You.

SQLite.Net Extensions Data insertion error on Windows Phone 8.1 Runtime project

I have been using the SQLiteAsyncConnection for all the database operations and it was recently I came to know that it doesn't allow association between tables. Now I am in the process of moving the code to use the SQLite.Net Extension which supports extensions. I have found a weird issue when I insert datetime data into the table. It completely changes the datetime on insertions.
App.db2.Insert(new FrequentlyAssignedShifts()
{
ShiftStart = Convert.ToDateTime(btnShiftStart.Content.ToString()),
ShiftEnd = Convert.ToDateTime(btnShiftEnd.Content.ToString()),
});
And this is how the connection is established.
string databasePath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Scheduler.sqlite");
public static SQLiteConnection db2;
var platform = new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT();
db2 = new SQLiteConnection(platform, databasePath);
I checked until the last point using breakpoints, whether the right data is getting inserted. It all seems fine, it is getting the right date from the button controls, but the moment it passes this part of the code the date value changes. The "ShiftStart & ShiftEnd" variables are datetime variables in the Table. Could someone please advise.
Thanks for reading the post.
Maybe too late but as far I can see, the time is saved as ticks on the database, so instead of using "Date" or "Timestamp" or anything else, use bigint, thus, the field will hold the time as ticks using UTC, once you retrieve the value from database use the function "ToLocalTime()" from DateTime and vioala!!!!

truncated datetime Exception in SQL

I have a problem in SQL and don't know why this is happening. First I show my code:
for (int i = 0; i < dateList.Count;i++ )
{
String connectionQuery = form1.conStringBox.Text;
SqlConnection connection = new SqlConnection(connectionQuery);
SqlCommand sqlComInsert = new SqlCommand(#"INSERT INTO [" + form1.tableName.Text + "] ([" + form1.cusName.Text + "],[" + form1.date.Text + "]) VALUES(#cusName, #date)", connection);
sqlComInsert.Parameters.AddWithValue("cusName", cusName[i]);
sqlComInsert.Parameters.AddWithValue("date",date[i]);
This modified code came from here:
Update database with date format
I try to describe the most important things:
I have a textfile where I read the values from (there are two columns -> cusName and date). I store them in two lists (cusName list = String, date list = DateTime). Now I want to store them in my database. The columntype of the date column is datetime.
Somehow with the above code I get an exception (string or binary data would be truncated). This happens at the date column. In my SQL Server 2008 it get's displayed as (for example) 2013-10-21 00:00:00.000. Since I'm working on a german computer, when I put them in my List they get displayed as: 21.10.2013 00:00:00.
Now why do I have accepted the answer in the other thread? Because when I tried the above code on another computer it worked. When I'm trying this on the computer I'm sitting right now I get the error. I'm working with SQL Server 2008 and Windows 7 on both computers (but I once realized that there are differences with the case sensivity on SQL Server). So I don't know where the error is. Hope someone can help me.
Sample data from a textfile looks like for every row:
Name1;2013-09-27
I would recommend formatting the date strings in YYYYMMDD format before sending them to the SQL server.
I would assume that passing a DateTime-typed field would handle the conversion automatically, but I haven't worked with servers/computers in different regions, so I don't know that for sure. I do know that SQL will handle converting a "YYYYMMDD" string into a Date/DateTime (continue passing the string as a parameter if you go that route).
On the other hand, the error that you mentioned is not commonly associated with DateTime fields (they will usually give you something about a date conversion failure). I would double-check your name column and the lengths of all of your name inputs, just in case. Could also be multi-byte encoding on the strings that makes them take up more room than they appear to? It's hard to say without knowing what the input looks like!

Categories