Click here to Skip to main content
14,766,101 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys.
I am working with my project and having a hard time on how to update multiple data using datagridview. I will display the data from my database to my datagridview in my form 1 after searching. From there, I will update and insert data in the database from my datagridview. I can insert data to my datagridview but when I will Update it by clicking my Update Button, an error message will display.

Here is my code:

//Search Button
private void button1_Click(object sender, EventArgs e)
           MySqlConnection = new SqlConnection("Data Source=BENJ\\SQLEXPRESS;Initial Catalog= ComputerizedEnrollmentSystem; User ID=sa; Password=aspire4736z");
           m_da = new SqlDataAdapter("SELECT StudentID as 'Student Number', StudentFirstName as 'First Name', StudentLastName as 'Last Name', GradeLevel as 'Level', SectionName as 'Section Name', SectionCode as 'Section Code',FinalGrade as 'Final Grade' From tblClass Where GradeLevel='" + comboBox1.SelectedItem + "' AND SectionName='" + comboBox2.SelectedItem + "' AND SectionCode='" + comboBox3.SelectedItem + "' ", MySqlConnection);
           ds = new System.Data.DataSet();
           dg_grade.DataSource = ds.Tables[0];



//Update Button
private void button2_Click(object sender, EventArgs e)
        cmd = new SqlCommandBuilder(m_da);
        m_da.Update(ds, "Student_Info");
        MessageBox.Show("Grade Inserted Successfully", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);

    catch(Exception ex)
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


Here is the error message:
"Dynamic SQL Generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information "

Please somebody help me :) I am new here.
DarkDreamer08 29-Dec-14 10:49am
I solved it now! I just need to indicate a primary key to my table where I load data to my data gridview :)
ridoy 29-Dec-14 10:51am
good to know that, thumbsup!
Prabhani Panamulla 30-Dec-14 3:49am
cheers !!
[no name] 29-Dec-14 20:19pm
Good job!!!

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