Getting Values from an Updatable data grid

G

Guest

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.
 
G

Guest

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
 
G

Guest

Thanks, This worked.
--
Thanks
Kiran Kumar Pinjala


Elton W said:
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 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top