Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am developing a web application using entity framework, asp 4.5 with sql 2012 as back end. Actually it is an online examination website. Now the issue is, I have to insert 36 rows to the table (36 Questions for an exam, with answers) once the user finishes the exam. So i have taken all the 36 answers and kept it in a data table and i passed the data table to Stored procedure( and the ID is auto generated) . and then i checked and it is working fine. But the problem is when concurrent transactions are happening( ie, if 10 or more people "Submit" the answers at the same time), only 1 or 2 user's data is inserting and also sometimes the insert is not happening properly. So how can i do multiple concurrent insert to stored procedure ..?

My Submit button code

C#
    protected void btnSubmit_Click(object sender, EventArgs 
    {
        try
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[4] 
            { new DataColumn("StudentId", typeof(int)),
                new DataColumn("QuestionId", typeof(int)),
                new DataColumn("Options",typeof(string)),
                new DataColumn("Point",typeof(int))

            });
            dt.Rows.Add(userid, a[0], RadioButtonList1.SelectedItem.Text, 0);
            dt.Rows.Add(userid, a[1], RadioButtonList2.SelectedItem.Text, 0);
            dt.Rows.Add(userid, a[2], RadioButtonList3.SelectedItem.Text, 0);
            .
....... .... dt.Rows.Add(userid, a[35], RadioButtonList36.SelectedItem.Text, 0);

            ojclsExam.TableInsert(dt);
}


array "a" contains the question Nos.. I am Not considering "Point" now so just passing "0" directly and i have created a user defined table type "dbo.StudAns" in sql server for passing this data table to stored procedure. Business layer code is

C#
public void TableInsert(DataTable dt)
{

    var items = new SqlParameter("Paramtable", SqlDbType.Structured);
    items.Value = dt;
    items.TypeName = "dbo.StudAns";
    objExamDBEntities.ExecuteStoreProcedure("spInsertAnswerTable", items);

}

and stored procedure is
SQL
USE [ExamDB] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[spInsertAnswerTable] @tblAnsers StudAns READONLY AS BEGIN SET NOCOUNT ON;

  INSERT INTO tblStudentAnswer(StudentId, QuestionId, Options,Point)
  SELECT StudentId, QuestionId, Options,Point FROM @tblAnsers
END

and my table is

SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblStudentAnswer](
	[AnswerId] [bigint] IDENTITY(1,1) NOT NULL,
	[StudentId] [bigint] NULL,
	[QuestionId] [int] NULL,
	[Options] [varchar](50) NULL,
	[Point] [int] NULL,
 CONSTRAINT [PK_tblStudentAnswer] PRIMARY KEY CLUSTERED 
(
	[AnswerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


It is working properly but when it comes multiple simultaneous entries, it is not working. Thanks in advance..
Posted
Updated 29-Dec-14 18:04pm
v4
Comments
Zoltán Zörgő 29-Dec-14 14:23pm    
0) Concurrency on sql server side should not be any problem - so your design seems to have some flows
1) How do you pass exactly the "data table"?
2) Do you use session state?
3) What does it mean "not happening properly"?
4) How does the table on sql side looks like (DDL)?
Member 11341772 29-Dec-14 15:55pm    
Thanks for your reply.
I have updated the question.. please check
Zoltán Zörgő 29-Dec-14 17:11pm    
Interesting. Should work.
Just an idea: return @@ROWCOUNT from the stored procedure, and print it as result, just to see, if you really have all 36 rows in the table typed parameter. If not, you can try passing an XML instead.
Dave Kreskowiak 29-Dec-14 15:31pm    
Why are you not storing the answers they give as they submit each question and move on to the next one?
Member 11341772 29-Dec-14 15:55pm    
Thanks for your reply.
i have 36 radiobuttonlist in a single page.. I have updated the question, please check

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