Selecting an id from database using razor pages? - asp.net

I am the following. As a user registers his account name and password are put into an existing table. Then his id gets read from that table and another table is made using that id as the tablename.
Now inserting works great but reading the Id from that table seems to give me some problems.
Now it would be great if I was even able to get that id to display on my webpage. i feel like when I am able to do that I can just do something like CREAT TABLE + #Id + etc.
This is what I have so far
var gebruikersnaam = Request.Form["registnaam"];
var wachtwoord = Request.Form["registpassword"];
if (IsPost && Request.Form["registreren"] == "Registreren")
{
var insertCommand = db.Execute("INSERT INTO Accounts (gebruikersnaam, wachtwoord) VALUES(#0, #1)", gebruikersnaam, wachtwoord);
var id = db.Query("SELECT * FROM Accounts WHERE gebruikersnaam = #0", gebruikersnaam);
Response.Write(#id);
}
And this is what the Response.Write(#id) shows me on the webpage:
System.Collections.ObjectModel.ReadOnlyCollection`1[System.Object]

Try merging the create and select query into one.
INSERT INTO Accounts (gebruikersnaam, wachtwoord)
OUTPUT Inserted.Id
VALUES('value1', 'value2')
Getting the Id using a separate query could be a problem in the future especially if the data becomes large, doing this optimize the speed of data retrieval in DB like getting the Id of an inserted record.

Related

How to get live table row count in DynamoDb programmatically in ASP.NET Core?

I wrote something like below code in my ASP.NET WEB API. I want to get the live row count t0 display in my application. The problem with the below code is it's showing Scanned count as 7134. But actual value is in millions.
var cancellationToken = new CancellationToken();
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
var request = new ScanRequest
{
TableName = "exampleTable",
Select = Select.COUNT
};
var response = client.ScanAsync(request, cancellationToken).Result;
var totalCount = response.Count.ToString();
return totalCount;
What do you mean by 'live' row count?
Also, if you have only a few pieces of code that insert into your Dynamo table you could insert a record specifically for maintaining count, and then update this record's counter whenever inserting / deleting from the table.. It's more work, but may be better than incurring the costs involved in scanning millions of records, multiple times a day.

Best way for displaying total Pages for a datasource in Appmaker

I have a Google drive table data source which stores list of open positions. Now in the data source I've set "Query per size" field to 10 so that I can get 10 records per page. I've added a Pager as well to show pagination.
My query is I want to display like "Page 1 of X" to my end users and this X will vary based on certain search filters. What will the best way to achieve this in Appmaker?
I've tried counting total records in a data source as per below code but every time updating that with the search criteria and recounting it is not a proper solution.
//Server side
var newQuery = app.models.Company.newQuery();
var records = newQuery.run();
var totalCount =0;
for(var i=0;i<records.length;i++)
{
totalCount=totalCount+1;
}
return totalCount;
In case you don't have any filters in your table your server code can be as simple as
// Server script
function getPagesCount(pageSize) {
var recordsCount = app.models.MyModel.newQuery().run().length;
var pagesCount = Math.ceil(recordsCount / pageSize);
return pagesCount;
}
As an alternative you can consider creating Calculated Model with a single field PagesCount.
In case you have some filters associated with the table then you'll need to run the query for the pages number with exact same filters.
Most likely the entire setup will not work effectively with Drive Tables since there is no way to query records number without querying records themselves. With Cloud SQL data backend one can create Calculated SQL Model with lightweight native SQL query (here :PageSize is query parameter which should be equal to the query.limit of the actual datasource):
SELECT
Ceil(COUNT(1) / :PageSize) AS RecordsNumber
FROM
TableName
WHERE
...
I've achieved this using Calculated Model as suggested by Pavel.
Steps :
Create a calculated data source with one field count.
In that data source add one parameter searchQuery. This will contain users filter going forward. Currently I have only one search query in which user can search many things. So I've added one parameter only.
In this data source add following server script.
Code:
// Server script
function getTotalRecords(query) {
var receivedQuery = query.parameters.searchQuery;
// console.log('Received query:' + query.parameters.searchQuery);
var records = app.models.Company.newQuery();
records.parameters.SearchText = query.parameters.searchQuery;
if(receivedQuery !== null) {
records.where = '(Name contains? :SearchText or InternalId contains? ' +
':SearchText or LocationList contains? :SearchText )';
}
var recordsCount = records.run().length;
var calculatedModelRecords = [];
var draftRecord = app.models.RecordCount.newRecord();
draftRecord.count = ''+recordsCount;
calculatedModelRecords.push(draftRecord);
return calculatedModelRecords;
}
.
On the Appmaker page bind a label with this data source.
On search query/your filter applied event add following code which Reload this data source and assign value to Parameter.
// Client script
function updateRecordCount(newValue) {
var ds = app.datasources.RecordCount;
ds.query.parameters.searchQuery = newValue;
ds.unload();
ds.load();
}

How do I do a Global Update in Firebase?

I'm trying to wrap my head around firebase, and one issue I can't seem to work out is how mass updates occur.
Lets say I have 1,000 users and they all have "user_role": "member"
And I decide to update all 1,000 users to "user_role":"paid-member"
in SQL I can just do UPDATE users SET user_role = 'paid-member' ... So what's the Firebase equivalent?
Or more importantly, how would I accomplish something like this in firebase...
UPDATE users SET user_role = 'paid-member' WHERE user_role = 'member'
Unfortunately, it's not possible to do this without knowing the list of users beforehand. If you had all the usernames in an array, you could do something like this:
var usernames = [...];
for (var i = 0; i < usernames.length; i++) {
var ref = new Firebase("https://myfirebase.firebaseio.com/users/" + usernames[i]);
ref.child("user_role").set("paid-member");
}
You'll have to retrieve the list of usernames first using a child_added or value event handler.

Linq to Entities: can't get collection query to work

I'm struggling to get a collection of records using L2E. Here's the model view:
http://pascalc.nougen.com/stuffs/aspnet_linq_model2.png
I have a user identifier, which is associated to 1 or many UserGroup which themselves are linked to TestCase. I would like to get all TestCases of all groups the user id X is associated to.
I also notice that I don't get all Project for users that are associated to 2 (or more).
Here's how I do so far:
QASModel.QASEntities qasEntities = new QASModel.QASEntities();
QASModel.User authenticatedUserEntity = (from u in qasEntities.Users
where u.ID.Equals(authenticatedUserId)
select u).FirstOrDefault();
// Get the authenticated user usergroups
var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();
// Get all testcases of all user group the authenticated user is associated to
var allTestcases = usergroup.TestCases;
// Get the authenticated user projects based on it's usergroup(s)
var authenticatedUserProjects = usergroup.Projects;
authenticatedUserProjects give back only 1 project, where the user is linked to 2 projects.
And allTestcases gives back no result, although there are about 8 entries in TestCases associated to a project associated to one of the same UserGroup the user belongs to.
Thanks
I think your problem is in this line:
var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();
Shouldn't your code get all UserGroups of that User? The above line will return only 1 UserGroup, this is, if the user belongs to more than 1 UserGroup the 2nd one won't be returned.
To correct this:
var userTestCases = new List<TestCase>();
var userProjects = new List<Project>();
foreach(UserGroup ug in authenticatedUserEntity.UserGroups)
{
userTestCases = userTestCases.Concat(ug.TestCases);
// Get the authenticated user projects based on it's usergroup(s)
userProjects = userProjects.Concat(ug.Projects);
...
}
var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();
probably is wrong, since you want all usergroups the user is associated with.
In any case you can easily revert the query to work like this
var testcases = from tc in new QASModel.QASEntities().TestCases
where tc.UserGroup.UserId == USERID
select tc
This way you won't have to execute multiple queries to get all test cases, each FirstOrDefault or ToList or ForEach will actually execute the query.

How can I insert from multiple tables into one?

I have a table for users (username, password .. etc) and another one for products (product name, price, quantity, username .. etc)
I want the username from the first table to be inserted (with the rest of the product info) into the product table when the user put it for sale.
How can I do this?
I'm using visual web developer 2008 express
INSERT INTO product_information (username,product_name, ...., Date)
SELECT username, 'Book',....., Date
FROM users;
something along that line
Your question doesn't exactly make sense. It sounds like you want to insert into one table (Products) and one of the fields is also in another table (User.username). Where does the rest of the data for Products come from? Can't you just insert the username directly?
Insert into products (name, price, username) values (?, ?, ?)
Use whatever mechanism you want to pass in the data to that SQL statement.
If, however, you actually have a third table, say, 'Prod_for_sale' and you want to insert a bunch of data into that table, from both Users and Products, you can do something like this (as suggested by Rick J, but this example has two tables):
insert into products for sale (username, user_country, product_name, product_price)
select u.username, u.country, p.name, p.price
from products p, users u
where p.id = ?
and u.username = ?
Then you can copy whatever data you want from the tables in question into the destination table.
A word of advice though: you should be using a user ID instead of a username to identify the user's data in the database. This will allow you to change your username policy later or allow for usernames to be updated, etc. Generally try to make all your identifiers numbers and you'll save yourself hassle in the future.
If you have Linq (.NET 3.5 / C# 3.0):
var users = from u in userTable
from p in productTable
where p.username = u.username
select new productDetails()
{
username = u.username,
price = p.price
}
then newtable.insertAllOnSubmit(users);
if in straight sql
insert into newtable
select table1.value1, table2.value1
from table1, table2
where table1.username = table2.username
Edit:
to clarify... value1 and value2 are columns from the corresponding cases. You'll have one for each parameter you want to insert. And your newtable becomes table1

Resources