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
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
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
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
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..