Inserting Mutiple Row into a 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
 
W

William Ryan

The last two inserts (1 5 d) and (1 5 e) have the same PK if I'm reading
this correctly.
 
H

Hai Nguyen

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
 
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();

}
 

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

Top