Is there a way to reserve a variable used by the operation in c#? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this issue in my c# MVC App where i have a receipt and this receipt has a receipt number and i select the last receipt number inserted in the database and add 1 to it, the problem appears when 2 users submitting the receipt at the same time. What happens is that they both get the same receipt number and they both insert into the database, unfortunately one is inserted and the other gives exception and rollback the whole operation. So my question is there a way to prevent this from happening, in other words reserve a receipt number taken by user, so it won’t be used by the other user.
Thanks in advance.

If using sql server you should probably use an IDENTITY column. Another option would be to use a GUID as the key.
So you need to reset the receipt number for each new year? You could make a separate call to get the receipt number first without doing inserts into other tables. This would reserve a number and return immediately. You could use an identity column for this, either a separate table per year or reset the identity value and truncate at the start of each new year. Or you could have a table for Ids with Year and ReceiptNumber as the PK and insert a new row with incrementing receipt number by one.

Related

what is the best way to store Numbers of a Bingo Card in database table [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I wanna create a web-based bingo game using MVC and EF, I want to store 24 numbers for each card in the database table as a record,I'm not sure how to create my Bingo cards table,one way that comes to my mind is design a table with 24 integer field for each cell's number this way when I fetch the record in business logic I can't traverse the numbers because they are in the separate fields(actually I can but I think this way is not the right way to do)the other way is I concatenate all numbers together and separate it with comma and store it as a string so when I fetch the record I can split the string of comma and have all numbers in an array or something
so what is the best solution for this situation ?
how can I design my table for storing numbers of a bingo card ?
Good question!
You're right, storing it in 24 different integer fields is definitely a bad way to go. Concatenating them with commas might seem like a reasonable compromise, but it's inflexible, as you're treating them as text, not integers, so later on if you wanted to run queries such as SELECT MAX(CardNumber) FROM BingoCardNumbers WHERE CardID = 1 you couldn't do anything like that.
My suggestion would be to have two different tables, BingoCards and BingoCardNumbers. The BingoCards table would have a CardID, as well as other information relevant to the card, such as who the card belongs to, so maybe you'd want a UserID column in there too. No card numbers are stored in this table.
Then, in the BingoCardNumbers table, that would have three columns, CardID, which is the primary key from the first table, NumberIndex (1-24, which is the index of the number in the card), and finally CardNumber, which is the actual number itself. Then, if you want to get all the numbers for a particular card, you can simply do:
SELECT CardNumber FROM BingoCardNumbers WHERE CardID = 1 ORDER BY NumberIndex

Generate sequential number based on selected sequence [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a website dropdown that is populated from a SQL query. The values in the dropdown are from a table column and are like:
ABC-0123
ABC-0124
ABC-0125
ABC-0126.01
ABC-0126.02
ABC-0127
DEF-0123
DEF-0124
DEF-0125.1.01
DEF-0125.1.02
DEF-0125.2
I have a button to generate a new number based on what is selected in the dropdown. For example, if ABC-0125 is selected, ABC-0128 would need to be created since it's the next number in sequence. If ABC-0126.01 is selected, ABC-0126.03 would need to be created.
I'm looking for ideas on how to perform this. I considered just using the dropdown or querying the database directly.
I've split the selected value as a start:
String strDocFamily = drpDocFamily.SelectedValue;
string[] strDocTiers = strDocFamily.Split('.');
This may be open ended, but I'm looking for some suggestions on how to proceed. Thank you.
A solution might be to split the storage of the data into two or more columns, one for the alphabetic part ('ABC') another for the 'family' (0128 - the first set of digits) and others for the other tiers. You could then directly look up the maximum value of the 'family' column based on alphabetic. For example:
SELECT MAX(family-number)
FROM table-name
WHERE alpha-part='ABC';
Here is a UniqueID generator that can be easily adapted to generate a similar sequence to the one you need.
https://stackoverflow.com/a/39312025/2495728

C# / SQL Server : MVVM insert data into multiple tables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I work on a small project just to get to understand the mvvm model in C# better.
I work with a Microsoft SQL Server database with three tables: customer, location and address.
Every customer can have one or more locations, and every location has a specific address.
My current thought how to accomplish this:
First insert the customer.
To insert the location, get the highest customer_id and insert the location with the max(customer_id)
Then, to insert the address, get the max(location_id) and insert the address, with the location id
Is there a better way to do this?
I haven't found any tutorial with an example of inserting data into more than one table, especially not using SQL Server.
And my next problem is: what should I bind to my TextBoxes, so that I can insert the content of it?
I thought about having a save button. This button would then execute a method, where I insert the data from the bound TextBoxes. Should I do this with commands, instead of writing a method?
Thanks already!
Sql server has a OUTPUT clause which you can use
something like
INSERT INTO MyTable VALUES({CustomerName})
OUTPUT INSERTED.ID
then you can store the inserted customer's actual ID and do the rest inserts in separate queries.
As for your second question yes you should do it with Command binding to a method in your View model
The most advisable for your situation is to use a transaction.
Example.- https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction(v=vs.110).aspx

how to add instantly made rows key to another table.? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table named product where there is a field named id its the primary key here. I need to put this key to my another table pro_size_tbl as below
pro_size_tbl
==================
id
productid
sizeid
the matter is when the user adds a product the sizes of the products are saved in session and I am trying to save them in pro_size_tbl by the product key what I just made.
The thing I am doing right now is I am adding the product in the product table and in the next line I am retrieving the max id of the product table and using it as the key for the pro_size_tbl.
My question is is there any better way to do this thing?
This is simple.You can use LAST_INSERT_ID().See this sample:
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();
There is a function LAST_INSERT_ID() which can give you the desired value.
But you need to use right below the insert statement.
owh. from what i understood, u need to put auto generated mysql id to another table?
if so, here is the real deal
$sql_tbl1 = "insert here for first table";
$result_tbl1 = mysql_query($sql_tbl1) or die(mysql_error());
$id = mysql_insert_id();
$sql_tbl2 = "insert here value ($id, 'bla', 'bla')";
mysql_query(sql_tbl2);
LAST_INSERT_ID() function is ok but it is doing the same work as i am doing taking the maximum value.i guess more or less both way are the same.

Mysql and c# updating [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I currently have a mysql data table that fills up when people complete a form. I then have my visual studio c# bot pull this data and do what I want. The table numbers the table using integers that increase by 1 each time. My bot looks in the table for id 1 and adds it.
The problem I have is that once my bot completes what someone wants and deletes the data, it does not recognize to go onto the next one since that id is not 1 and something else, like 2. I tried to have my c# bot add 1 to a variable each time and then have that variable represent the id so that every time the bot completed something it would go onto the next id. The problem with this was that every time the bot completed with one person the variable was reset back to 1. I am stuck now and can't figure out what to do. Any help would be great.
This is currently the string I use "select name from names where id = 1". If I can somehow instead of having the id = 1 and have it look for the next lowest id in my table then that would work.
Edit: Actually I think I figured out how to have the last added id be used. I will post it if I find it.
In MySQL, if you want the lowest ID then you could do something like this:
SELECT
Name
FROM
Names
ORDER BY ID
Limit 1
Is that what you are looking for?

Categories