DateTime Picker not saving correct date in Sql Database

  • Thread starter Thread starter wgraulich
  • Start date Start date
W

wgraulich

Guest
I have created 2 datetime picker fields in my windows form applications and have linked them to a sql table when clicking save button. The data is going into the sql table but is showing the wrong date that was selected. Example I pick the date of 2020-06-29 in the form. When I hit save the date showing up in the sql table is 1753-01-01. I am guessing my save button click is written wrong but I can't see the mistake.


private void Savebtn_Click(object sender, EventArgs e)
{
//Get the value from the input fields
c.Start_Date = Start_Date.MinDate;
c.End_Date = End_Date.MinDate;
c.Callers_Name = Callers_Name.Text;
c.Callers_Phone = Callers_Phone.Text;
c.City = City.Text;
c.Address = Address.Text;
c.Reason = Reason.Text;
c.Description = Description.Text;
c.Dispatcher = Dispatcher.Text;

//Inserting Data into DAtabase uing the method we created in previous episode
bool success = c.Insert(c);
if (success == true)
{
//Successfully Inserted
MessageBox.Show("Data Entered Successfully");
}
else
{
//FAiled to Add Contact
MessageBox.Show("Failed to add Data. Try Again");
}
}
}
}

This is my Class.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Overnight_Parking_Form.OvernightParkingClasses
{
class ParkingClass1
{
//Getter Setter Properties
//Acts as Data Carrier in Our Applicaton
public int ID { get; set; }
public DateTime Date_Entered { get; set; }
public DateTime Start_Date { get; set; }
public DateTime End_Date { get; set; }
public string Callers_Name { get; set; }
public string Callers_Phone { get; set; }
public string City { get; set; }
public string Address { get; set; }
public string Reason { get; set; }
public string Description { get; set; }
public string Dispatcher { get; set; }

static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

//SElecting Data from Database
public DataTable Select()
{
///Step 1: Database Connection
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();
try
{
//Step 2: Writing SAL Query
string sql = "SELECT * FROM Overnight_Parking";
//Creating cmd using sql and conn
SqlCommand cmd = new SqlCommand(sql, conn);
//Creating SQL DataAdapter using cmd
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}

//Inserting DAta ino Database
public bool Insert (ParkingClass1 c)
{
//Creating a default return type and setting its value to false
bool isSuccess = false;

//STep 1: Connect DAtabase
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
//STep 2: Create a SQL Query to insert DAta
string sql = "INSERT INTO Overnight_Parking (Start_Date, End_Date, Callers_Name, Callers_Phone, City, Address, Reason, Description, Dispatcher) VALUES (@Start_Date, @End_Date, @Callers_Name, @Callers_Phone, @City, @Address, @Reason, @Description, @Dispatcher)";
//Creating SQL Command using sql and conn
SqlCommand cmd = new SqlCommand(sql, conn);
//Create Parameters to add data
cmd.Parameters.AddWithValue("@Start_Date", c.Start_Date);
cmd.Parameters.AddWithValue("@End_Date", c.End_Date);
cmd.Parameters.AddWithValue("@Callers_Name", c.Callers_Name);
cmd.Parameters.AddWithValue("@Callers_Phone", c.Callers_Phone);
cmd.Parameters.AddWithValue("@City", c.City);
cmd.Parameters.AddWithValue("@Address", c.Address);
cmd.Parameters.AddWithValue("@Reason", c.Reason);
cmd.Parameters.AddWithValue("@Description", c.Description);
cmd.Parameters.AddWithValue("@Dispatcher", c.Dispatcher);

//Connection Open Here
conn.Open();
int rows = cmd.ExecuteNonQuery();
//If the query runs successfully then the value of rows will be greater than zero else its value will be 0
if(rows>0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}

//Method to update data in database from our application
public bool Update(ParkingClass1 c)
{
//Create a default return type and set its default value to false
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
//SQL to update data in our Database
string sql = "UPDATE Overnight_Parking SET Dated_Entered=@Dated_Entered, Start_Date=@Start_Date, End_Date=@End_Date, Callers_Name=@Callers_Name, Callers_Phone=@Callers_Phone, City=@City, Address=@Address, Reason=@Reason, Description=@Description, Dispatcher=@Dispatcher WHERE ID=@ID";

//Creating SQL Command
SqlCommand cmd = new SqlCommand(sql, conn);
//Create Parameters to add value
cmd.Parameters.AddWithValue("@Date_Entered", c.Date_Entered);
cmd.Parameters.AddWithValue("@Start_Date", c.Start_Date);
cmd.Parameters.AddWithValue("@End_Date", c.End_Date);
cmd.Parameters.AddWithValue("@Callers_Name", c.Callers_Name);
cmd.Parameters.AddWithValue("@Callers_Phone", c.Callers_Phone);
cmd.Parameters.AddWithValue("@City", c.City);
cmd.Parameters.AddWithValue("@Address", c.Address);
cmd.Parameters.AddWithValue("@Reason", c.Reason);
cmd.Parameters.AddWithValue("@Description", c.Description);
cmd.Parameters.AddWithValue("@Dispatcher", c.Dispatcher);
//Open DAtabase Connection
conn.Open();

int rows = cmd.ExecuteNonQuery();
//if the query runs sucessfully then the value of rows will be greater than zero else its value will be zero
if(rows>0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//Close Connection
conn.Close();
}
return isSuccess;
}
}
}

Continue reading...
 
Back
Top