Populating a drop down list with non-repeating values - sql

I have a multiple search query which includes a drop down to pick an admin which needs to be called from the Assigned_To column in my RMS db. The problem is that the dropdown shows every single value repeated. For example:
Admin:
Not assigned
Not assigned
"
"
"
Alexandra M.
Martin D.
Alexandra M.
I want it to look like:
Admin:
Not assigned
Martin D.
Alexandra M.
"Not assigned" is the default value of a case that is created.
Here is my code:
var db = Database.Open("RMS") ;
var selectCommand = "SELECT * FROM RMS";
var formSSO = "";
var formAssigned_To = "";
var formCase_Status= "";
var fromDate = "";
var toDate = "";
formSSO = Request.QueryString["formSSO"];
formAssigned_To = "%" + Request["formAssigned_To"] + "%";
formCase_Status = Request["formCase_Status"];
fromDate = Request.QueryString["fromDate"];
toDate = Request.QueryString["toDate"];
selectCommand = "SELECT DISTINCT * FROM RMS WHERE 1=1";
if(!Request.QueryString["formSSO"].IsEmpty() ) {
selectCommand +=" AND SSO LIKE #0";
}
if(!Request.QueryString["formAssigned_To"].IsEmpty() ) {
selectCommand +=" AND Assigned_To LIKE #1";
}
if(!Request.QueryString["formCase_Status"].IsEmpty() ) {
selectCommand +=" AND Case_Status = #2";
}
if(!Request.QueryString["fromDate"].IsEmpty() ) {
selectCommand +=" AND Created >= #3";
}
if(!Request.QueryString["toDate"].IsEmpty() ) {
selectCommand +=" AND Created <= #4";
}
var data = db.Query(selectCommand, formSSO, formAssigned_To, formCase_Status, fromDate, toDate);
var columns = new[]{"ID", "SSO", "Category", "System", "Subject", "Created", "Assigned_To"};
var grid = new WebGrid(data, ajaxUpdateContainerId: "grid", defaultSort: "ID", columnNames: columns);
if (Request.QueryString[grid.SortDirectionFieldName].IsEmpty()) {
grid.SortDirection = SortDirection.Descending;
}
}
And this is the html form field for the dropdown:
<li class="form-line form-line-column" id="id_22">
<label class="form-label-top" id="label_22" for="input_22"> ADMIN </label>
<div id="cid_22" class="form-input-wide">
<select name="formAssigned_To">
#foreach(var row in data){
<option></option>
<option value="#row.ID">#row.Assigned_To</option>
}
</select>
</div>
</li>

Can't you just use
SELECT DISTINCT Assigned_To FROM RMS WHERE 1=1
That would give you the distinct values for the dropdown, otherwise you would have to do it
from the returned results.

I figured it out! I changed my form field to this: (Which has its own SELECT DISTINCT because it was specific to this field.
<li class="form-line form-line-column" id="id_22">
<label class="form-label-top" id="label_22" for="input_22"> ADMIN </label>
<div id="cid_22" class="form-input-wide">
<select name="formAssigned_To">
<option></option>
#foreach(var row in db.Query("SELECT DISTINCT Assigned_To FROM RMS")){
<option value="#row.Assigned_To">#row.Assigned_To</option>
}
</select>
</div>
</li>
I left my selectCommand like this:
selectCommand = "SELECT * FROM RMS WHERE 1=1";
This was thanks to: CSHTML - SQL QUERY....WHERE NAME=#NameOne

Related

how to Optimize multiple searches in the table

I want to select a few fields from the table and search For example, a user records the project name, color project status, project date, and so on
For example, in the search form, I want to select the project name and color of the project status and project date, and fetch the information
But the problem is that I have to apply too many (if)
How can I select and search multiple fields with the least if ?
Please see the image and the codes and help.
if (hesabrsee.Projectid != 0 && red != 0 && yealoo != 0)
{
fraindList = fraindList.Where(p => p.Projectid == hesabrsee.Projectid && p.ControlState == red && p.ControlState == yealoo);
ViewBag.yealoo = "checked";
ViewBag.red = "checked";
return View(fraindList.ToList());
}
<div class="col-md-4 col-xs-12">
<label>نام پروژه</label>
<select class="form-control" id="form_frame" name="Projectid">
<option value="" default="" selected="">انتخاب کنید</option>
#foreach (var item in ViewBag.proList)
{
<option value="#item.projectID">#item.projectName</option>
}
</select>
</div>
You can try to use the following code:
string s = "select * from Hesabrsee where Projectid=" + hesabrsee.Projectid;
s += red != 0 ? "ControlState==red" : "";
s += green != 0 ? "ControlState==green" : "";
var fraindList = _context.Student.FromSqlRaw(s).AsEnumerable();
if (FramDate != null)
{
fraindList = fraindList.Where(l => l.ControlDate.Date >= ConvertDateTime.ConvertShamsiToMiladi(FramDate));
}
if (ToDate != null)
{
fraindList = fraindList.Where(l => l.ControlDate.Date >= ConvertDateTime.ConvertShamsiToMiladi(ToDate));
}
return View(fraindList.ToList());

How display the last n of records in MVC

i am taking last 4 four comments and want to display in the tooltip,
I am doing the below code but its showing the contents like "System.Collection.Generic.List"
var list = db.PO_Prd_Comments.Where(t => t.PO_TrgCal_ID == item.ID && t.Reply == false).OrderByDescending(t => t.PO_TrgCal_ID).Take(4).ToList();
List<string> comments = new List<string>();
if (list.Count != 0)
{
foreach (var ts in list)
{
comments.Add(ts.PrdComment);
if (list.Count == 1)
{
notify = list.SingleOrDefault().notify;
}
else
{
notify = true;
}
}
}
<td>
<a href="#" onclick="popup4(#CountID)" title="#comments"
<img src="~/Images/comment.GIF"/></a>
</td>
How i display these four comments in the tooltip.
comments is a list of string. You need to convert that to a single string and use that as the title property value.
<a href="#" onclick="popup4(#CountID)" title="#String.Join(Environment.NewLine,comments)">
<img src="~/Images/comment.GIF"/>
</a>

How can i select items from the list

I am not able to get items in the list. All I get is an item from the span. How can I get items in the select. If I don't use span in the css selector, then the script generates an error that the element is not visible.
This is the HTML of the page:
<table>
<tbody>
<tr>
<td>
<div>
<div class="select2-container select2-container-active" id="s2id_agency" style="width: 150px;">
<a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1">
<span>01_me</span>
<abbr class="select2-search-choice-close" style="display:none;"></abbr>
<div>
<b></b>
</div>
</a>
<input class="select2-focusser select2-offscreen" type="text"></div>
<select name="agency" id="agency" style="width:150px;" class="select2-offscreen" tabindex="-1">
<option value="14814">01_me</option>
<option value="14333">A_RLAgency</option>
<option value="14771">Agency_20150111183734</option>
<option value="14254">RLAgency</option>
<option value="14065">unknown_agency</option></select>
</div>
</td>
</tr>
</tbody>
This is my script:
try {
WebDriverWait wait = new WebDriverWait(driver, 30);
List<String> displayed = new ArrayList<String>();
List<String> sorted = new ArrayList<String>();
List<WebElement> verifyAgency = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By
.cssSelector("#s2id_agency > a.select2-choice > span")));
System.out.println("NUMBER OF ROWS IN THIS TABLE = "
+ verifyAgency.size());
for (WebElement element : verifyAgency) {
System.out.println("" + element.getText());
displayed.add(element.getText());
sorted.add(element.getText());
}
Collections.sort(sorted);
log.info(sorted);
if (!displayed.equals(sorted)) {
final String failedMsg = "Agency is not in alphabatical ordr.";
log.error(failedMsg, null);
boolean passed = false;
Assert.assertFalse(passed, failedMsg);
System.out.println(failedMsg);
}
} catch (Exception e) {
final String failedMsg = "Failed trying to check agency names in alphabetical order.";
log.error(failedMsg, null);
boolean passed = false;
Assert.assertFalse(passed, failedMsg);
System.out.println(failedMsg);
}
Try out with this code:
Select objSelect = new Select(dw2.findElement(By.id("agency")));
List<WebElement> lstOptions = objSelect.getOptions();
boolean sorted = false;
for (int i = 0; i < lstOptions.size() - 1; i++) {
if (lstOptions.get(i).getText()
.compareTo(lstOptions.get(i + 1).getText()) > 0) { // Checking sorting based on natural sort
sorted = false;
break;
}
sorted = true;
}
if (sorted)
System.out.println("Sorted");
else
System.out.println("Un-sorted");
You are probably trying to use visibilityOfAllElementsLocatedBy on select list options which are not visible. Infact according to your requirement you don't need them to be visible. You can alternatively use presenceOfElementLocated
and to grab the list simple SelectElement class should be enough
Note: mine is C#
IWebElement element = driver.FindElement(By.XPath("//select[#id='agency']"));
SelectElement selectList = new SelectElement(element);
IList<IWebElement> options = selectList.Options;
And, if that's a static list you can avoid all of this quirky work and simply do
IWebElement element = driver.FindElement(By.XPath("//*[#id='agency']"));
SelectElement selectList = new SelectElement(element);
var isTrue = selectList.getText().equals("01_me / A_RLAgency / Agency_20150111183734 / RLAgency / unknown_agency")
see here

fill #html.dropdownlist from other #html.dropdownlist by same table using ajax

I'm working on some search/filter functionality by using dropdownlists
the question is how can i populate the second dropdownlist by choosing item in first ddl
if all data saved in ont table.
i have table of CarType that has manufacturer, model and year.
when i choose a manufacturer in first ddl the second will filled by models of this manufacturer.
Code i have in Controller:
public ActionResult Search(string manufacturer, string carModel, string searchString, string gear)
{
using (CarTypesLogic logic = new CarTypesLogic())
{
IEnumerable<CarType> allCarTypes = logic.GetAllCarTypes();
var manufacturerList = new List<string>();
var modelList = new List<string>();
var gearList = new List<string>();
var foundManufacturer = from maker in allCarTypes
orderby maker.Manufacturer
select maker.Manufacturer;
var foundModels = from model in allCarTypes
orderby model.Model
select model.Model;
var foundGears = from transmission in allCarTypes
select transmission.Gear;
manufacturerList.AddRange(foundManufacturer.Distinct());
modelList.AddRange(foundModels.Distinct());
gearList.AddRange(foundGears.Distinct());
ViewBag.Manufacturer = new SelectList(manufacturerList);
ViewBag.Gear = new SelectList(gearList);
ViewBag.carModel = new SelectList(modelList);
var foundCars = from car in allCarTypes select car;
if (!String.IsNullOrEmpty(manufacturer))
{
foundCars = foundCars.Where(car => car.Manufacturer == manufacturer);
}
if (!String.IsNullOrEmpty(gear))
{
foundCars = foundCars.Where(car => car.Gear == gear);
}
if (!String.IsNullOrEmpty(searchString))
{
foundCars = foundCars.Where(car => car.Manufacturer.ToLower().Contains(searchString.ToLower()));
}
if (!String.IsNullOrEmpty(carModel))
{
foundCars = foundCars.Where(car => car.Model == carModel);
}
return View(foundCars);
}
}
My cshtml code:
#using (Html.BeginForm())
{
<p>
Manufacturer: #Html.DropDownList("manufacturer", String.Empty)
Model: #Html.DropDownList("carModel",String.Empty)
Gear: #Html.DropDownList("gear", String.Empty)
Free Type: #Html.TextBox("SearchString")
<br />
<input type="submit" value="Search"/>
</p>
}
you need to use Cascaded DropDownList.
here are few examples:
http://mvc4beginner.com/Sample-Code/Cascading-DropDownList/APS-.NET-MVC-Cascading-DropdownLists-Using-LINQ.html
http://code.msdn.microsoft.com/Cascading-DropDownList-in-833683f9/sourcecode?fileId=48387&pathId=451527572
http://www.itorian.com/2013/02/cascading-dropdownlist-in-aspnet-mvc.html

Adding an 'if' statement to an AJAX process in Webmatrix

Below is my code which processes an AJAX request.
Currently, when I select an area on my form page, it will send a "getresort" value to this process page, and everything works fine.
The issue comes when I don't want to search for a specific area, and instead want to display ALL resorts in ALL areas. If I select "all" in the form, it doesn't submit a value, so the WHERE clause fails as it can't get the request from the query string.
I'm assuming I need an 'if' statement in there somewhere, to check if the query string is blank, so that it runs a SQL statement that removes the WHERE AreaName = #0 part.
Where should this 'if' statement live?
#{
var db = Database.Open("StayInFlorida");
var getresortval = Request.QueryString["resval"];
var type = Request.QueryString["type"];
var res = Request.QueryString["res"];
var area = Request.QueryString["area"];
var allresorts = "SELECT ResortID, ResortName FROM ResortInfo WHERE AreaName = #0";
var qallresorts = db.Query(allresorts, getresortval);
if(type == "resortupdate") {
foreach(var row in qallresorts){
<option value='#row.ResortID'>#row.ResortName</option>
}
}
#{
var db = Database.Open("StayInFlorida");
var getresortval = Request.QueryString["resval"];
var type = Request.QueryString["type"];
var res = Request.QueryString["res"];
var area = Request.QueryString["area"];
var allresorts = "SELECT ResortID, ResortName FROM ResortInfo";
if(!getresortval.IsEmpty()){
allresorts += " WHERE AreaName = #0";
}
var qallresorts = db.Query(allresorts, getresortval);
if(type == "resortupdate") {
foreach(var row in qallresorts){
<option value='#row.ResortID'>#row.ResortName</option>
}
}