i am using asp.net web api 2 to do it,i have 2 questions with my code. how can i remove the T in datetime. my date and access have some field is Null and nullable, also how to change it to better format or layout to show null?
the result is in:
<ArrayOfDraft xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Details>
<Access>1</Access>
<Date>2021-11-06T16:04:00</Date>
<Mobile>386754298</Mobile>
<Name>Emily</Name>
<UserId>ABC001</UserId>
</Details>
<Details>
<Access>4</Access>
<Date i:nil"true"/>
<Mobile>386754298</Mobile>
<Name>King</Name>
<UserId>ABD002</UserId>
</Details>
My Code:
public IHttpActionResult Getbyid(int id)
{
List<TestClass> draft = new List<TestClass>();
string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "Select UserID, Name, Mobile, Access, Date From tblTest";
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
SqlDataReader sdr = sqlcomm.ExecuteReader();
while (sdr.Read())
{
draft.Add(new TestClass()
{
UserId = Convert.ToInt32(sdr.GetValue(0)),
Name = sdr.GetValue(1).ToString(),
Mobile = sdr.GetValue(2).ToString(),
Access = Convert.ToInt32(sdr.GetValue(3)),
Date = Convert.ToDateTime(sdr.GetValue(4))
});
}
return Ok(draft);
}
How to change it to like this:
<Date>2021-11-06 16:04:00</Date>
and
<Date></Date>
or
<Date>null</Date>