I'm doing a bit of C# Winforms coding in my spare time, just getting to grips with everything. I have a SQL script which creates a local db on vs2012 as follows:
-- Creating table 'Users'--
CREATE TABLE [dbo].[Users]
(
[UserID] int IDENTITY(1,1) NOT NULL,
[Surname] nvarchar(30) NOT NULL,
[Forename] nvarchar(30) NOT NULL,
[Company] nvarchar (30) NOT NULL,
[SecurityLevel] int NOT NULL,
[IssueDate] DateTime NOT NULL,
[ExpiryDate] DateTime NOT NULL,
[CardID] int NOT NULL,
);
GO
Now I want to save details to that table, so I created a method:
private void btnSaveDetails_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection();
SqlCommand com = new SqlCommand();
sc.ConnectionString = (Properties.Settings.Default.BioEngineering);
sc.Open();
com.Connection = sc;
com.CommandText = ("INSERT INTO Users (Forename, Surname, Company, SecurityLevel, IssueDate, ExpiryDate, CardID) VALUES ('" + this.txtFirstName.Text + "','" + this.txtLastName.Text + "','" + this.txtCompany.Text + "','" + this.cboSecurityLevel.Text + "','" + this.dtpIssueDate.Value + "','" + this.dtpExpiryDate.Value + "','" + this.cboCardID.Text + "');");
com.ExecuteNonQuery();
sc.Close();
}
When I run the code I get an error
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
I know it has something to do with the datetime format of either the SQL or C# equivalent but I don't know how to format the datetime in order to comply to the error. Any ideas? I tried formatting it withing the Command Text line but it didn't seem to resolve the issue.