I have a SQL-server and a column called CitizenshipDate. That column is of type datetime in the SQL.
However, the problem is that it can have the value '0' which is NOT a datetime value but rather a string value.
So what I'm trying to do is to handle it as a string in C# when inserting it values but get the error:
Conversion failed when converting datetime from character string.
I get that error because I'm trying to insert string into a datetime in the SQL-server.
Here is my code:
class Person {
public string PersonalIdentityNumber { get; set; }
public string SpecialIdentityNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string CitizenshipDate { get; set; }
}
List<FolkbokforingspostTYPE> deserializedList = new List<FolkbokforingspostTYPE>();
deserializedList = Deserialize<List<FolkbokforingspostTYPE>>();
var myPersons = Deserialize<List<FolkbokforingspostTYPE>>()
.Select(x => new Person
{
PersonalIdentityNumber = x.Personpost.PersonId.PersonNr,
SpecialIdentityNumber = x.Personpost.PersonId.Tilltalsnamnsmarkering != null ? x.Personpost.PersonId.Tilltalsnamnsmarkering : null,
LastName = x.Personpost.Namn.Efternamn,
FirstName = x.Personpost.Namn.Fornamn,
CitizenshipDate = x.Personpost.Medborgarskap != null ? x.Personpost.Medborgarskap.Medborgarskapsdatum : null
});
string connetionString = null;
SqlDataAdapter adpter = new SqlDataAdapter();
DataSet ds = new DataSet();
XmlReader xmlFile;
connetionString = "Data Source=tsrv2062;Initial Catalog=Bums;User ID=BumsUser;Password=2tusen7Bums";
xmlFile = XmlReader.Create("navetout.xml", new XmlReaderSettings());
ds.ReadXml(xmlFile);
using (var connection = new SqlConnection(connetionString))
{
connection.Open();
DateTime datum = DateTime.Now;
string LastChangedBy = "System";
foreach (Person p in myPersons)
{
SqlCommand command1 = Avreg(p.UnregistrationReason, p.GivenNameNumber,p.ProtectedIdentity, p.CitizenshipDate, connection);
command1.Parameters.AddWithValue("@PersonalIdentityNumber", string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
command1.Parameters.AddWithValue("@FirstName", p.FirstName);
command1.Parameters.AddWithValue("@LastName", p.LastName);
command1.ExecuteNonQuery();
Console.WriteLine(string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
}
}
Console.WriteLine("Done");
// }// Put a break-point here, then mouse-over PersonalIdentityNumber... deserializedList contains everything if you need it
//catch (Exception)
// {
// throw;
// }
Console.ReadKey();
}
public static SqlCommand Avreg(string s, string t, string p, string c, SqlConnection connection)
{
var query = "UPDATE Seamen SET FirstName = @FirstName, "+
"LastName = @LastName, "+
SqlCommand command1;
//Here is the `CitizenshipDate`
if (c == "0")
{
query += ", CitizenshipDate = '0'";
command1 = new SqlCommand(query, connection);
command1.Parameters.Clear();
}
else
{
query += ", CitizenshipDate = @CitizenshipDate";
command1 = new SqlCommand(query, connection);
command1.Parameters.Clear();
command1.Parameters.AddWithValue("@CitizenshipDate", c ?? DBNull.Value.ToString());
}
//Ignore these if statements
if ((!string.IsNullOrEmpty(s)) && !string.IsNullOrEmpty(t))
{
}
else
{
query += ", GivenNameNumber = @GivenNameNumber WHERE PersonalIdentityNumber = @PersonalIdentityNumber";
t = "00";
command1 = new SqlCommand(query, connection);
command1.Parameters.Clear();
command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());
return command1;
}
return command1;
}
Please note that I cannot change the type in the database. I somehow need a way to insert the value from String. Can someone help ?
0is not a proper value for datetime. TryCitizenshipDate = NULLNullvalue ?