Getting Values from an Updatable data grid

Discussion in 'ASP .Net' started by =?Utf-8?B?S2lyYW4gS3VtYXIgUGluamFsYQ==?=, Oct 31, 2005.

  1. Hi,
    May be this is silly, or I just need a second pair eyes to look at this.

    I am trying to get values that I have edited in a datagrid and update the
    values with those values. Here is the code that I am using in my update
    command.

    TextBox txtProdName = (TextBox)e.Item.Cells[1].Controls[0];
    TextBox txtSuppID = (TextBox)e.Item.Cells[2].Controls[0];
    TextBox txtCatID = (TextBox)e.Item.Cells[3].Controls[0];
    TextBox txtQtyPerUnit = (TextBox)e.Item.Cells[4].Controls[0];
    TextBox txtUnitPrice = (TextBox)e.Item.Cells[5].Controls[1];
    TextBox txtUnitsInStock = (TextBox)e.Item.Cells[6].Controls[0];
    TextBox txtUnitsOnOrder = (TextBox)e.Item.Cells[7].Controls[0];
    TextBox txtReorderLevel = (TextBox)e.Item.Cells[8].Controls[0];
    CheckBox ChkDiscontinued = (CheckBox)e.Item.Cells[9].Controls[1];

    string strSQL;

    strSQL = "UPDATE Products SET ";
    strSQL += "ProductName = '" + txtProdName.Text.Replace("'","'''") + "',";
    strSQL += "SupplierID = " + txtSuppID.Text + "," ;
    strSQL += "CategoryID = " + txtCatID.Text + ",";
    strSQL += "QuantityPerUnit = '" + txtQtyPerUnit.Text + "',";
    strSQL += "UnitPrice = " + txtUnitPrice.Text + ",";
    strSQL += "UnitsInStock = " + txtUnitsInStock.Text + ",";
    strSQL += "UnitsOnOrder = " + txtUnitsOnOrder.Text + ",";
    strSQL += "ReorderLevel = " + txtReorderLevel.Text + ",";
    strSQL += "Discontinued = " +
    Convert.ToInt32(Convert.ToBoolean(ChkDiscontinued.Checked));
    strSQL += " WHERE ProductID = " + e.Item.Cells[0].Text;

    I am only getting values that I already have before I edited the cells. What
    Am I doing wrong here.
    --
    Thanks
    Pinjala.
    =?Utf-8?B?S2lyYW4gS3VtYXIgUGluamFsYQ==?=, Oct 31, 2005
    #1
    1. Advertising

  2. Try:

    if(!this.IsPostback)
    {
    datagrid.DataSource = dataObject;
    datagrid.DataBind();
    }

    Otherwise, once posting back, editing values are replaced by values from
    data source.

    HTH

    Elton Wang


    "Kiran Kumar Pinjala" wrote:

    > Hi,
    > May be this is silly, or I just need a second pair eyes to look at this.
    >
    > I am trying to get values that I have edited in a datagrid and update the
    > values with those values. Here is the code that I am using in my update
    > command.
    >
    > TextBox txtProdName = (TextBox)e.Item.Cells[1].Controls[0];
    > TextBox txtSuppID = (TextBox)e.Item.Cells[2].Controls[0];
    > TextBox txtCatID = (TextBox)e.Item.Cells[3].Controls[0];
    > TextBox txtQtyPerUnit = (TextBox)e.Item.Cells[4].Controls[0];
    > TextBox txtUnitPrice = (TextBox)e.Item.Cells[5].Controls[1];
    > TextBox txtUnitsInStock = (TextBox)e.Item.Cells[6].Controls[0];
    > TextBox txtUnitsOnOrder = (TextBox)e.Item.Cells[7].Controls[0];
    > TextBox txtReorderLevel = (TextBox)e.Item.Cells[8].Controls[0];
    > CheckBox ChkDiscontinued = (CheckBox)e.Item.Cells[9].Controls[1];
    >
    > string strSQL;
    >
    > strSQL = "UPDATE Products SET ";
    > strSQL += "ProductName = '" + txtProdName.Text.Replace("'","'''") + "',";
    > strSQL += "SupplierID = " + txtSuppID.Text + "," ;
    > strSQL += "CategoryID = " + txtCatID.Text + ",";
    > strSQL += "QuantityPerUnit = '" + txtQtyPerUnit.Text + "',";
    > strSQL += "UnitPrice = " + txtUnitPrice.Text + ",";
    > strSQL += "UnitsInStock = " + txtUnitsInStock.Text + ",";
    > strSQL += "UnitsOnOrder = " + txtUnitsOnOrder.Text + ",";
    > strSQL += "ReorderLevel = " + txtReorderLevel.Text + ",";
    > strSQL += "Discontinued = " +
    > Convert.ToInt32(Convert.ToBoolean(ChkDiscontinued.Checked));
    > strSQL += " WHERE ProductID = " + e.Item.Cells[0].Text;
    >
    > I am only getting values that I already have before I edited the cells. What
    > Am I doing wrong here.
    > --
    > Thanks
    > Pinjala.
    =?Utf-8?B?RWx0b24gVw==?=, Nov 1, 2005
    #2
    1. Advertising

  3. Thanks, This worked.
    --
    Thanks
    Kiran Kumar Pinjala


    "Elton W" wrote:

    > Try:
    >
    > if(!this.IsPostback)
    > {
    > datagrid.DataSource = dataObject;
    > datagrid.DataBind();
    > }
    >
    > Otherwise, once posting back, editing values are replaced by values from
    > data source.
    >
    > HTH
    >
    > Elton Wang
    >
    >
    > "Kiran Kumar Pinjala" wrote:
    >
    > > Hi,
    > > May be this is silly, or I just need a second pair eyes to look at this.
    > >
    > > I am trying to get values that I have edited in a datagrid and update the
    > > values with those values. Here is the code that I am using in my update
    > > command.
    > >
    > > TextBox txtProdName = (TextBox)e.Item.Cells[1].Controls[0];
    > > TextBox txtSuppID = (TextBox)e.Item.Cells[2].Controls[0];
    > > TextBox txtCatID = (TextBox)e.Item.Cells[3].Controls[0];
    > > TextBox txtQtyPerUnit = (TextBox)e.Item.Cells[4].Controls[0];
    > > TextBox txtUnitPrice = (TextBox)e.Item.Cells[5].Controls[1];
    > > TextBox txtUnitsInStock = (TextBox)e.Item.Cells[6].Controls[0];
    > > TextBox txtUnitsOnOrder = (TextBox)e.Item.Cells[7].Controls[0];
    > > TextBox txtReorderLevel = (TextBox)e.Item.Cells[8].Controls[0];
    > > CheckBox ChkDiscontinued = (CheckBox)e.Item.Cells[9].Controls[1];
    > >
    > > string strSQL;
    > >
    > > strSQL = "UPDATE Products SET ";
    > > strSQL += "ProductName = '" + txtProdName.Text.Replace("'","'''") + "',";
    > > strSQL += "SupplierID = " + txtSuppID.Text + "," ;
    > > strSQL += "CategoryID = " + txtCatID.Text + ",";
    > > strSQL += "QuantityPerUnit = '" + txtQtyPerUnit.Text + "',";
    > > strSQL += "UnitPrice = " + txtUnitPrice.Text + ",";
    > > strSQL += "UnitsInStock = " + txtUnitsInStock.Text + ",";
    > > strSQL += "UnitsOnOrder = " + txtUnitsOnOrder.Text + ",";
    > > strSQL += "ReorderLevel = " + txtReorderLevel.Text + ",";
    > > strSQL += "Discontinued = " +
    > > Convert.ToInt32(Convert.ToBoolean(ChkDiscontinued.Checked));
    > > strSQL += " WHERE ProductID = " + e.Item.Cells[0].Text;
    > >
    > > I am only getting values that I already have before I edited the cells. What
    > > Am I doing wrong here.
    > > --
    > > Thanks
    > > Pinjala.
    =?Utf-8?B?S2lyYW4gS3VtYXIgUGluamFsYQ==?=, Nov 1, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. x
    Replies:
    1
    Views:
    717
    Kevin Spencer
    Nov 6, 2003
  2. =?Utf-8?B?Smlt?=

    Use of updatable query questions

    =?Utf-8?B?Smlt?=, Oct 4, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    381
    Adam Knight
    Oct 4, 2005
  3. Patrick Guio

    class for "updatable enum"

    Patrick Guio, Nov 8, 2004, in forum: C++
    Replies:
    0
    Views:
    332
    Patrick Guio
    Nov 8, 2004
  4. Ray Porter
    Replies:
    2
    Views:
    369
    Ray Porter
    Sep 1, 2010
  5. Black

    Updatable Datagrid with Multipage

    Black, Aug 2, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    105
    Black
    Aug 2, 2005
Loading...

Share This Page