This is a migrated thread and some comments may be shown as answers.

Update query in grid

1 Answer 19 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arun
Top achievements
Rank 1
Arun asked on 20 Jun 2013, 04:02 AM
Hi,

            I tried the update query to update the details in grid control with below code.But I got the error(Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
). when i click the update button.

 My Code:
              GridDataItem updateitem = (e.Item as GridDataItem);
            string id = updateitem.OwnerTableView.DataKeyValues[updateitem.ItemIndex]["userid"].ToString();//Error Comes in this line
            TextBox txtuname = (updateitem)["userid"].Controls[0] as TextBox;
            TextBox txtdob = (updateitem)["dateofjoin"].Controls[0] as TextBox;
            TextBox txtpwd = (updateitem)["pwd"].Controls[0] as TextBox;
            string unameupdate = txtuname.Text;
            string dobupdate = txtdob.Text;
            string pwdupdate = txtpwd.Text;
            conn.Open();
            string updateaccount = "update login set userid ='" + unameupdate + "',pwd ='" + pwdupdate + "',cpwd ='" + pwdupdate + "',dateofjoin ='" + dobupdate + "' where userid ='" + id + "'";
            cmd = new MySqlCommand(updateaccount, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            showaccountuser();

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 20 Jun 2013, 07:11 AM
Hi Arun,

I guess you want to update a row in the grid. Please try the following example code snippet.

C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
   if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           GridEditableItem updateitem = (e.Item as GridEditableItem);
           string id = updateitem.OwnerTableView.DataKeyValues[updateitem.ItemIndex]["OrderID"].ToString();
           TextBox txtuname = (updateitem)["CustomerID"].Controls[0] as TextBox;
           TextBox txtdob = (updateitem)["OrderDate"].Controls[0] as TextBox;
           TextBox txtpwd = (updateitem)["ShipName"].Controls[0] as TextBox;
           string unameupdate = txtuname.Text;
           string dobupdate = txtdob.Text;
           string pwdupdate = txtpwd.Text;
           conn.Open();
           string updateaccount = "update Orders set CustomerID ='" + unameupdate + "',OrderDate ='" + dobupdate + "',ShipName ='" + pwdupdate + "' where OrderID ='" + id + "'";
           SqlCommand = new SqlCommand(updateaccount, conn);
           SqlCommand.ExecuteNonQuery();
           conn.Close();     
       }
   }

Thanks,
Princy
Tags
Grid
Asked by
Arun
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or