0

i want to know how to search datetime in url of web api, i want to search the date and it can show with xml format in website. such as if i search 2018-01-01 and it will show after 2018-01-01 date and equals to 2018-01-01. do i need to parse datetime to string first? Is this possible to do it?

my code:

        [HttpGet()]
        public IHttpActionResult Get()
        {
            List<TestClass> test = new List<TestClass>();
            string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(mainconn);
            string sqlquery = "SELECT UserID, Name, Mobile, Access, Date From tbluser";
            sqlconn.Open();
            SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
            SqlDataReader reader = sqlcomm.ExecuteReader();
            while (reader.Read())
                {
                    Test.Add(new TestClass()
                    {
                        UserId = Convert.ToInt32(reader.GetValue(0)),
                        Name = reader.GetValue(1).ToString(),
                        Mobile = reader.GetValue(2).ToString(),
                        Age = Convert.ToInt32(reader.GetValue(3)),
                        Date = Convert.ToDateTime(reader.GetValue(4))
                    });
                }
            return Ok(test);
        }

my class:

[DataContract]
public class TestClass
    {
        [DataMember(Order=1)]
        public string UserId { get; set; }
        [DataMember(Order = 2)]
        public string Name { get; set; }
        [DataMember(Order = 3)]
        public string Mobile { get; set; }
        [DataMember(Order = 4)]
        public int Age { get; set; }
        [DataMember(Order = 5)]
        public DateTime Date { get; set; }
    }

expected:

http://localhost:IP/api/value?Date=2018-01-01

expected result:

<ArrayOfTest xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Test>
<UserId>1</UserId>
<Name>Emily</Name>
<Mobile>8375269</Mobile>
<Age>21</Age>
<Date>2018-01-01</Date>
</Test>
<Test>
<UserId>2</UserId>
<Name>Victor</Name>
<Mobile>08314732</Mobile>
<Age>31</Age>
<Date>2022-03-05</Date>
</Test>
<Test>
<UserId>3</UserId>
<Name>Peter</Name>
<Mobile>32475962</Mobile>
<Age>45</Age>
<Date>2019-10-25</Date>
</Test>
<Test>
<UserId>4</UserId>
<Name>John</Name>
<Mobile>24398592</Mobile>
<Age>24</Age>
<Date>2018-02-16</Date>
</Test>
<Test>
<UserId>5</UserId>
<Name>Kobe</Name>
<Mobile>19873491</Mobile>
<Age>38</Age>
<Date>2020-05-12</Date>
</Test>
  • Are you looking for string to dateTime conversion ? https://stackoverflow.com/a/919249/8763644 – ahmet gül Aug 05 '22 at 08:50
  • not really, what i want is to convert datetime in the database to string and do like a keyword search in the url link to find out datetime in web api – user27429245 Aug 05 '22 at 09:05

1 Answers1

1

You can pass the date as a String and then Convert it into Date format I think it's work

    [HttpGet]
    public IHttpActionResult Get(string Date)
    {
        List<TestClass> test = new List<TestClass>();

        DateTime? DateInFormat = Date.ToDate("yyyy-MM-dd");
        string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(mainconn);
        string sqlquery = "SELECT UserID, Name, Mobile, Access, Date From tbluser where Date ="+DateInFormat+"";
        sqlconn.Open();
        SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
        SqlDataReader reader = sqlcomm.ExecuteReader();
        while (reader.Read())
        {
            Test.Add(new TestClass()
            {
                UserId = Convert.ToInt32(reader.GetValue(0)),
                Name = reader.GetValue(1).ToString(),
                Mobile = reader.GetValue(2).ToString(),
                Age = Convert.ToInt32(reader.GetValue(3)),
                Date = Convert.ToDateTime(reader.GetValue(4))
            });
        }
        return Ok(test);
    }
Anurag
  • 43
  • 6