Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
'Table doesn't have a primary key.'

i have 2 tables
1. prtmst (from where i need to read data) (oldedb con)
2. accmst (to where i need to write data) (sql con)
accmst is created with primary key

i need to read data from "prtmst" and search into "accmst" and insert if not exist or update id already exist.

note : "prtmst" and "accmst" col name are not same

please help me for 'Table doesn't have a primary key.
got tired of this

What I have tried:

C#
private void Oacc_repl(SqlConnection NConn, OleDbConnection OConn)
        {
            OConn.Open();
            OleDbCommand ocmd = new OleDbCommand("select * from prtmst ", OConn);
            OleDbDataReader ordr = ocmd.ExecuteReader();

            NConn.Open();
            DataTable acctbl = new DataTable("dbo.accmst");
            acctbl.PrimaryKey = new DataColumn[] { acctbl.Columns["acc_code"] };


            while (ordr.Read())
            {
                //string k = ordr["prt_code"].ToString();
                //DataRow foundRow = acctbl.Rows.Find(k);
                //if (foundRow != null)
                if (acctbl.Rows.Contains(ordr["prt_code"]))
                {
                   MessageBox.Show("Record Found ..... ");
                }
            } 
       }
Posted
Updated 31-May-20 22:59pm
v2

1 solution

Read the error message - it couldn't be much clearer:
Table doesn't have a primary key.

You are trying to use a table that has no primary key as if it did.
The easiest way to fix this is to add an ID column and set it as the primary key.
 
Share this answer
 
Comments
Kishore_Patel 1-Jun-20 5:40am    
column "acc_code" is my primary key

do I have to change "acc_code" to "ID" ?
Kishore_Patel 1-Jun-20 5:44am    
I have created table as below :

string saccmst = "CREATE TABLE[dbo].[accmst] " +
"(acc_code nchar(5), acc_name nvarchar(50), acc_type nvarchar(3), off_add1 nvarchar(150), off_add2 nvarchar(35), off_add3 nvarchar(35), off_phone nvarchar(35), " +
"off_email nvarchar(35), off_mobile nvarchar(15), fac_add1 nvarchar(150), fac_add2 nvarchar(35), fac_add3 nvarchar(35), fac_mobile nvarchar(15), fac_phone nvarchar(35), fac_email nvarchar(35), " +
"gst_no nchar(15), cst_no nchar(15), pan_no nchar(15), auth_per nvarchar(35), bspl nchar(1), bspl_type nchar(1), bspl_grp nchar(3), bspl_sgrp nchar(3), brk_code nchar(5), " +
"grp_code nchar(5), off_state nvarchar(15), CONSTRAINT kacode PRIMARY KEY CLUSTERED (acc_code), INDEX ianame NONCLUSTERED (acc_name), index iacode NONCLUSTERED (acc_code))";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900