SQL Server & C# : filter for System.Date - results only entries at 00:00:00 -


i have connected sql server database in visual studio , displaying content in grid. created dropdown menu column names selectable options , text field filter specific content, e.g., dropdown = "start" - textfield = 14.03.2015 = filter column "start" each entry contains "14.03.2015" - , display in grid.

i'm done part. problem left i'm facing whenever enter date - e.g., 14.03.2015 displays dates start @ 00:00:00 - other entries not start @ 00:00:00 ignored , can't figure out how modify work properly.

the grid looks this: http://abload.de/img/untitled123yqkyn.png

and using following c# code filter:

protected void button1_click(object sender, eventargs e) {     string filterexpression = string.empty;      if (dropdownlist1.selectedvalue.tostring().equals("start"))     {         filterexpression = string.format("start  = '{0}'", textbox1.text);     }     else if (dropdownlist1.selectedvalue.tostring().equals("end"))     {         filterexpression = string.format("end  = '{0}'", textbox1.text);     }     else if (dropdownlist1.selectedvalue.tostring().equals("creation time"))     {         filterexpression = string.format("datetimecreated = '{0}'", textbox1.text);     }     else if (dropdownlist1.selectedvalue.tostring().equals("last modified"))     {         filterexpression = string.format("lastmodifiedtime = '{0}'", textbox1.text);     }     else     {         filterexpression = string.concat(dropdownlist1.selectedvalue, " '%{0}%'");     }      sqldatasource1.filterparameters.clear();     sqldatasource1.filterparameters.add(new controlparameter(dropdownlist1.selectedvalue, "textbox1", "text"));     sqldatasource1.filterexpression = filterexpression; } 

excuse quality of code, i'm new c# , matter programming itself. hope there able me.

use cast statement on date fields remove time portions follows:

filterexpression = string.format("cast(start  date) = '{0}'", textbox1.text); filterexpression = string.format("cast(end    date) = '{0}'", textbox1.text); filterexpression = string.format("cast(end    date) = '{0}'", textbox1.text); filterexpression = string.format("cast(lastmodifiedtime   date) = '{0}'", textbox1.text); 

also if inputs dates, try this:

filterexpression = string.format("convert(date,start) = '{0}'", textbox1.text); filterexpression = string.format("convert(date,end) = '{0}'", textbox1.text); filterexpression = string.format("convert(date,end) = '{0}'", textbox1.text); filterexpression = string.format("convert(date,lastmodifiedtime) = '{0}'", textbox1.text); 

last not least, if inputs varchar, try this:

filterexpression = string.format("convert(varchar,start,101) = '{0}'", textbox1.text);     filterexpression = string.format("convert(varchar,end,101) = '{0}'", textbox1.text);     filterexpression = string.format("convert(varchar,end,101) = '{0}'", textbox1.text);     filterexpression = string.format("convert(varchar,lastmodifiedtime,101) = '{0}'", textbox1.text); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -