By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Insert Mutiple row into database

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Hai Nguyen" <ha******@neo.tamu.edu> wrote in message news:uA**************@tk2msftngp13.phx.gbl...
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
Nov 18 '05 #2

P: n/a
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

"Hai Nguyen" <ha******@neo.tamu.edu> wrote in message news:uA**************@tk2msftngp13.phx.gbl...
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
Nov 18 '05 #3

P: n/a
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[i].ToString();

string qKey = qKeyList[i].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" <do********@nospam.comcast.net> wrote in message news:u7****************@tk2msftngp13.phx.gbl...
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...


"Hai Nguyen" <ha******@neo.tamu.edu> wrote in message
news:Os*************@TK2MSFTNGP12.phx.gbl...
Oh my bad
(PR) (PR)
ID Project Ans
1 2 a
1 3 b
1 4 c
1 5 d
1 6 e

I want it look like that but i fail when I loop to insert those rows in

the
database

"Hai Nguyen" <ha******@neo.tamu.edu> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
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




"Hai Nguyen" <ha******@neo.tamu.edu> wrote in message news:uA**************@tk2msftngp13.phx.gbl...
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
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.