"The table does not contain a primary key"

I am trying to populate a combo box based on a value item that was selected from a data grid view.

Here is my code to initialize it

            DataSet dsCIF2 = new DataSet();
            DataRow drPitch;
            String sqlPitch = @" Select * from Pitch";
            String connStr5 = Properties.Resources.cString;
            SqlDataAdapter daPitch = new SqlDataAdapter(sqlPitch, connStr5);
            DataTable dtPitch = new DataTable();
            daPitch.Fill(dtPitch);
            daPitch.Fill(dsCIF2, "Pitch");

            comboBox2.DisplayMember = "PitchDesc";
            comboBox2.ValueMember = "PitchID";
            comboBox2.DataSource = dtPitch;

In the following code, I use this to find the pitch identifier from the selected row as a data grid, and it returns the correct tone id, as seen from debugging.

 int matchBookingID = 0;
            matchBookingID = Convert.ToInt32(DGV.SelectedRows[0].Cells[0].Value);
            drMatchData = dsCIF.Tables["MatchStats"].Rows.Find(matchBookingID);
                Pitch = Convert.ToInt32(drMatchData["PitchID"].ToString());

Now when I try to use this identifier to find datarow in the pitch table, I get an error

The table does not have a primary key

in this line of code

                drPitch = dsCIF2.Tables["Pitch"].Rows.Find(Pitch);

I do not know why I get this error, thanks in advance!

Update: table has SQL primary key CODE

create TABLE PITCH
(
PitchID int NOT NULL,
PitchDesc varchar(30) NOT NULL,
CONSTRAINT pkPitchID PRIMARY KEY(PitchID),
)
+4
source share
1 answer

, . ,

daPitch.FillSchema(dsCIF2, SchemaType.Source, "Pitch");

DataSet dsCIF2 = new DataSet();

String sqlPitch = @" Select * from Pitch";
String connStr5 = Properties.Resources.cString;
SqlDataAdapter daPitch = new SqlDataAdapter(sqlPitch, connStr5);
SqlCommandBuilder cmdBPitch = new SqlCommandBuilder(daPitch);

daPitch.FillSchema(dsCIF2, SchemaType.Source, "Pitch");

DataTable dtPitch = new DataTable();
daPitch.Fill(dtPitch);
daPitch.Fill(dsCIF2, "Pitch");
+2

Source: https://habr.com/ru/post/1696009/


All Articles