Can't insert records into database
My published application does not insert (write) records into database
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hanook159 reputationPosted Nov 20, 2013, 11:17 PM EST
Jignesh TrivediRank #16 · 50.3K reputationPosted Nov 21, 2013, 12:20 AM EST
do one thing register Application_Error() event in Global.asax and write notepad file or window event log on error.
it help to know error.
hope this will help you.
Jon Sambo33 reputationPosted Nov 21, 2013, 12:27 AM EST
Jon Sambo33 reputationPosted Nov 21, 2013, 12:27 AM EST
Jignesh TrivediRank #16 · 50.3K reputationPosted Nov 21, 2013, 12:31 AM EST
Jon Sambo33 reputationPosted Nov 21, 2013, 12:52 AM EST
CONNECTION STRING: "data source=10.1.26.237;Initial Catalog=securitySurvey;Integrated Security=True;Trusted_Connection=True;Persist Security Info=True;User ID = csg; Password = csg"; // providerName="System.Data.SqlClient" />
XXXXXXXXXX Code XXXXXXXXXX
try
{
foreach (DataRow row in dt.Rows)
{
//
cmd.Parameters.Add("@questionId", SqlDbType.VarChar).Value = Convert.ToByte(dt.Rows[j]["questionsId"].ToString());
cmd.Parameters.Add("@categoryID", SqlDbType.VarChar).Value = Convert.ToByte(dt.Rows[j]["categoryId"].ToString());
cmd.Parameters.Add("@categoryDescr", SqlDbType.VarChar).Value = dt.Rows[j]["categoryDescr"].ToString();
cmd.Parameters.Add("@questionDescr", SqlDbType.VarChar).Value = dt.Rows[j]["questionDescr"].ToString();
//
cmd.Parameters.Add("@inAdequate", SqlDbType.VarChar).Value = answerArray[j, 0];
cmd.Parameters.Add("@marginal", SqlDbType.VarChar).Value = answerArray[j, 1];
cmd.Parameters.Add("@satisfactory", SqlDbType.VarChar).Value = answerArray[j, 2];
cmd.Parameters.Add("@veryGood", SqlDbType.VarChar).Value = answerArray[j, 3];
cmd.Parameters.Add("@excellent", SqlDbType.VarChar).Value = answerArray[j, 4];
cmd.Parameters.Add("@currentDate", SqlDbType.VarChar).Value = DateTime.Now;
//
cmd.ExecuteNonQuery();
//
j = j + 1;
}
cmdComm.ExecuteNonQuery();
}
// Catch the exception as e and pass it to the ShowErrors routine.
catch (SqlException e)
{
}
XXXXXXXXXXXXXXXXXXXXXXXXXXX STORED PROCEDURE BELOW XXXXXXXXXXXXXXXXXXXXXXX
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[pSurvey]
(
@questionID int,
@categoryID int,
@categoryDescr varchar (50),
@questionDescr varchar (150),
@inAdequate int,
@marginal int,
@satisfactory int,
@veryGood int,
@excellent int,
@currentDate dateTime
)
AS
BEGIN
INSERT INTO Answers
(
questionId,
categoryId,
categoryDescr,
QuestionDescr,
inAdequate,
marginal,
satisfactory,
veryGood,
excellent,
currentDate
)
VALUES
(
@questionID,
@categoryID,
@categoryDescr,
@questionDescr,
@inAdequate,
@marginal,
@satisfactory,
@veryGood,
@excellent,
@currentDate
)
END
Jignesh TrivediRank #16 · 50.3K reputationPosted Nov 21, 2013, 1:10 AM EST
you code and SP looks ok
Here you write Try catch block to catch the error
check, Is any Error generated in SqlException catch block?
Jon Sambo33 reputationPosted Nov 21, 2013, 1:16 AM EST
Jignesh TrivediRank #16 · 50.3K reputationPosted Nov 21, 2013, 1:21 AM EST
Hanook159 reputationPosted Nov 21, 2013, 1:40 AM EST
ExecuteNonQuery() with no other statements in try... catch
try
{
foreach ()
{
//stored procedure code
cmd.Parameters.Add("@currentDate", SqlDbType.VarChar).Value = DateTime.Now;
//
cmd.ExecuteNonQuery();
//
j = j + 1;
}
cmdComm.ExecuteNonQuery();
}
check whether that is causing any issue?
Jon Sambo33 reputationPosted Nov 21, 2013, 2:03 AM EST
Older IIS on target server.
Taking a break
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Dec 12, 2013, 9:30 PM EST
catch{throw;}