2

I didn't use datetime as the value of my table yet I wonder why the time still showing up:

enter image description here

Here is my codes to get the gridview output:

   private void CalendarDataGrid()
    {
        con.Open();

        cmd = new SqlCommand(@"SELECT StocksHistory.DateTrack,Stocks.ItemID
                            ,StocksHistory.StockIn,StocksHistory.StockOut
                            ,StocksHistory.StockOnHand
                            FROM Stocks
                            FULL OUTER JOIN StocksHistory
                            ON Stocks.ItemID = StocksHistory.ItemID
                            WHERE DateTrack between @date1 and @date2", con);




        cmd.Parameters.AddWithValue("@date1",txtDate1.Text);
        cmd.Parameters.AddWithValue("@date2",txtDate2.Text);


        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        con.Close();
    }

How can I only get the Date?

rai nalasa
  • 849
  • 1
  • 12
  • 32
  • Change the format string for the column. – SLaks May 31 '16 at 02:54
  • Check [How to return the date part only from a SQL Server datetime datatype](http://stackoverflow.com/questions/113045/how-to-return-the-date-part-only-from-a-sql-server-datetime-datatype?page=1&tab=votes#tab-top) – Andrés Robinet May 31 '16 at 02:59
  • could I get an example? – rai nalasa May 31 '16 at 03:00
  • What data type is field `DateTrack` in table `StocksHistory`? If it was `date`, the time should not appear at all. Does a direct select query in Management Studio show the time as well? Maybe it's just a matter of display data type in `GridView1`. – Andrew May 31 '16 at 03:00
  • @robinet I did'nt use datetime – rai nalasa May 31 '16 at 03:00
  • @rainalasa Well, then maybe this is what you are looking for? [How to format DateTime columns in DataGridView?](http://stackoverflow.com/questions/4033113/how-to-format-datetime-columns-in-datagridview) – Andrés Robinet May 31 '16 at 03:03
  • Are you not able to handle the code for input? It's obvious that the initial data were DateTime and converted to text type(varchar). By sure, the datatype of the column is text. If you cannot handle, I suggest to get the index of first blank ' ' and trim the unnecessary latter part. – Kay Lee May 31 '16 at 03:24
  • The change anything.It was date not datetime from the start – rai nalasa May 31 '16 at 03:27
  • If the DateTrack field values are strings: Convert.ToDateTime(String.Format({0:m/d/yy}, textDate1.Text)). Alternatively you can convert them to date part like this: Convert.ToDateTime(textDate1.Text).Date. – Tetsuya Yamamoto May 31 '16 at 03:31
  • If you never need the time, then just declare the column to be of type `DATE` in your table.... – marc_s May 31 '16 at 05:41

3 Answers3

1

I found the solution. Reference

<asp:BoundField HeaderText="Date" 
                DataField="SampleDate" 
                DataFormatString="{0:MM/dd/yyyy}"  >
Community
  • 1
  • 1
rai nalasa
  • 849
  • 1
  • 12
  • 32
0
"SELECT cast(StocksHistory.DateTrack as date),Stocks.ItemID
                        ,StocksHistory.StockIn,StocksHistory.StockOut
                        ,StocksHistory.StockOnHand
                        FROM Stocks
                        FULL OUTER JOIN StocksHistory
                        ON Stocks.ItemID = StocksHistory.ItemID
                        WHERE DateTrack between @date1 and @date2"
meghlashomoy
  • 123
  • 1
  • 9
0

You just need to update your inline SQL. Replace StocksHistory.DateTrack either by CAST(StocksHistory.DateTrack AS DATE) AS DateTrack or by CONVERT(DATE, StocksHistory.DateTrack) AS DateTrack.

varun kumar dutta
  • 202
  • 1
  • 4
  • 10