Insert Mutiple row into database

H

Hai Nguyen

Hi all

I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field

(PR) (PR)
ID Project Ans
1 2 a
1 3 b
1 4 c
1 5 d
1 5 e


I got this error:
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again

Would you please tell me what to do to fix this problem?

Thanks
 
H

Hai Nguyen

Sorry my bad:
It should be like this

(PR) (PR)
ID Project Ans
1 2 a
1 3 b
1 4 c
1 5 d
1 5 e

Hi all

I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field

(PR) (PR)
ID Project Ans
1 2 a
1 3 b
1 4 c
1 5 d
1 5 e


I got this error:
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again

Would you please tell me what to do to fix this problem?

Thanks
 
S

Steve C. Orr [MVP, MCSD]

It looks like you have two rows that have the same primary key. Primary keys must be unique.
In your example it looks like your last row is conflicting with the row before it.
Try deleting the last row and see if that fixes it.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com



Hi all

I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field

(PR) (PR)
ID Project Ans
1 2 a
1 3 b
1 4 c
1 5 d
1 5 e


I got this error:
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again

Would you please tell me what to do to fix this problem?

Thanks
 
H

Hai Nguyen

Would you please take a lot at the code? It inserts into the table after the first try. When the 2nd time i comes back to insert it fails

PR) (PR)
ID Project Ans
1 2 a -> done
1 3 b -> fail here, program throws error
1 4 c
1 5 d




private void loadAnsToData(ArrayList myansList)

{

ArrayList qKeyList = storeQuestionKey();


string mystrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("dcpols.mdb");

OleDbConnection conn = new OleDbConnection(mystrConn);


string command = "Insert Into tblProjectData(PD_Proj_PKF,Q_PK,PD_Answer)"+

"Values (@projectID,@questionID,@answer)";

OleDbCommand myCommand = new OleDbCommand(command,conn);

myCommand.Connection.Open();

for (int i=0; i< qKeyList.Count;i++)

{

string answer = myansList.ToString();

string qKey = qKeyList.ToString();




OleDbParameter p1 = new OleDbParameter("@projectID",OleDbType.BigInt);

p1.Direction = ParameterDirection.Input;

p1.Value = Session["ProKey"];

myCommand.Parameters.Add(p1);



OleDbParameter p2 = new OleDbParameter("@projectID",OleDbType.BigInt);

p2.Direction = ParameterDirection.Input;

p2.Value = Convert.ToInt32(qKey);

myCommand.Parameters.Add(p2);



OleDbParameter p3 = new OleDbParameter("@projectID",OleDbType.VarChar);

p3.Direction = ParameterDirection.Input;

p3.Value = answer;



myCommand.Parameters.Add(p3);

myCommand.ExecuteNonQuery();

}



myCommand.Connection.Close();

}

William Ryan said:
How are you defining the DataRelation? With an Array of dataColumns?
http://www.knowdotnet.com/articles/datarelation.html

Where in the update is it failing? If it's failing on anything after (1,3)
then this isn't the problem, but let me see the DataRelation code...
Hi all

I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field

(PR) (PR)
ID Project Ans
1 2 a
1 3 b
1 4 c
1 5 d
1 5 e


I got this error:
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again

Would you please tell me what to do to fix this problem?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top