I'm posting this object with json
[DataContract(Namespace = "http://localhost:30366/avtorji")]
public class Avtor
{
[DataMember]
public int idAvtor { get; set; }
[DataMember]
public string ime { get; set; }
[DataMember]
public string priimek { get; set; }
[DataMember]
public DateTime datum_rojstva { get; set; }
public Avtor() { }
}
This is my request:
{
"idAvtor": 4,
"ime": "M Vaqqas",
"priimek": "Noob",
"datum_rojstva": "2014-01-03 23:28:56"
}
My function is
public void DodajAvtorja(Avtor avtor)
{
string connString = null;
try
{
connString = "server=localhost;userid=;password;database=";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand comm = conn.CreateCommand();
//avtor.datum_rojstva.ToString("yyyy-MM-dd HH:mm:ss")
comm.CommandText = "INSERT INTO avtor (idAvtor, ime, priimek, datum_rojstva)"
+" VALUES (@idAvtor, @ime, @priimek, @datum_rojstva)";
comm.Parameters.AddWithValue("@idAvtor", avtor.idAvtor);
comm.Parameters.AddWithValue("@ime", avtor.ime);
comm.Parameters.AddWithValue("@priimek", avtor.priimek);
comm.Parameters.AddWithValue("@datum_rojstva", avtor.datum_rojstva);
conn.Open();
int dodanih = comm.ExecuteNonQuery();
if (dodanih > 0)
{
UriTemplateMatch match = WebOperationContext.Current.IncomingRequest.UriTemplateMatch;
UriTemplate template = new UriTemplate("/avtor/{idAvtor}");
Uri novAvtorUri = template.BindByPosition(match.BaseUri, avtor.idAvtor.ToString());
WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(novAvtorUri);
}
conn.Close();
}
catch (Exception)
{
throw;
}
}
It gives me an error "400 Bad Request" before it goes to function. I'm assuming this is because of DateTime property. Can you even send datetime that way? And what would be the solution?
Thank you for help!