Click here to Skip to main content
15,889,842 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am not able to update data in the datagrid.
Its showing the option to update but after giving new value its not taking that value from the text boxes.

protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{

    //OleDbConnection con;
    //OleDbCommand cmd;
    try
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=d:\\tanu\\database_hardware.mdb;");
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "UPDATE additem SET name=@name, Item_qty=@Item_qty where Item_id=@Item_id";

        cmd.Parameters.Add("@name", OleDbType.VarChar).Value = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
        cmd.Parameters.Add("@Item_qty", OleDbType.Numeric).Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
        cmd.Parameters.Add("@Item_id", OleDbType.Numeric).Value = DataGrid1.DataKeys[e.Item.ItemIndex];

        cmd.Connection = con;
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();

        DataGrid1.EditItemIndex = -1;
        Binddata();
        
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

this is my code for update. Please tell me where its wrong


ya Unfortunately code is running.
And i m not able to get the mistake.
And where it is terrible ??
Posted
Updated 24-Mar-10 1:45am
v2

Deleting messages is broken, but I removed this because the OP one voted me for offering to help
 
Share this answer
 
v2
Please don't push 'answer' if you're posting more questions.

I did answer your question and tell you how to solve your problem, but you one voted me, so I deleted it. Your code is terrible, your variable names are terrible, your approach to using a database is terrible. I'd fire anyone who turned in code like this. I'd tell you how to fix it, but, you 1 voted me.
 
Share this answer
 
Is this code running ? It's obviously terrible design to have code like this, but if it's being run, and if you can step through it in the debugger, and it appears to work then the main question would be, is the data source not updated ?
 
Share this answer
 
[Edit : Reply as answer added to question]
 
Share this answer
 
v2

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