Editing the records in database via DataGridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • artteam
    New Member
    • Feb 2007
    • 18

    Editing the records in database via DataGridView

    Hi frnds,
    I am working on a project in which one of the form has a datagridview, I was successful in retrieving the table from the database.Now that I want to edit the records on this datagrid and save them back in the physical database.I have been try to do this in a number of ways but getting some or other error pops up.
    The following is the code I have used.

    myUpdateCommand .CommandText = @"UPDATE Inventory SET ItemDescription = ?, ItemNumber = ?, ItemCode = ?, ItemType = ?, Location = ?, Unit = ?, OurPrice = ?, StudentPrice = ?, PONumber = ?, Quantity = ? WHERE (ItemDescriptio n = ?) AND (ItemNumber = ?) AND (ItemType = ?) AND (Location = ?) AND (Unit = ?) AND (OurPrice = ?) AND (StudentPrice = ?) AND (PONumber = ?) AND (Quantity = ?)";
    this.myUpdateCo mmand.Connectio n = this.myOleDbCon nection;
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "ItemDescriptio n",this.dataGri dView1.CurrentR ow.Cells[0].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "ItemNumber",th is.dataGridView 1.CurrentRow.Ce lls[1].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "ItemCode",this .dataGridView1. CurrentRow.Cell s[2].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "ItemType",this .dataGridView1. CurrentRow.Cell s[3].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "Location",this .dataGridView1. CurrentRow.Cell s[4].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "DateItemCreate d",this.dataGri dView1.CurrentR ow.Cells[5].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "Unit",this.dat aGridView1.Curr entRow.Cells[6].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "OurPrice",this .dataGridView1. CurrentRow.Cell s[7].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "StudentPrice", this.dataGridVi ew1.CurrentRow. Cells[8].Value.ToString ()));
    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "PONumber",this .dataGridView1. CurrentRow.Cell s[9].Value.ToString ()));

    this.myUpdateCo mmand.Parameter s.Add(new OleDbParameter( "Quantity",this .dataGridView1. CurrentRow.Cell s[10].Value.ToString ()));
    this.myOleDbDat aAdapter.Update Command = myUpdateCommand ;

    DataSet myChangedDataSe t = myDataSet.GetCh anges();
    this.myDataSet. GetChanges();
    btnSave.Enabled = false;
    this.myOleDbDat aAdapter.Update (myDataSet, "Inventory" );

    Could someone help me out with this and let me know what statement is causing the problem or is it the entire technique thats causing the problem?

    Thank You in advance!!!
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Your update statement is not going to work, because you are trying to set values to a record and identify the record using those values which don't exist in the database. Do you have a unique identifier in the database? This is usually an id number and you usually update the record using this as the sole identifier - Where uniqueID = thisUniqueID...

    Hope that this helps.

    Comment

    Working...