sql - SELECT between two DateTimePicker values -


i'm little stumped , hope can help!

i trying sql select command display set of values dates set between datetimepicker1 , datetimepicker2, code have:

        if combobox1.text = "all"         da = new oledbdataadapter("select * times ([date] between '" & datetimepicker1.value & "' , '" & datetimepicker2.value & "')", myconn)         da.fill(ds, "times")         dim view1 new dataview(tables(0))         source1.datasource = view1         datagridview1.datasource = view1         exit sub     end if 

this code kind of working.... it's pulling information out of access database (i'm restricted using access...) , displpaying within datagrid.

the trouble i'm having it's selecting dd between range, ie:

datetimepicker1 equal 03/08/2015 datetimepicker2 equal 05/08/2015

the data being pulled is:

03/06/2015 04/06/2015 05/06/2015 03/07/2015 04/07/2015 05/07/2015 03/08/2015 04/08/2015 05/08/2015 

seems using dd value rather whole date value..

i'm thinking it's syntax error i'm struggling figure out where!

thanks!

i've changed code to:

    if combobox1.text = "all"         dim sql = "select * times [date] between @startdate , @enddate"         dim cmd = new oledbcommand(sql, myconn)         cmd.parameters.addwithvalue("@startdate", datetimepicker1.value)         cmd.parameters.addwithvalue("@endpdate", datetimepicker2.value)         dim da = new oledbdataadapter(cmd)         da.fill(ds, "times")         dim view1 new dataview(tables(0))         source1.datasource = view1         datagridview1.datasource = view1         exit sub     end if 

this isn't displaying data @ all... it's not throwing errors , pulling headers through displaying no data.. advice?

thanks

always use sql parameters(f.e. prevent sql-injection), that'll fix issue:

dim sql = "select * times [date] between @startdate , @enddate" dim cmd = new oledbcommand(sql, myconn) cmd.parameters.addwithvalue("@startdate", datetimepicker1.value); cmd.parameters.addwithvalue("@endpdate", datetimepicker2.value); dim da = new oledbdataadapter(cmd) 

maybe want truncate time portion of datetime:

cmd.parameters.addwithvalue("@startdate", datetimepicker1.value.date); cmd.parameters.addwithvalue("@endpdate", datetimepicker2.value.date); 

bu since datetimepicker2.value.date truncates time means select midnight , not later @ day. need include end-date adding 1 day, datetimepicker2.value.date.adddays(1). include data of next day if midnight. instead of using between better use >= , <.

so better approach:

dim sql = "select * times [date] >= @startdate , [date] < @enddate"  cmd.parameters.addwithvalue("@startdate", datetimepicker1.value.date); cmd.parameters.addwithvalue("@endpdate", datetimepicker2.value.date.adddays(1)); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -