Is there a way to use parameterized queries with this code? - javascript

I've gotten this from the forum already, but one of the answers provided a way to use search parameters in a url string. Some of my tables are too big to load in a browser, so apparently I will have to find out how to add search parameters to this code. The only reason I didn't use the other code was that it showed how to do that with a calendar.
I know nothing about JSON/jQuery/razor/c#. Please help.
#{
var db = Database.Open("LGOnline");
var result = db.Query("SELECT * FROM CashOS");
var data = result.Select(x => new
{
ID = x.ID,
STORE_NO = x.STORE_NO,
DATE = x.DATE,
MWS_AMOUNT = x.MWS_AMOUNT,
FINAL_AMOUNT = x.FINAL_AMOUNT
}).ToArray();
Json.Write(data, Response.Output);
Response.ContentType = "application/json";
}
Update: I am using Microsoft WebMatrix 3. I am able to get the data from my tables with this code and it converts them to JSON. I just don't need all of it at once.
Update: I got it to do what I was trying to do. It isn't pretty, and I am open for any number of suggestions, but this made it to where I can type in the url and add &STORE_NO=55 and also i can select a date if I want to.
#{
var db1 = Database.Open("LGOnline");
var formValue2 = Request.Form["STORE_NO"];
var formValue3 = Request.Form["DATE"];
if (IsPost)
{
Response.Redirect("test.cshtml?&STORE_NO=" + formValue2 + "&DATE=" + formValue3);
}
var Keyword2 = Request.QueryString["STORE_NO"]; //Retrieves passed variable from the database search page for STORE_NO
var Keyword3 = Request.QueryString["DATE"]; //Retrieves passed variable from the database search page for DATE
var sqlQ = "SELECT * FROM CashOS WHERE STORE_NO LIKE #0 AND DATE LIKE #1";
var dataQ = db1.Query(sqlQ, "%" + Keyword2 + "%", "%" + Keyword3 + "%");
var requestedData = dataQ.Select(x => new
{
ID = x.ID,
STORE_NO = x.STORE_NO,
DATE = x.DATE,
MWS_AMOUNT = x.MWS_AMOUNT,
FINAL_AMOUNT = x.FINAL_AMOUNT
}).ToArray();
Json.Write(dataQ, Response.Output);
Response.ContentType = "application/json";
}
This is the output by the way:
[{"ID":28,"STORE_NO":55,"DATE":"/Date(1442811600000)/","MWS_AMOUNT":10.1600,"FINAL_AMOUNT":10.1600}]
Thank You all for your help!!

Related

Removing parameter values of a url in the next page using javascript only

I need to remove the values from the url after the ? in the next page the moment i click from my first page. I tried a lot of coding but could not get to a rite path. Need help.
The strings ex- Name, JobTitle and Date are dynamically generated values for ref.
Below are the links associated with the code:
Required url
file:///C:/Users/varun.singh/Desktop/www%20updated%2027.8.2015%20Old/www/Candidates/newOne.html?
Resultant url:
file:///C:/Users/varun.singh/Desktop/www%20updated%2027.8.2015%20Old/www/Candidates/newOne.html?Name=Name%201&JobTitle=Title%201&Date=Entered%20Date%201
listItem.onclick = function(){
var elementData=listData[this.id];
var stringParameter= "Name=" + elementData.name +"&JobTitle="+elementData.job_title+"&Date="+ elementData.entered_date;
//window.location.href = window.location.href.replace("ListCandidateNew", "newOne") + "?" + stringParameter;
window.location.href="file:///C:/Users/varun.singh/Desktop/www%20updated%2027.8.2015%20Old/www/Candidates/newOne.html?"
+ stringParameter;
}
This should work:
var url = file:///C:/Users/varun.singh/Desktop/www%20updated%2027.8.2015%20Old/www/Candidates/newOne.html?Name=Name%201&JobTitle=Title%201&Date=Entered%20Date%201
var index = url.lastIndexOf("?");
url = url.slice(0, index+1); // index+1 so that "?" is included
Thanks everond for trying and attempting to answer my problem. Well, i have found the solution using window.sessionStorage as i wanted by keeping the string parameter alive to pass the values. Here is the full code:
I have two pages for passing the value from one to another: ListCandidateNew.html and newOne.html
ListCandidateNew.html
listItem.onclick = function()
{
var elementData=listData[this.id];
var stringParameter= "Name=" + elementData.name +"&JobTitle="+elementData.job_title+"&Date="+ elementData.entered_date;
window.sessionStorage['Name'] = elementData.name;
window.sessionStorage['JobTitle'] = elementData.job_title;
window.sessionStorage['Date'] = elementData.entered_date;
**newOne.html**
function LoadCandidateDetail()
{
document.getElementById('Name').innerHTML = window.sessionStorage['Name'];
document.getElementById('JobTitle').innerHTML = window.sessionStorage["JobTitle"];
document.getElementById('Date').innerHTML = window.sessionStorage["Date"];
}

Get String value between two strings through javascript

I want to get the string value between ";L0|" and ";GTSet" from the following type of strings.
var test = "GP0|#9d72d96c-407f-4e45-b2e6-9361faf5808a;L0|#09d72d96c-407f-4e45-b2e6-9361faf5808a|Travel;GTSet|#ac96f075-b7d2-4e90-8dc2-da8875f395fc";
var test2 = "GP0|#15a06b93-f7aa-4dda-b0d6-7bf2d2905f27;L0|#015a06b93-f7aa-4dda-b0d6-7bf2d2905f27|Special Event;GTSet|#ac96f075-b7d2-4e90-8dc2-da8875f395fc";
Here is what i have done already.
var str = test2.match(";L0|" + "(.*?)" + ";GTSet");
alert(str[1]);
and this returns a string from the very beginning till the ";GTSet"
Jsfiddle link here
I guess you are getting this value from SharePoint Search results, right? If so, according to Automatically created managed properties in SharePoint Server 2013:
Data format for Managed Metadata.
To query for items tagged with a Managed Metadata field, you have to
use the Unique Identifier for each label. You can find the Unique
Identifier for each term in a term set in the Term Store Management
Tool, on the GENERAL tab. In addition, the data format that is used in
the query has to specify from which level in the term set the query
should apply. This specification is set by adding one of the following
prefixes to the Unique Identifier:
To query for all items that are tagged with a term: GP0|#
To query for all items that are tagged with a child of term: GPP|#
To query for all items that are tagged with a term from a term set: GTSet|#
Based on this information the following example demonstrates how to parse search result value for managed metadata:
function parseTaxonomySearchResultValue(val){
var taxValue = {TermSetGuids: [], TermValues: []};
var parts = val.split(';');
parts.forEach(function(part){
if (part.startsWith("GP0|#")) //term?
{
var termGuid = part.replace("GP0|#", "");
taxValue.TermValues.push({ TermGuid: termGuid});
}
else if (part.startsWith("GTSet|#")) //term set?
{
taxValue.TermSetGuids.push(part.replace("GTSet|#", ""));
}
else if (part.startsWith("L0|#")) //Term with label?
{
var termParts = part.replace("L0|#0", "").split('|');
var termGuid = termParts[0];
var termLabel = termParts[1];
var result = taxValue.TermValues.filter(function(tv){
return tv.TermGuid == termGuid;
});
if (result.length == 0)
taxValue.TermValues.push({TermGuid : termGuid, Label : termLabel});
else
result[0].Label = termLabel;
}
});
return taxValue;
}
//Usage
var taxValue = 'GP0|#9d72d96c-407f-4e45-b2e6-9361faf5808a;L0|#09d72d96c-407f-4e45-b2e6-9361faf5808a|Travel;GTSet|#ac96f075-b7d2-4e90-8dc2-da8875f395fc';
var taxValue = parseTaxonomySearchResultValue(taxValue);
document.getElementById('output').innerHTML = "Term info:<br/>" + "Guid= " + taxValue.TermValues[0].TermGuid + "<br/> Label= " + taxValue.TermValues[0].Label;
<div id='output'/>

Locating MAX Number in a string for a list of records

I am relatively new to JavaScript and trying to get the following achievement.
Lets say we have the following data records.
Example data records:
A_ID_R1_V1
A_ID_R1_V2
A_ID_R2_V1
Basically I am looking for two results:
1.Create a string based on record A_ID_R1_V2.
Input = A_ID_R1_V2
Output = A_ID_R1_V3
My thoughts about this is so locate the MAX V<#> for A_ID_R1_ and then add 1.
2.Create a string based on record A_ID_R2_V1.
Input = A_ID_R2_V1
Output = A_ID_R3_V1
My thoughts about this is so locate the MAX R<#> for all records and then add 1.
Thanks a lot in advance!!!!!!!!
I have strong feeling that this is some kind of school homework...
Anyhow, this will do the job, but its not sophisticated nor very effective
function updateId(letter){
var pos = (letter === 'V')?3:2;
var parts = id.split('_');
var num = parseInt(parts[pos].substring(1,parts[pos].length));
parts[pos] = letter+(num+1);
id = parts.join('_');
logVal(id);
}
try this with regex: http://jsfiddle.net/mig1098/wh6n2oqL/
newEntry = function(entry){
var str2 = entry.replace(/\d+$/,'');
var num = parseInt(entry.replace(/A_ID_R(\d+)_V/,''));
num = num+1;
return str2+num;
}

javascript toFixed(2) not working

I have this piece of code:
var desconto = document.getElementById('desconto').value;
var portes = document.getElementById('portes').value;
var pr_equipamentos = document.getElementById('pr_total_equipamentos_escondido').value;
var pr_total;
pr_total = (pr_equipamentos * ((100-desconto)/100)) + portes;
pr_total = pr_total.toFixed(2);
alert(pr_total);
document.getElementById('pr_total_proposta').innerHTML = pr_total + " €";
The ID's desconto, portes and pr_total_equipamentos_escondido are input type in a form.
In this case I'm not able to use the toFixed(2). The first formula of pr_total gives me the number: 1324.7865372846 and the next step is not working (pr_total = pr_total.toFixed(2)).
What am I doing wrong?
When you define a var, in JavaScript it is a string, so you need to parse.
try:
pr_total = parseFloat(pr_total).toFixed(2);
_txt3.value = (parseFloat(t1) - parseFloat(t2));
_txt3.value = parseFloat(_txt3.value).toFixed(2);

SQLite: Current date plus one day in where clause

I need to get 3 queries on an 'appointment' table.
where appointment.start_date is today,
where appointment.start_date = today+1 day, and
where appointment.start_date is > today+1 day.
I've got 1. down fine.
var resultSet = conn.execute('SELECT * FROM appointments WHERE date() = date(start_date)');
For 2., I've tried this:
var resultSet = conn.execute('SELECT * FROM appointments WHERE date("now", "+1day") = date(start_date, "+1day")');
I got date('now', '+1day') from this link this link but it returns the same results as the first query.
Can someone help with these queries?
If you're using exactly what's posted, I'd say you have a spacing problem
SQLite uses '+1 day' and you have posted '+1day'
Barring any explicit error messages, with the space it should work
I'm not sure for SQLite, but try next code:
var now = new Date();
var nowStr = now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
var nowPlus1 = new Date(Number(now)+24*60*60*1000);
var nowPlus1Str = nowPlus1.getFullYear()+"-"+(nowPlus1.getMonth()+1)+"-"+nowPlus1.getDate();
var resultSet = conn.execute("SELECT * FROM appointments WHERE start_date = '" + nowStr + "'");
var resultSet = conn.execute("SELECT * FROM appointments WHERE start_date = '" + nowPlus1Str + "'");
var resultSet = conn.execute("SELECT * FROM appointments WHERE start_date > '" + nowPlus1Str + "'");
Sorry totally my own idiosity my query was wrong: WHERE date("now", "+1day") = date(start_date, "+1day") should have read WHERE date("now", "+1day") = date(start_date) I had an extra +1 day. Doh!
Try with "plus one day" instead of "+1 day". Its not documented, but worked for me in another code.
var resultSet = conn.execute('SELECT * FROM appointments WHERE date("now plus one day") = date(start_date plus one day")');

Categories