Results 1 to 3 of 3

Thread: Help with updating Datagrid

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Help with updating Datagrid

    I have a wierd problem. I have made a copy/paste from one of my programs that finns a datagrid with values from a sql db. And it works fine in that example, but when I use the same code with the oledb and an access db I get an error... here is the code for the updatecommand

    PHP Code:
        protected void DataGrid1_UpdateCommand(object sourceDataGridCommandEventArgs e)
            {
                                
                
    OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\inetpub\\wwwroot\\webreport\\userdb.mdb");
                

                
    OleDbCommand cmd = new OleDbCommand("Update",myConn);
                
    cmd.CommandType CommandType.StoredProcedure;  
                
                
                
    OleDbParameter param = new OleDbParameter();
                
                
                
                
                
    string userid DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
                
    param cmd.Parameters.Add("@puserid",OleDbType.varchar);
                
    param.Direction ParameterDirection.Input;
                
    param.Value userid;
                
                
    string username = ((TextBox)e.Item.Cells[1].Controls[0]).Text
                
    param cmd.Parameters.Add("@pusername",OleDbType.VarChar);
                
    param.Direction ParameterDirection.Input;
                
    param.Value username;
                
                
    string password = ((TextBox)e.Item.Cells[2].Controls[0]).Text
                
    param cmd.Parameters.Add("@ppassword",OleDbType.VarChar);
                
    param.Direction ParameterDirection.Input;
                
    param.Value password;

                
    string admin = ((TextBox)e.Item.Cells[3].Controls[0]).Text
                
    param cmd.Parameters.Add("@padmin",OleDbType.Boolean);
                
    param.Direction ParameterDirection.Input;
                
    param.Value System.Convert.ToBoolean(admin);
                
            



                
    cmd.Connection.Open();
                
    cmd.ExecuteNonQuery();
                
    cmd.Connection.Close();
                
    DataGrid1.EditItemIndex = -1;
                
    BindGrid();
                
            } 
    The error occurs on the line

    PHP Code:

                string userid 
    DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); 
    and the error is:

    Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

    The only thing I can think of is that primary keys are different in Access and SQL... other than that there is no difference between the code...
    The stored procedure looks like:

    UPDATE reportusers SET username = [@pusername], [password] = [@ppassword], admin = [@padmin]
    WHERE userid=[@puserid];



    any help would be nice! This is a wierd problem...


    kind regards
    Henrik

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Double check that you set the datakey property for the datagrid to the correct column.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Hmm, when I made the first example (from which I had borrowed code to this one) I don't recall setting such a property... I assumed it automatically recogniced the db primary key column...? which is the first one...

    kind regards
    Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width