Pass Parameters To Dapper List - dapper

Need to know the best practice for passing multiple parameters to a dapper list function
I need to pass the #StartDate, #EndDate, #AgentId, #Crc passed into the posted method but not sure exactly how to do it using Dapper. Thanks in advance
public List<CallSearchRepositoryBO> GetCallSearchRecords(DateTime startDate, DateTime endDate, int agentId, string crc)
{
try
{
using (IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CCWorkforceConnectionString"].ConnectionString))
{
var items = connection.Query<CallSearchRepositoryBO>
("SELECT h.historyid " +
" ,h.Dialid " +
" ,h.Crc " +
" ,c.szExternalId ExternalId " +
" ,a.AgentId " +
" ,UPPER(a.LastName +',' +a.FirstName) Agent" +
" ,c.ContactId" +
" ,CallDateTime" +
" ,h.ProjName Campaign" +
" ,h.PhoneNum PhoneNumber" +
" ,szQ01 Q1" +
" ,szQ02 Q2" +
" ,szQ03 Q3" +
" ,szQ04 Q4" +
" ,szQ05 Q5" +
" ,szQ06 Q6" +
" ,szQ07 Q7" +
" ,szQ08 Q8" +
" ,szQ09 Q9" +
" ,szQ10 Q10" +
" ,txtQ33 Comments" +
" ,szFaxType AlertType" +
" ,r.VoxFilePath+VoxFileName RecordingPath" +
" ,r.RecLength" +
" ,CASE WHEN DataLength(txtQ33) > 0 THEN 1 ELSE 0 END HasComments " +
" FROM Touchstar..History h" +
" INNER JOIN Touchstar..Agent a WITH(NOLOCK) ON h.AgentId = a.AgentId" +
" INNER JOIN Touchstar..Recording r WITH(NOLOCK) ON h.HistoryId = r.HistoryId" +
" LEFT JOIN Touchstar..Contact c WITH(NOLOCK) ON h.DialId = c.DialId" +
" LEFT JOIN Touchstar..Sales s WITH(NOLOCK) ON c.ContactId = s.ContactId" +
" WHERE h.CallDateTime BETWEEN #StartDate AND #EndDate" +
" AND h.AgentId=#AgentId"+
" AND h.Crc=#Crc," +
" AND c.szCampaignId IN('UP2','UP4')" +
" ORDER BY CallDateTime DESC").ToList();
return items;
}
}
catch (Exception)
{
throw;
}
}

Pass through an object as a second parameter to the Query function. Notice the extra parameter in the below code "new {StartDate = startDate, EndDate = endDate, AgentId = agentId, Crc = crc}"
var items = connection.Query<CallSearchRepositoryBO>
("SELECT h.historyid " +
" ,h.Dialid " +
" ,h.Crc " +
" ,c.szExternalId ExternalId " +
" ,a.AgentId " +
" ,UPPER(a.LastName +',' +a.FirstName) Agent" +
" ,c.ContactId" +
" ,CallDateTime" +
" ,h.ProjName Campaign" +
" ,h.PhoneNum PhoneNumber" +
" ,szQ01 Q1" +
" ,szQ02 Q2" +
" ,szQ03 Q3" +
" ,szQ04 Q4" +
" ,szQ05 Q5" +
" ,szQ06 Q6" +
" ,szQ07 Q7" +
" ,szQ08 Q8" +
" ,szQ09 Q9" +
" ,szQ10 Q10" +
" ,txtQ33 Comments" +
" ,szFaxType AlertType" +
" ,r.VoxFilePath+VoxFileName RecordingPath" +
" ,r.RecLength" +
" ,CASE WHEN DataLength(txtQ33) > 0 THEN 1 ELSE 0 END HasComments " +
" FROM Touchstar..History h" +
" INNER JOIN Touchstar..Agent a WITH(NOLOCK) ON h.AgentId = a.AgentId" +
" INNER JOIN Touchstar..Recording r WITH(NOLOCK) ON h.HistoryId = r.HistoryId" +
" LEFT JOIN Touchstar..Contact c WITH(NOLOCK) ON h.DialId = c.DialId" +
" LEFT JOIN Touchstar..Sales s WITH(NOLOCK) ON c.ContactId = s.ContactId" +
" WHERE h.CallDateTime BETWEEN #StartDate AND #EndDate" +
" AND h.AgentId=#AgentId"+
" AND h.Crc=#Crc," +
" AND c.szCampaignId IN('UP2','UP4')" +
" ORDER BY CallDateTime DESC", new {StartDate = startDate, EndDate = endDate, AgentId = agentId, Crc = crc}).ToList();

Related

Inserting into a table snowflake with a passed variable

I have a table which has 3 attributes
ATTRIBUTE DATATYPE FLAG( 0 OR 1)
My stored procedure takes a string of records in the form
"Att1 Datatype1 1, Att2 Datatype2 0, Att3 Datatype3 0...)
so for example a passed string can be
"DATABASE VARCHAR 1, SCHEMA VARCHAR 1, TIMESTAMP TIMESTAMP 0"
My code gets the column names of the table and stores it in a string
My code takes the passed string and puts them into array elements seperated by the ,
However the issue arrises when I try to do an insertion into my table
I keep getting the error "invalid Identifier"
CREATE OR REPLACE PROCEDURE "ADMINDB"."TOOLKIT".ADD_ATTRIBUTES_SESSION_META ("P_ATTRIBUTE_DATATYPE_FLAG" VARCHAR(16777216))
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
COMMENT='Add Attributes to the table SESSION_ATTRIBUTES_META'
EXECUTE AS CALLER
AS
$$
var v_list = P_ATTRIBUTE_DATATYPE_FLAG;
var arr_list = [];
arr_list = P_ATTRIBUTE_DATATYPE_FLAG.split(',');
var v_string;
var arr_col_att = [];
var v_sqlCode = `SELECT * FROM ` + "ADMINDB" + "." + "TOOLKIT" + "." + "SESSION_ATTRIBUTES_META";
try{
var sqlStmt = snowflake.createStatement({sqlText : v_sqlCode});
var sqlRS = sqlStmt.execute();
}catch(err){
errMessage = "Failed: Code: " + err.code + "\n State: " + err.state;
errMessage += "\n Message: " + err.message + v_sqlCode;
errMessage += "\nStack Trace:\n" + err.stackTraceTxt + v_sqlCode;
throw 'Encountered error in executing v_sqlCode. \n' + errMessage;
}
for (i = 1; i <= sqlStmt.getColumnCount(); i++) {
arr_col_att.push(sqlStmt.getColumnName(i));
}
arr_col_att[0] = arr_col_att[0].replace(/\s/g, ',');
var v_col_att = arr_col_att.toString();
v_string = arr_list[0].toString();
v_string = v_string.replace(/\s/g, ',');
v_sqlCode = `INSERT INTO ` + "ADMINDB" + "." + "TOOLKIT" + "."
+ "SESSION_ATTRIBUTES_META" + `(` + v_col_att + `) VALUES( ` + v_string + `)`;
try{
var sqlStmt = snowflake.createStatement({sqlText : v_sqlCode});
var sqlRS = sqlStmt.execute();
}catch(err){
errMessage = "Failed: Code: " + err.code + "\n State: " + err.state;
errMessage += "\n Message: " + err.message + v_sqlCode;
errMessage += "\nStack Trace:\n" + err.stackTraceTxt + v_sqlCode;
throw 'Encountered error in executing v_sqlCode. \n' + errMessage;
}
return "SUCCESS!";
$$;
CALL "ADMINDB"."TOOLKIT".ADD_ATTRIBUTES_SESSION_META('DATABASE VARCHAR 1,SCHEMA VARCHAR 1,TIMESTAMP TIMESTAMP 0');
Modifying line 33 and 36, fixes the issue:
CREATE OR REPLACE PROCEDURE "ADMINDB"."TOOLKIT".ADD_ATTRIBUTES_SESSION_META ("P_ATTRIBUTE_DATATYPE_FLAG" VARCHAR(16777216))
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
COMMENT='Add Attributes to the table SESSION_ATTRIBUTES_META'
EXECUTE AS CALLER
AS
$$
var v_list = P_ATTRIBUTE_DATATYPE_FLAG;
var arr_list = [];
arr_list = P_ATTRIBUTE_DATATYPE_FLAG.split(',');
var v_string;
var arr_col_att = [];
var v_sqlCode = `SELECT * FROM ` + "ADMINDB" + "." + "TOOLKIT" + "." + "SESSION_ATTRIBUTES_META";
try{
var sqlStmt = snowflake.createStatement({sqlText : v_sqlCode});
var sqlRS = sqlStmt.execute();
}catch(err){
errMessage = "Failed: Code: " + err.code + "\n State: " + err.state;
errMessage += "\n Message: " + err.message + v_sqlCode;
errMessage += "\nStack Trace:\n" + err.stackTraceTxt + v_sqlCode;
throw 'Encountered error in executing v_sqlCode. \n' + errMessage;
}
for (i = 1; i <= sqlStmt.getColumnCount(); i++) {
arr_col_att.push(sqlStmt.getColumnName(i));
}
arr_col_att[0] = arr_col_att[0].replace(/\s/g, ',');
var v_col_att = arr_col_att.toString();
v_string = arr_list[0].toString();
v_string = v_string.replace(/\s/g, "','" );
v_sqlCode = `INSERT INTO ` + "ADMINDB" + "." + "TOOLKIT" + "."
+ "SESSION_ATTRIBUTES_META" + `(` + v_col_att + `) VALUES( '` + v_string + `' )`;
try{
var sqlStmt = snowflake.createStatement({sqlText : v_sqlCode});
var sqlRS = sqlStmt.execute();
}catch(err){
errMessage = "Failed: Code: " + err.code + "\n State: " + err.state;
errMessage += "\n Message: " + err.message + v_sqlCode;
errMessage += "\nStack Trace:\n" + err.stackTraceTxt + v_sqlCode;
throw 'Encountered error in executing v_sqlCode. \n' + errMessage;
}
return "SUCCESS!";
$$;
CALL "ADMINDB"."TOOLKIT".ADD_ATTRIBUTES_SESSION_META('DATABASE VARCHAR 1,SCHEMA VARCHAR 1,TIMESTAMP TIMESTAMP 0');
As far I see, you haven't implemented the process multiple records in the argument for now, but I think you can handle it.

fnDrawCallback how to loop through each row

How do i loop through the enitre datset within the in datatables method?
"fnFooterCallback": function (nFoot, aData, iStart, iEnd, aiDisplay) {
$(aData).each(function (index) {
$(".it").append("<div class='mapdata' id='" + nFoot + "B' title='" + aData[index][14] + "|" + aData[index][15] + "' >");
});
},
The above works if the pagination is removed. But if iDisplayLength is set to 25 for example then it only goes through 25 records, not all 100 etc.
"fnFooterCallback": function (nFoot, aData, iStart, iEnd, aiDisplay) {
$(aData).each(function (index) {
$(".it").append("<div class='mapdata' id='" + nFoot + "B' title='" + aData[index][14] + "|" + aData[index][15] + "' data-id='true' data-id2='" + aData[index][1] + "' data-isapprove='" + aData[index][12] + "' ><div class='gTab1'><div class='info'><div class='maptitle'>" + aData[index][1] + "</div><div class='location'></div></div></div ></div >");
});
},
The above did it

Iterating through nested arrays with hyphen in object name

First of all, I know naming array objects with hyphens is completely incorrect, and I'm not the creator of this. I need to call an API within a service, and many objects are named improperly, like {"children-education": [ and { "Kid Stories": [.
I have tried assigning the name to a variable like let edChild = "child-education"and then parsing it to an object, like edChild = JSON.parse(edChild)to no avail. I really have no idea of what I'm doing, nor even if it's possible to do so.
I kinda have the option to call my customer and kindly ask his team to rename the objects to something less... stupid than special characters than I can't possible call in Typescript, but I'd like to learn if there's a way to surpass this in future occasions or if they can't rename it.
Here's an example of the JSON I'm trying to iterate through:
{
"business":
[
{
"anim":
[
{
"child-education": [
{
"Kid Stories": [
{
"id": 1,
"name": "Three Little Pinkies",
"url": "#",
"description": "shows how the world is beautiful"
},
(... and so on)
Thanks in advance.
You can parse that JSON string like any JSON string, using JSON.parse()
let str = "{\n" +
" \"business\":\n" +
" [\n" +
" {\n" +
" \"anim\":\n" +
" [\n" +
" {\n" +
" \"child-education\": [\n" +
" {\n" +
" \"Kid Stories\": [\n" +
"\n" +
" {\n" +
" \"id\": 1,\n" +
" \"name\": \"Three Little Pinkies\",\n" +
" \"url\": \"#\",\n" +
" \"description\": \"shows how the world is beautiful\"\n" +
" }]}]}]}]}" +
""
console.log(JSON.parse(str));
After that, you can use bracket notation to access any property names
let str = "{\n" +
" \"business\":\n" +
" [\n" +
" {\n" +
" \"anim\":\n" +
" [\n" +
" {\n" +
" \"child-education\": [\n" +
" {\n" +
" \"Kid Stories\": [\n" +
"\n" +
" {\n" +
" \"id\": 1,\n" +
" \"name\": \"Three Little Pinkies\",\n" +
" \"url\": \"#\",\n" +
" \"description\": \"shows how the world is beautiful\"\n" +
" }]}]}]}]}" +
""
const obj = JSON.parse(str);
console.log(obj.business[0].anim[0]['child-education'][0]['Kid Stories'][0]);

How to use radio button in html and javascript to insert into the database in CN1

I was trying to get the values of radio button in html page when checked and insert it into the sqlite database and mysql. This is my full code below:
Form hi = new Form("Hi World");
final WebBrowser b = new WebBrowser(){
#Override
public void onLoad(String url) {
// Placed on onLoad because we need to wait for page
// to load to interact with it.
BrowserComponent c = (BrowserComponent)this.getInternal();
// Create a Javascript context for this BrowserComponent
JavascriptContext ctx = new JavascriptContext(c);
JSObject window = (JSObject)ctx.get("window");
window.set("addAsync", new JSFunction(){
public void apply(JSObject self, final Object[] args) {
String a = (String)args[0];
// Double b = (Double)args[1];
System.out.println("Value picked is "+a);
JSObject callback = (JSObject)args[1];
callback.call(new Object[]{new String(a)});
System.out.println("ok Good");
}
});
}
};
b.setPage( "<html lang=\"en\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\">\n" +
" <script>\n" +
"function test() {"+
" var radios = document.getElementsByName('radiotest');"+
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
"}"+
// + \n" +
" var radios = document.getElementsByName('radiotest')"+
" document.addEventListener('click', function(){\n" +
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
" a = document.getElementById('val1').value ;\n" +
" b = document.getElementById('val2').value\n" +
" window.addAsync(a, b, function(result){\n" +
" document.getElementById('result').innerHTML = result;\n" +
" });\n" +
" }, true); "
+ "" +
" </script>\n" +
" </head>\n" +
" <body >\n" +
" <table border='0' cellspacing = '15'>"+
" <thead>"+
" </thead>"+
" <tbody>"+
" <tr>"+
" <td><font size = '3'> <b>A </font></b><input type='radio' name='radiotest' id='val1' onclick='test()' value='1' style='border: none;'> value1</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td><font size = '3'> <b> B</font></b><input type='radio' name='radiotest' id='val2' onclick='test()' value='2' style='border: none;'> value2</input>"+
"</td>"+ " </tr>"+
" <tr>"+
" <td> <font size = '3'> <b>C </font></b><input type='radio' name='radiotest' id='val3' onclick='test()' value='3'style='border: none;'> value3</input>"+
" </tr>"+
"<tr>"+
" <td> <font size = '3'> <b>D </font></b> <input type='radio' name='radiotest'id='val4' onclick='test()' value='4'style='border: none;'> value4</input>"+
" </tr>"+
"</tbody>"+
"</table>"+
"<span id=\"result\"></span>"+
" </body>\n" +
"</html>", null);
hi.setLayout(new BorderLayout());
hi.setLayout(new BorderLayout());
hi.addComponent(BorderLayout.CENTER, b);
hi.show();
The code above did not work for me. Pls help.
The problem am having now is to add actionListener to the radio button when checked by collecting its value and insert these values into the database accordingly.
I have edited my code, With this I can get the value selected with javascript function with the code below. Now, How can I insert the values of the radio button into the database as I click on radio button.
Form hi = new Form("BrowserComponent", new BorderLayout());
BrowserComponent bc = new BrowserComponent();
bc.setPage( "<html lang=\"en\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\">\n" +
" </head>\n" +
" <body >\n" +
" <table border='0' cellspacing = '15'>"+
" <thead>"+
" </thead>"+
" <tbody>"+
" <tr>"+
" <td><font size = '3'> <b>A </font></b><input type='radio' name='radiotest' id = 'val1' onclick='test()' value='1' style='border: none;'> value1</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td><font size = '3'> <b> B</font></b><input type='radio' name='radiotest'id = 'val2' onclick='test()' value='2' style='border: none;'> val2</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td> <font size = '3'> <b>C </font></b><input type='radio' name='radiotest' onclick='test()' value='3'style='border: none;'> val3</input>"+
" </tr>"+
"<tr>"+
" <td> <font size = '3'> <b>D </font></b> <input type='radio' name='radiotest' onclick='test()' value='4'style='border: none;'> val4</input>"+
" </tr>"+
"</tbody>"+
"</table>"+
" <script type='text/javascript'>" +
//This javascript is perfect now.
"function test() {"+
" var radios = document.getElementsByName('radiotest');"+
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
// " instruction = document.getElementById('input3').value" +
// "'"+option_code2+"'"+
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
"}"+
"</script>"+
" </body>\n" +
"</html>", null);
TextField tf = new TextField();
hi.add(BorderLayout.CENTER, bc).
add(BorderLayout.SOUTH, tf);
//bc.addWebEventListener("onLoad", (e) -> bc.execute("fnc('<p>Hello World</p>')"));
hi.show();
Make sure your libraries are up to date, if the JavaScript bridge works correctly for setURL but fails for setPage it sounds like something we fixed in a recent update.

Syntax error in VBA SQL

Getting data from various text boxes on a form and doing an update to a SQL Server database. Here is the creation of the SQL:
Dim upDateStr As String
upDateStr = "UPDATE dbo_Master_Accounts SET dbo_Master_Accounts.FirstName = " & Chr$(34) & Me.disFirstName & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.LastName = " & Chr$(34) & Me.disLastName & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.Address_Line_1 = " & Chr$(34) & Me.disAddr1 & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.City = " & Chr$(34) & Me.disCity & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.State = " & Chr$(34) & Me.disState & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.PostalCode = " & Chr$(34) & Me.disPostalCode & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.Phone_Number_1 = " & Chr$(34) & Me.disHomePhone & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.Phone_Number_2 = " & Chr$(34) & Me.disCellPhone & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.Gender = " & Chr$(34) & Me.disGender & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.Date_Of_Birth = " & Chr$(34) & Me.disDateofBirth & Chr$(34) & ", "
upDateStr = upDateStr + "dbo_Master_Accounts.Email = " & Chr$(34) & Me.disEmailAddress & Chr$(34) & ", "
upDateStr = upDateStr + "WHERE (((dbo_Master_Accounts.Master_ID) = " & Chr$(34) & Me.frmoldCardno & Chr$(34) & ""
Looked at query in immediate and all the data is there and it looks correct. Here is the immediate window.
UPDATE dbo_Master_Accounts
SET dbo_Master_Accounts.FirstName = "John",
dbo_Master_Accounts.LastName = "Handy",
dbo_Master_Accounts.Address_Line_1 = "123 From",
dbo_Master_Accounts.City = "Somewhere",
dbo_Master_Accounts.State = "IL",
dbo_Master_Accounts.PostalCode = "50310",
dbo_Master_Accounts.Phone_Number_1 = "1234567890",
dbo_Master_Accounts.Phone_Number_2 = "",
dbo_Master_Accounts.Gender = "M",
dbo_Master_Accounts.Date_Of_Birth = "02/14/1967",
dbo_Master_Accounts.Email = "me#mine.com",
WHERE (((
dbo_Master_Accounts.Master_ID
) = "000055"
But I get a syntax error that I can't see. Tried running with only first and last two line of code and get the same error.
Thanks in advance
jpl
Start by trying this to see if it works (removed comma before where, and fixed the parenthesis) and then use parameters to do it right, because this way you're open to SQL injection attacks
Dim upDateStr As String
upDateStr = "UPDATE dbo_Master_Accounts SET dbo_Master_Accounts.FirstName = '" & Me.disFirstName & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.LastName = '" & Me.disLastName & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.Address_Line_1 = '" & Me.disAddr1 & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.City = '" & Me.disCity & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.State = '" & Me.disState & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.PostalCode = '" & Me.disPostalCode & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.Phone_Number_1 = '" & Me.disHomePhone & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.Phone_Number_2 = '" & Me.disCellPhone & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.Gender = '" & Me.disGender & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.Date_Of_Birth = '" & Me.disDateofBirth & "', "
upDateStr = upDateStr + "dbo_Master_Accounts.Email = '" & Me.disEmailAddress & "' "
upDateStr = upDateStr + "WHERE (dbo_Master_Accounts.Master_ID) = '" & Me.frmoldCardno & "' "

Resources