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

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

Related

How do I find influenced program,when i add a column.Oracle & C# [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 2 years ago.
Improve this question
I have a question.
I need the table A add a column, its name is called "A1" then happed a program of company is not working,because it programs use Merge into method, let this program not run, until I add "A1" and Related code write into this Program.
This Program is C# Program.
Now, I will add a new column in the table B.
But my company has many program access in table B, and I don't know how many program access the table B...
So, how to do that, let me can advance find influenced program?
or
How to know which C# programs are using oracle operating table b ,
let me can get these programe name & path
Thinks!
Unless your code is inside the database itself (as stored procedures) there is nothing in oracle that can tell you. And since you said your code is in C#, that appears to be the case. This is where you have to rely an a search with whatever facilities are available in your source code control system.
I am not sure what do you mean by program here. But If you want to know which all PL/SQL objects are using TABLE_A then you can use the USER_DEPENDENCIES view as follows:
SELECT REFERENCED_OWNER,
NAME,
TYPE
FROM USER_DEPENDENCIES
WHERE REFERENCED_OWNER = 'SCHEMA_NAME_OF_TABLE_A'
AND REFERENCED_NAME = 'TABLE_A';

Is there a way to reserve a variable used by the operation in c#? [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 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.

How to Update database from UI which has many textboxes and CheckBox in asp.net and SQL Server? [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 am new to ASP.net. I have a webform where I have 40 text box 18 drop downs and 29 checkbox. I need to load the all data from these controls to SQL server database table using stored procedure. I dont want to pass all the parameters one by one and update the table.
Is there any other short way to do it? Please help
I am not sure I follow your question here?
The simplest way, would be to do a model object. Create a method that accepts the model object that includes all the data from your form. This methods feeds the stored procedure call.
This is the common way to do it. And the fastest. You need to assign the data to data-fields. There is no way around it really. But of course it can be done in many different ways.
So you want a more specific answer, you will have to elaborate what you mean by:
"I dont want to pass all the parameters one by one and update the
table."
I can't come up with a scenario where it would be desireable to pass each parameter one by one into an update statement...
but you will have to mention each field in your SQL stored procedure, that you want to update.
if that is your only question:
UPDATE table-name
SET column-name = value, column-name = value, ...
WHERE condition
If you have used a model in your view and used Razor syntax to render the relevant controls than if you have a form defined, posting back to the controller that accepts the same model as it's parameter will handle the binding of all the properties back to the model.
You then either parse the model or use the properties and send them down to entity framework, or any other ORM that you might be using, and have it handle the update operation.
[HttpPost]
public ActionResult YourPostMethod(YourModelUsedInView model)
{
// process your model here and send it down to the DB.
}

I can't drop table in SQL even after tried all the methods I found on the Internet [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I can't drop an SQL table even after I tried everything I found on the web. I actually want to delete and recreate an SQL table after clicking the "SAVE" button in my C# app (a simple SQL database editor). It works fine for tables with just data (no special rules like primary keys, forbidden nulls, etc.) but for more complicated ones I call a delete function (DROP TABLE table1), then a CREATE TABLE table1 ..., it throws an error "There is already an object named 'table1' in the database". Can anyone help? Thanks in advance!
i think your issue the tables didn't drop. since there is a relationship between tables.
So if the table you doping is a FK to other table , it will not go through.
try DeleteRule = Cascade.

Whats Better: Check if table exists or always fire create statement [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 9 years ago.
Improve this question
Is it OK to always fire the create statements rather than checking them like
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'my_table_name'
in SQL SERVER CE and C#.
Because irrespective of the approach, there is always a single query that will be fired. Isnt it ???
I am using trasaction for creating all tables. So, I am sure if there is one table present, then definitely all of them are created.
So if i use the same transaction, then the transaction will fail right at the very first create table statement: But is this good performance wise???
Check if table exists or always fire create statement
If the table already exists in database, you will get an exception.
You can check for table existence, delete and then create it, something like:
if OBJECT_ID('dbo.my_table_name', 'U') IS NOT NULL
DROP TABLE dbo.my_table_name;
--and then
CREATE TABLE ....
If you mean try to create the table and look for an exception, then no. Exceptions are expensive, and if they are not handled properly can terminate your program unexpectedly.
The best practice would be to check for the table's existence.

Categories