Solved

Problem Inserting Multi rows if checkboxes are checked

Posted on 2009-05-12
5
235 Views
Last Modified: 2013-12-17
I nedd need to insert 15 0r less rows depending on how many checkboxes are checked.

The code below is doing just that except that it thrown exception with error below. IThe issue is why is it inserting a row twice or having primary key violation?

Violation of PRIMARY KEY constraint 'PK_TRAILER_PARK_PAGES'. Cannot insert duplicate key in object 'dbo.TRAILER_PARK_PAGES'.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_TRAILER_PARK_PAGES'. Cannot insert duplicate key in object 'dbo.TRAILER_PARK_PAGES'.
The statement has been terminated.

Source Error:


Line 93:                 // Log eventual errors and rethrow them
Line 94:                 //Utility.LogError(ex);
Line 95:                 throw ex;
Line 96:             }
Line 97:             finally
 

Source File: c:\Inetpub\wwwroot\ParkRanger\App_Code\Setup\GenericDataAccess.cs    Line: 95

Stack Trace:


[SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_TRAILER_PARK_PAGES'. Cannot insert duplicate key in object 'dbo.TRAILER_PARK_PAGES'.
The statement has been terminated.]
   AMSC.ParkRanger.Setup.GenericDataAccess.ExecuteNonQuery(DbCommand command) in c:\Inetpub\wwwroot\ParkRanger\App_Code\Setup\GenericDataAccess.cs:95
   AMSC.ParkRanger.UnitAdmim.TrailerParkIntregation.EnableUnit(String unitId, Int32 chartId, String chartName, Int32 displayOrder, Int32 inUse, Int32 grouping) in c:\Inetpub\wwwroot\ParkRanger\App_Code\UnitAdmin\TrailerParkIntregation.cs:108
   Units_EnableTrailerPark.btnEnable_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\ParkRanger\Units\EnableTrailerPark.aspx.cs:118
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

 

Error thrown

Violation of PRIMARY KEY constraint 'PK_TRAILER_PARK_PAGES'. Cannot insert duplicate key in object 'dbo.TRAILER_PARK_PAGES'.

The statement has been terminated. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 

Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_TRAILER_PARK_PAGES'. Cannot insert duplicate key in object 'dbo.TRAILER_PARK_PAGES'.

The statement has been terminated.
 

Source Error: 
 
 

Line 93:                 // Log eventual errors and rethrow them

Line 94:                 //Utility.LogError(ex);

Line 95:                 throw ex;

Line 96:             }

Line 97:             finally

 
 

Source File: c:\Inetpub\wwwroot\ParkRanger\App_Code\Setup\GenericDataAccess.cs    Line: 95 
 

Stack Trace: 
 
 

[SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_TRAILER_PARK_PAGES'. Cannot insert duplicate key in object 'dbo.TRAILER_PARK_PAGES'.

The statement has been terminated.]

   AMSC.ParkRanger.Setup.GenericDataAccess.ExecuteNonQuery(DbCommand command) in c:\Inetpub\wwwroot\ParkRanger\App_Code\Setup\GenericDataAccess.cs:95

   AMSC.ParkRanger.UnitAdmim.TrailerParkIntregation.EnableUnit(String unitId, Int32 chartId, String chartName, Int32 displayOrder, Int32 inUse, Int32 grouping) in c:\Inetpub\wwwroot\ParkRanger\App_Code\UnitAdmin\TrailerParkIntregation.cs:108

   Units_EnableTrailerPark.btnEnable_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\ParkRanger\Units\EnableTrailerPark.aspx.cs:118

   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111

   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110

   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10

   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13

   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36

   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
 

 

// business component that insert rows

        public bool EnableUnit(string unitId, int chartId, string chartName, int displayOrder, int inUse, int grouping)

        {

            // get a configured DbCommand object

            DbCommand customerCommand = GenericDataAccess.CreateCommand();

            // set the stored procedure name

            customerCommand.CommandText = "adm_UnitPageDefinition";

            // create a new parameter

            DbParameter parameter = customerCommand.CreateParameter();

            parameter.ParameterName = "@Trailer_Id";

            parameter.Value = unitId;

            parameter.DbType = DbType.Int32;

            customerCommand.Parameters.Add(parameter);

            // create a new parameter

            parameter = customerCommand.CreateParameter();

            parameter.ParameterName = "@Chart_Id";

            parameter.Value = chartId;

            parameter.DbType = DbType.Int32;

            customerCommand.Parameters.Add(parameter);

            // create a new parameter

            parameter = customerCommand.CreateParameter();

            parameter.ParameterName = "@Chart_Name";

            parameter.Value = chartName;

            parameter.DbType = DbType.String;

            parameter.Size = 50;

            customerCommand.Parameters.Add(parameter);

            // create a new parameter

            parameter = customerCommand.CreateParameter();

            parameter.ParameterName = "@Display_Order";

            parameter.Value = displayOrder;

            parameter.DbType = DbType.Int32;

            customerCommand.Parameters.Add(parameter);

            // create a new parameter

            parameter = customerCommand.CreateParameter();

            parameter.ParameterName = "@In_Use";

            parameter.Value = inUse;

            parameter.DbType = DbType.Int32;

            customerCommand.Parameters.Add(parameter);

            // create a new parameter

            parameter = customerCommand.CreateParameter();

            parameter.ParameterName = "@Grouping";

            parameter.Value = grouping;

            parameter.DbType = DbType.Int32;

            customerCommand.Parameters.Add(parameter);
 

            // result will represent the number of changed rows

            int result = -1;
 

            // execute the stored procedure

            result = GenericDataAccess.ExecuteNonQuery(customerCommand);
 

            // result will be 1 in case of success 

            return (result >= 1);

        }
 

// The code behind that implement it 

 protected void btnEnable_Click(object sender, EventArgs e)

    {

        string unitId = ddlUnit.SelectedItem.Text.ToString();

        int chartId;

        string checkName;

        int displayOrder;

        int inUse;

        int grouping;

        bool success = true;
 
 

        for (int i = 0; i <= 14; i++)

        {

            if (chkUserSettings.Checked)

            {

                chartId = 55;

                checkName = "User Setting";

                displayOrder = 55;

                inUse = 1;

                grouping = 10;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 
 

            if (chkAllParameters.Checked)

            {

                chartId = 56;

                checkName = "All Parameters";

                displayOrder = 17;

                inUse = 1;

                grouping = 5;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkParameterLookup.Checked)

            {

                chartId = 57;

                checkName = "Parameter Lookup";

                displayOrder = 18;

                inUse = 1;

                grouping = 5;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkParameterHistory.Checked)

            {

                chartId = 58;

                checkName = "Parameter History";

                displayOrder = 19;

                inUse = 1;

                grouping = 5;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkAllMeasurements.Checked)

            {

                chartId = 59;

                checkName = "All Measurements";

                displayOrder = 59;

                inUse = 1;

                grouping = 6;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkMeasurementsLookup.Checked)

            {

                chartId = 60;

                checkName = "Measurements Lookup";

                displayOrder = 60;

                inUse = 1;

                grouping = 6;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkMeasurementsHistory.Checked)

            {

                chartId = 61;

                checkName = "Measurements History";

                displayOrder = 61;

                inUse = 1;

                grouping = 6;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkMeasurementsTrends.Checked)

            {

                chartId = 62;

                checkName = "Measurements Trends";

                displayOrder = 62;

                inUse = 1;

                grouping = 2;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkHispeed.Checked)

            {

                chartId = 63;

                checkName = "Hispeed";

                displayOrder = 63;

                inUse = 1;

                grouping = 1;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkDigitalsSearch.Checked)

            {

                chartId = 64;

                checkName = "Digitals Search";

                displayOrder = 64;

                inUse = 1;

                grouping = 4;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkStatusDigitals.Checked)

            {

                chartId = 65;

                checkName = "Status Digitals";

                displayOrder = 65;

                inUse = 1;

                grouping = 4;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkAlarmStatus.Checked)

            {

                chartId = 66;

                checkName = "Alarm Status";

                displayOrder = 66;

                inUse = 1;

                grouping = 6;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkInverterStatus.Checked)

            {

                chartId = 67;

                checkName = "Inverter Status";

                displayOrder = 67;

                inUse = 1;

                grouping = 6;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkDigitals.Checked)

            {

                chartId = 68;

                checkName = "Digitals";

                displayOrder = 11;

                inUse = 1;

                grouping = 4;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }
 

            if (chkInverterHistory.Checked)

            {

                chartId = 70;

                checkName = "Inverter History";

                displayOrder = 68;

                inUse = 1;

                grouping = 6;
 

                success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);

            }

        }

Open in new window

0
Comment
Question by:Projack
  • 2
  • 2
5 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24369303
what are you doing in "adm_UnitPageDefinition"
Are you inserting the values or updating the values in the table?
0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 500 total points
ID: 24369491
Please let us know which field(s) are marked as Primary Key in the table TRAILER_PARK_PAGES.
I just noticed that you have entered charId value from 55 to 68 and then 70. Is it intentional to miss value 69 or it might creating the problem somehow in your stored procedure.
Anyways, check your procedure which is entering the new rows into the table TRAILER_PARK_PAGES because some INSERT or UPDATE command on this creating duplicated Primary Key value. If you could share the code of your stored procedure, we may try to point to error(s) if there is any.
0
 

Author Comment

by:Projack
ID: 24370770
fields are unitId, chartid, and grouping are all primary key in table TRAILER_PARK_PAGES
chartId 69 has been inserted, when the unit is created in unit table
 

Create Procudure adm_UnitPageDefinition

@unitId int, 

@chartId int ,

@charName varchar(50),

@displayOrder int,

@In_use int,

@grouping int
 

AS
 

insert into TRAILER_PARK_PAGES

values (@unitId, @chartId int, @charName, @displayOrder,@In_use, @grouping int)
 

go

Open in new window

0
 

Author Comment

by:Projack
ID: 24374113
From the error the exception occur at here

{
if (chkUserSettings.Checked)
{
chartId = 55;
checkName = "User Setting";
displayOrder = 55;
inUse = 1;
grouping = 10;  // it occured here
success = trailerParkIntregation.EnableUnit(unitId, chartId, checkName, displayOrder, inUse, grouping);
}
could it be that the loop in more that the item and is trying to insert the first row twice
for (int i = 0; i <= 14; i++)
0
 
LVL 11

Assisted Solution

by:Muhammad Ousama Ghazali
Muhammad Ousama Ghazali earned 500 total points
ID: 24374350
I again tried to read your requirement and code. I think you should remove the loop altogether because you are entering the rows/records based upon the Checked status of checkboxes already so loop is an additional block here.
See if it could help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now