1 Replies - 2579 Views - Last Post: 02 May 2010 - 06:58 PM

#1 vivekrane1986   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 02-August 08

Insert records in ms access using select.

Posted 01 May 2010 - 12:10 AM

Hi,

I am trying to insert records in MS access database from another database table. The problem I am facing is with primary key. There are few records in another table that have same key as that in current table.

So I have decided to insert primary key by my self and records using select statement and that's where I stuck.

Here is my code I am gettting error as Syntax error in insert statement.
Can anyone figure out what I can do to achieve it?

Here is my code:

foreach (DataGridViewRow dr in dgvImport.Rows)
            {
                if (dr.Cells[0].Value != null) //Cells[0] Because in cell 0th cell we have added checkbox
                {
                    int intSrNo = ApplicantManager.getMaxid() + 1;
                string strApplicantNo = GeneralDeclaration.gstrWardName.Substring(0, 3) + "/" + (intSrNo).ToString("0000");

                string query = "Insert INTO [MS Access;DATABASE=" + Application.StartupPath + "\\" + GeneralDeclaration.gstrDatabaseame + "].[TblMstApplicant] values " + intSrNo + "," + strApplicantNo + ", Select SerialNo,TenementHolder1,TenementHolder2,TenementHolder3,TenementHolder4,TenementHolder5,SpouseName1,SpouseName2,SpouseName3,SpouseName4,SpouseName5,Street,Area,City,FamilyMem1,FamilyMem2,FamilyMem3,FamilyMem4,FamilyMem5,FamilyMem6,FamilyMem7,FamilyMem8,FamilyMem9,FamilyMem10,AllotDate,Que1,Ans1,Que2,Ans2,Que3,Ans3,Que4,Ans4,CreatedBy,CreatedDate,ModifiedBy,ModofiedDate,Remark from TblMstApplicant where ApplicantNo='" + dr.Cells[1].Value+"'";

                if (!(adm.Execute(query) == GeneralDeclaration.gConst4Ok))
                {
                    MessageBox.Show("Error while importing records");
                    break;
                }
              }



Thanks and Regards,
Vivek Rane

Is This A Good Question/Topic? 0
  • +

Replies To: Insert records in ms access using select.

#2 nkasei28   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Re: Insert records in ms access using select.

Posted 02 May 2010 - 06:58 PM

why not perform the select separately in one query execution prior to the insert and then add the result to the insert statement in another query.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1