Click here to Skip to main content
15,923,015 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Good day,

Scenario: Using a web form the user wants to add stock items to the stock table.

E.g.

User wants to add Value in textbox1 to the database 10 times. as apposed adding the value individually ten times.

e.g.
(GoodsInID,ProductID)
(1,555)
(2,555)
(3,555)

C#
protected void btnSubmit_click(object sender, EventArgs e)
    {
        try
        {
            using ( dbmodel dbContext = new dbmodel())
            {
                GoodsInDetails gdin = new GoodsInDetails();
                gdin.GoodsInID = int.Parse(txtorderID.Text);
                gdin.ProductID = int.Parse(txtbox1.Text);
                //**Add ProductID x amount of times dependednt on value in
                //txtQuanitity.text;***

                dbContext.Add(gdin);
                dbContext.SaveChanges();
                lblMessage.Text = "Succesfully added!";
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }
}


Any advice would be great!

Cheers
Jc

Move erroneous 'solution':
hi i want to mutiple record view in datagridview how insert in sqltable
Posted
Updated 13-Jul-11 0:44am
v2
Comments
BobJanova 1-Jun-11 6:51am    
repost

http://www.codeproject.com/Answers/204415/Insert-Record-Multiple-times.aspx
Nagy Vilmos 13-Jul-11 6:45am    
Don't add details be 'answering' the question. edit the original, as I did, by clicking 'Improve question'.

Well, the first thing you are going to need to do is open up a TransactionScope on your database connection. Basically, you'd open the transaction when you open the connection to the database and then you'd keep the transaction open while you insert the data. Finally, you'll either commit or rollback the data depending on the status of the insert.

The bit in the middle is easy - you simply need to loop n times, and perform the insert while iterating over the loop.
 
Share this answer
 
Comments
Nagy Vilmos 13-Jul-11 6:44am    
Gets my thumb up.
Learn transaction basics

see transaction topic in this link

Overview of Error Handling in SQL Server 2005[^]
 
Share this answer
 
Comments
Member 7949758 1-Jun-11 6:57am    
I dont think i have explained my self well, I havent got an error.

I would like to write a value in a text box to a table in my database multiple times. The maount can be specified in the webform.

e.g.

txtbox1= insert product code 555
txtbox2 = the amount of times I want to insert product code 555 to the table.
btnCLick= Writes the product code a specific amount of times dependent on txtbox2

The table has the following fields:
StockID,ProductID

stockID - Auto increment

So if I write ProductID it will assign a stock ID.

I want to know how to write 10(this can be any value) productIDs to the stock table in one transaction.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900