Using SQLAdapter.Update (Dataset) Throws Error "No Primary Key in Selection Command"

In short, C # throws me "Dynamic SQL generation for UpdateCommand is not supported in relation to SelectCommand, which does not return key column information", while the table in question has a primary key (Identity, since it is MSSQL).

My code is as follows:

       void loaddata()
    {
        try
        {
            DS = new DataSet();
            sqladapt = new SqlDataAdapter("SELECT servicetag,name,defect,primkey FROM " + Properties.Settings.Default.DBtableLaptops + "", sqlcon);
            sqladapt.FillSchema(DS, SchemaType.Source);
            sqladapt.Fill(DS);
            commandBuilder = new SqlCommandBuilder(sqladapt);
            DT = DS.Tables[0];
            Laptops_datagridview.DataSource = DT; //I'm using a datagridview to edit the data.
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
    void savedata()
    {
        //Doesn't work yet.
        try
        {
            sqladapt.Update(DS); // <-- Throws the aforementioned error.
            sqladapt.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Data loading, etc. works great, adds newlines and saves them. But whenever I update or delete a line, it throws an error. Primkey is my primary key, it is an automatically increasing integer.

Google, , , , , , .

, 6 .

EDIT: create :

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+3
3

primkey :

ALTER TABLE [dbo].[laptopregistratieDB_laptops]
   ADD CONSTRAINT PK_laptopregistratieDB_laptops PRIMARY KEY (primkey)

:

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL,
    constraint PK_laptopregistratieDB_laptops PRIMARY KEY (primkey)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

, , SQL Server ( , ):

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

SQL Server , ( , , " " )

, [primkey] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY. ( ).

+6

? , , ? DS.DataTables[0].PrimaryKey

DataTable DataSet. - :

DS.DataTables[0].PrimaryKey  = DS.DataTables[0].DataColumns[3];

.

+4

Error in line: " sqladapt = new SqlDataAdapter("SELECT servicetag,name,defect,primkey FROM ...because your selection request does not include a field that is primary or unique.

You need to fix the selected SQL by adding the main field to your table. For instance:

sqladapt = new SqlDataAdapter("SELECT ID, servicetag,name,defect,primkey FROM "

I hope for this help !!!

0
source

All Articles