Creating User specified table in sql server database - c#

I have a table in sql server datbase and gridview retrieves the result of that table. That table at first all the values are 0 but when a user log-in he can edit gridview so table will be updated . But if there is two different users , how they can make and see their own updated data in gridview ? . Here is the table looks like . I am just asking for logic , i just added one more column called Userid .
Any help appreciate ?
Thanks

If all the users has to edit the same data, i mean all users can see the data in the table, not just where userid = tableuserid, then u can use the application("") aspnet object to save when a user had updated the datatable and a webservice to the tell the others user that other user updated the table.
U can use something like versioning in the application("") object. when the users login and loads the data, then he save the current number in the applicationb object. Then you can create a javascript call to a web service to check if the number stills the same. if not reloads the page with the new
If the data is related to each user, you can take any of the others two answers... good luck
reply if you need help

I think your'e on the right track. Ultimately this data needs to have a UserId associated with it. When querying the data to populate your GridView you should filter where the UserId == currentUserId
EDIT: the specifics of the necessary database design for this will vary depending on the type of data, and how it might need to be broken up into more than one relational table.

Different user(s) will see their specific data in order to provide privacy (encapsulation). So, when a user logged in, on your application load you should query the table like below and fill the data to grid
select ID,Act1,Act2,[User Id]
from user_table
where [User Id] = logged_in_userid;
that way each user will update only their own data which eventually will get saved in DB. You may want to wrap the query in a stored procedure like below and in your page load call the procedure with parameter.
create procedure usp_get_userData #userid int
as
begin
select ID,Act1,Act2,[User Id]
from user_table
where [User Id] = #userid;
end

Related

How do I show balance log in a gridview

I have a system in which allows the user to apply for taking leaves. The system also credits leaves to each user's account, twice a year.
Now, the administrator has full authority over this whole system and, can cancel the and grant leaves.
To analyze whether a user has not exceeded all privileges extended to him, it is required that the admin should be shown a record of the credit and debit of leaves taking place in each user account, like a log.
Something like this:
Now, I have made a table as below, to house this data:
My query now is that how do i achieve this? I mean, what should be my sql query and how should i display it?
Should I create a Sql Server Agent job and display the data in a gridview? If yes, then how should I specify the steps for that.
My database has an employee table which stores the updated balance for each type of leave for every user. And the leave table houses the details of the requests made by the users. Basically, it stores all the details filled out in the leave application form.
I am confused as to how do I fetch the operations and changes occurring on these tables, for that is what is required to be displayed to the admin.
As per my understanding of your query, I have been shared my suggestion below :
1.My point of view ,you can get your expected output by using stored procedure in Database.
2.Because Hopefully You stored the values of employee information, leave type ,leave details are into separate table with the help of primary & foreign key constraints.
3.In the stored procedure, you can perform the following steps:
(1)Pass Input parameter : EmployeeId, FromDate, ToDate, HandledById (AdminId)
(2)Join all corresponding table with the help of Primary & foreign key Id.
(3)Select the appropriate column, Use aggregate function to find the leave.
(4)Use sub query in case of necessary as per your logic & table structure.
4.Then You can bind & display the data in gridview easily.
Do let me know if you need anything else.!
there is not much you need to do in sql for that. You can fetch the data that is to be displayed, but how you want it to display can be control on server side/Client side coding.
So its better you fetch data and user the ItemDataBoud event to control the representational part of the application.

How to pull a SQL Table entry based on highest IDENTITY entry and update two columns

I'm not well versed in SQL operations, and would like some help with a task I need to complete in code. I have written a cloud based app that accesses a SQL table containing test results - device ID's, serial numbers, test results etc.
There is a use-case where someone in the field would activate a menu where an update to this table occurs. When the device test result table is updated, I want to store the OLD information in a device test history table. This way, we can go back and see what was changed over time.
So I need to pull all the columns from the TestedDevice table, insert them into TestedDeviceHistory table, and include some additional information; the current date and the operator's id. (these are two new columns found only in TestedDeviceHistory)
At first, I'm using a SELECT INTO command, as follows:
SELECT *
INTO dbo.TestedDevicesHistory
FROM dbo.TestedDevices
WHERE CertificateID = #cert
Then I'm attempting this (obviously broken) SQL command:
UPDATE dbo.TestedDeviceHistory
SET Caller = #caller,
RecordDate = #date
WHERE DeviceHistoryID = MAX(DeviceHistoryID)
Notes:
DeviceHistoryID is an IDENTITY integer column, so it's unique for each entry made in the history table.
CertificateID is unique in the TestedDevices table. It is expected NOT to be unique in the history table.
The code is written in C# 4.5
Maybe this is a case for a stored procedure, which I have never attempted to create or use. Or, perhaps the use of a cursor? Don't know! This is why I'm humbly asking for the more experienced with SQL to help :)
Not clear on if you only want to assign the Caller and RecordDate to the most recent record, or if it could be assigned to all the history records.
For all records, I believe you can do something like
SELECT *, #caller AS Caller, #date AS RecordDate INTO dbo.TestedDevicesHistory
FROM dbo.TestedDevices WHERE CertificateID=#cert

Get the exact SessionID generated in another Table

I am new to sql. In my Chat application, I am storing the messages with the help of following Database Tables.
SessionTable --> SessionID (PK) Username1 Username2 Time
MessagesTable --> SessionID (FK) Username Message StartTime
The First Database Table SessionTable is used to generate a SessionID (IDENTITY(1,1)) by the help of which the users(limited to two) can have conversation. Whenever a user sends message to the other user, the message data will be stored in MessagesTable of column Message with the respective user's name in the column Username and the Time when the user sends the message. But the problem here I am facing is that how to get the exact sessionID to be stored along with the message in MessagesTable which was generated in SessionTable ?
My another question is that, As I am storing the messages in a single database Table will it arise performance issues in future? (The project which I am creating will be used among atmost 30 members) and also please suggest whether the way I have designed the Database tables is better way of approach or not.
After you insert the row into SessionTable you can use the Scope_Identity() function to get the SessionID assigned to the new row.
As for performance, there are too many factors to provide a useful answer.
In short: It seems like you have one-to-many relationship between Session table and Message table. Thus, you are attempting to join the tables throw common SessionID column.
Depending on the messaging application load, you need to think about the indexing strategy as well. In another words, tables with high intensity of read & write are usually getting performance hit.
Solutiob #1: You'll definitely need to use SCOPE_IDENTITY().
DECLARE #SessionID INT;
INSERT INTO dbo.SessionTable (Username1, Username2, Time)
SELECT 'User1', 'User2', GETDATE();
SELECT #SessionID = SCOPE_IDENTITY();
After this creation of session, you would need to use #SessionID to add message detail in MessagesTable. Like the following code sample:
INSERT INTO dbo.MessagesTable (SessionID, Username, Message, StartTime)
SELECT #SessionID, 'User1', 'The sample text of the message' GETDATE();
GO
In conclusion: the overall principle is described in the code sample which is posted above. However, i did not check al the syntax, thus you may expect some syntax corrections in this code :)

Making a page where each user has his own information,gridview etc

After the user logged in , I need a page where each registered user has his own gridview and controls binded to his gridview.
The page will contain a sqldatasource binded to the gridview .
I thought about making a new table in the database for each user and in the form load to get the username after the user logged in, get the table name and replace the sqldatasource bind to his table name and the other controls fields for table name to his one.
Or is there any other way of doing this?
You definitely do not want to make a separate table for every user. How on Earth would you plan to scale that to multiple users?
What data does the user need to see on their page? Understand that a gridview doesn't have to map directly to a database table. It can map to any set of data. So you can store the data in your database in a way that makes sense to persist it (relational entities), then query and display it in a way that makes sense to display it.
For example (and it's a contrived example, since we don't know what data you have), if you have users who need to see a list of products that they've ordered, then you wouldn't create a table of products for each user. You'd probably have a table for each of the entities (User, Product, Order, etc.):
Users
----------
ID
Name
etc.
Products
----------
ID
Name
etc.
Orders
----------
ID
UserID
DateOrdered
etc.
And since each order would have a list of products, and each product can be on many orders, that's a many-to-many relationship. So you might create a linking table for that relationship:
OrderedProducts
----------
OrderID
ProductID
Then, for displaying in the UI, you would query the data to get only the products ordered by that user:
SELECT DISTINCT
Products.ID,
Products.Name,
Orders.OrderedDate
FROM
Users
INNER JOIN Orders ON Users.ID = Orders.UserID
INNER JOIN OrderedProducts ON Orders.ID = OrderedProducts.OrderID
INNER JOIN Products ON OrderedProducts.ProductID = Products.ID
WHERE
Users.ID = #userID
This should give you a list of distinct products ordered by that user and when they were ordered. (Note that this code is free-hand, I don't have a database handy to test it.)
So each user would see their own specific information.
You want to make sure that your data is modeled in a usable relational fashion. Define your entities (usually real-world things you're representing in the data) and define tables to represent those entities. Relate them together in natural ways. Relational databases are great at handling complex queries against well-defined data. Don't try to design your database around the nature of the display from the perspective of the user, design it around the nature of the information being stored.
You certainly DO NOT need a separate table for each of your users!
Why don't you just add an additional column to one of your tables to store the username. This way, each record is marked to belong to specific user. Reading the data for logged user is just a matter of simple Where clause on the table.
No thats not right,
You can't create a table for every user. Think if you have 10,000 users than you mean your database contain 10,000 users.
You have to create only one table in which all user's information is saved.
You have to save an identity like user name or email to your database and
When user login to your app by his username or email the information will be taken from table on the basis of username or email and set to gridview.
May it helps you...
this is not Logical to create a table for each user,save all user information in a table is better and faster
for this your can make a table for user information with a user id, and another table for user data entery.and make a relation ship between them

How do I query table data only for the current user's ID (VWD asp.net 3.5)?

I'm using the Details View control of VWD asp.net 2008
I have a users database table. One of the columns contain the "uniqueidentifier" user's profile ID, created by VWD (from the automatic profiles table).
From my table, I would like to select only the data pertaining to the logged in user.
How do I get the current unique user profileID in general and then how do I use that for my query?
In the configure data source option of the control, I select the check box for "Return Only Unique IDs" and the WHERE option. In this new window I select the Column to the profile userID ... "=" and then assumed I should select Source: "Profile".
Now what should the parameter properties or value be to select only the current logged userID?
OR how can I do this just in C#? There doesn't seem to be any code behind after instering a DB with the VWD controls?
Any help is appreciated.
Thank You!
Try this: http://bytes.com/topic/asp-net/answers/815202-passing-values-sql-datasource
See the last answer from user Munna.

Categories