I am trying to write a query to show all records owned by the current logged on user but i am having issues inserting the "userid" variable into the string?
#{
Layout = "~/_template1.cshtml";
var db = Database.Open("StayInFlorida");
var userid = WebSecurity.CurrentUserId;
var premierproperty = "SELECT PropertyName, PropertyID FROM PropertyInfo WHERE OwnerID='userid'";
}
<h1>Properties - Page coming soon</h1>
#userid
#foreach (var row in db.Query(premierproperty)){
#row.propertyname
}
Any ideas?
Try like this:
#{
Layout = "~/_template1.cshtml";
var db = Database.Open("StayInFlorida");
var userid = WebSecurity.CurrentUserId;
var premierproperty = "SELECT PropertyName, PropertyID FROM PropertyInfo WHERE OwnerID = #0";
}
<h1>Properties - Page coming soon</h1>
#userid
#foreach (var row in db.Query(premierproperty, userid))
{
#row.propertyname
}
Related
**I was helped with this code in my earlier post but it still did not work, instead it returns the default snowflake table like 7 times on one call.**
create or replace procedure FIND_EMPTY_TABLES(DATABASE_PATTERN string)
returns variant
language javascriptexecute as owner as $$ class Account {constructor(databases){this.databases = databases;}} class Database {constructor(name) {this.name = name;}} class Query{constructor(statement){this.statement = statement;}} var account = getDatabasesInAccount(DATABASE_PATTERN); var out = []; for (var i = 0; i < account.databases.length; i++) { out = out.concat(rsToJSON(getQuery(select TABLE_NAME, TABLE_CATALOG, TABLE_SCHEMA, TABLE_OWNER,ROW_COUNT
from ${account.databases[i].name}.INFORMATION_SCHEMA.TABLES
where TABLE_TYPE = 'BASE TABLE' and ROW_COUNT = 0))); } return out; //------ function getQuery(sql){ cmd1 = {sqlText: sql}; var query = new Query(snowflake.createStatement(cmd1)); query.resultSet = query.statement.execute(); return query; } function executeSingleValueQuery(columnName, queryString) { cmd = {sqlText: queryString}; stmt = snowflake.createStatement(cmd); var rs; rs = stmt.execute(); rs.next(); return rs.getColumnValue(columnName); } function getDatabasesInAccount(databasePattern){ const SYSTEM_DB_NAMES = ["SNOWFLAKE", "SNOWFLAKE_SAMPLE_DATA"]; var db = executeSingleValueQuery("name", "show databases"); var i = 0; var dbRS = getResultSet(select DATABASE_NAME from "${db}".INFORMATION_SCHEMA.DATABASES
where rlike (DATABASE_NAME, '${databasePattern}');`);
var databases = [];
var db;
while (dbRS.next()){
db = new Database(dbRS.getColumnValue("DATABASE_NAME"));
if (!SYSTEM_DB_NAMES.includes(db)) {
databases.push(db);
}
}
return new Account(databases);
}
function getResultSet(sql){
let cmd = {sqlText: sql};
let stmt = snowflake.createStatement(cmd);
let rs = stmt.execute();
return rs;
}
function rsToJSON(query) {
var i;
var row = {};
var table = [];
while (query.resultSet.next()) {
for(col = 1; col <= query.statement.getColumnCount(); col++) {
row[query.statement.getColumnName(col)] = query.resultSet.getColumnValue(col);
}
table.push(row);
}
return table;
}
$$;
All you need to do is read the SNOWFLAKE.ACCOUNT_USAGE.TABLES view where ROW_COUNT = 0
You can make use of the ACCOUNT_USAGE view TABLES:
https://docs.snowflake.com/en/sql-reference/account-usage/tables.html
SELECT TABLE_CATALOG, TABLE_NAME
FROM "SNOWFLAKE"."ACCOUNT_USAGE"."TABLES"
WHERE ROW_COUNT = 0
But you should be aware that there could be some latency.
This is a duplicate of proc or function that will loop over each database in snowflake and list tables with empty rows and I wrote a stored procedure there to do this.
I want to update a record. But every time i tried to update. It goes to catch statement. I am unable to find that where my query syntax is invalid.
It always goes to catch statement
I have no idea that either the query is wrong or any error is in the syntax.
if (IsPost) {
var value = Request.Form["value"];
var Student_Reg_No = Request.Form["Student_Reg_No"];
var Student_Name = Request.Form["Student_Name"];
var Father_Name = Request.Form["Father_Name"];
var Temporary_Address = Request.Form["Temporary_Address"];
var Permanent_Address = Request.Form["Permanent_Address"];
var Phone_No = Request.Form["Phone_No"];
var Blood_Group = Request.Form["Blood_Group"];
var Email_Address = Request.Form["Email_Address"];
if (Validation.IsValid()){
try{
var db = Database.Open("site_data");
var updateCommand = "Update site_data_table set Student_Reg_No =#0, Student_Name=#1, Father_Name=#2, Temporary_Address=#3,Permanent_Address=#4, Phone_No=#5, Blood_Group=#6, Email_Address=#7 where Id=#8)";
db.Execute(updateCommand, Student_Reg_No, Student_Name, Father_Name, Temporary_Address, Permanent_Address, Phone_No, Blood_Group, Email_Address, value);
Response.Redirect("~/edit?id=success");
}
catch{
<textarea>Data is Not updated</textarea>
}
}
}
var updateCommand = "Update site_data_table set Student_Reg_No =#0, Student_Name=#1, Father_Name=#2, Temporary_Address=#3,Permanent_Address=#4, Phone_No=#5, Blood_Group=#6, Email_Address=#7 where Id=#8)";
Change to
var updateCommand = "Update site_data_table set Student_Reg_No =#0, Student_Name=#1, Father_Name=#2, Temporary_Address=#3,Permanent_Address=#4, Phone_No=#5, Blood_Group=#6, Email_Address=#7 where Id=#8";
I have this method and I have not been able to order my result. It wont order at all.
Do you know what am doing wrong ?
public IEnumerable<SelectListItem> GetBranches()
{
List<SelectListItem> objList = null;
var strQuery = (from sl in _objDataCollection.Edmm_Tester
orderby sl.Code
select new SelectListItem() {Text = sl.Name, Value = sl.Code}).Distinct();
objList = strQuery.ToList();
return objList;
}
use this:
objList = strQuery.OrderBy(e=>e.Code).ToList();
Or this:
var strQuery = (from sl in _objDataCollection.Edmm_Tester
select new SelectListItem()
{
Text = sl.Name
, Value = sl.Code
}).OrderBy(e=>e.Code).Distinct();
I thought this would be the proper code to replace a null but it gives me an error "The name 'subt2' does not exist in the current context"
var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=#0";
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
if(sub2 == null){
var subt2 = 0.00;
}
else{
var subt2 = sub2;
}
and in the page I have #subt2
I decided to try to explain what I am doing to hopefully get a better response. I have a page that you can add products to. It makes an entry into the table with the TransID as a reference to this page. This page pulls the SUM from Price where rows have this TransID. The problem lies when there is not a product entered in the table with this TransID. I need it to output a 0.00 when there is not data to be displayed.
took me forever but I found a few articles and put them together. This is the code:
var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=#0";
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
if(String.IsNullOrEmpty(Convert.ToString(sub2)))
{
sub2 = 0.00;
}
I would try to replace it as follows:
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
var result = sub2 == null ? 0 : sub2;
return result;
When you define a variable by using a var (or an int, double, string, etc) it only exists in within the braces {} where it is defined. So since you are defining it inside the braces, it's not available later in the page. Better would be:
var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=#0";
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
var subt2 = 0;
if(sub2 == System.DBNull){
subt2 = 0.00;
}
else
{
subt2 = sub2;
}
TSQL:-
Update table1
Set Name = 'John',
Address = null
where
ID = 1
LINQ-TO-SQL
var tab = db.Table1.Single(s => s.ID == 3);
tab.Name = DateTime.Now;
tab.Address = null;
db.SubmitChanges();
There isn't a single LINQ to SQL statement for updates. You have to retrieve the object, modify it, then save the changes (code assumes a single row since you have a specific Id):
var entity = context.Table1.Single(t => t.Id == 1);
entity.Name = "John";
entity.Address = "Toronto";
context.SubmitChanges();
using (var dataContext = new MyEntities())
{
var contact = Contacts.Single (c => c.ContactID == 1);
contact.FirstName = 'John';
contact.Address= 'Toronto';
dataContext.SaveChanges();
}