By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 918 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

TROUBLE UPDATING DataGrid cells back to the database

P: n/a
I'm having trouble updating individual datagrid cells.

Have two tables car_master (columns include Car_ID, YEAR,VEHICLE) and
car_detail (columns include Car_ID,PRICE,MILEAGE,and BODY);both tables have a
FK relationship on CAR_ID

so the oracledataadapter1 select statement(CommandText) is:

select car_master.car_id,
car_master.year,car_master.vehicle,car_detail.car_ id AS EXPR1,
car_detail.price,car_detail.mileage,car_detail.bod y,car_detail.body FROM
car_master,car_detail where car_master.car_id=car_detail.car_id;
*****************
the Foreign Key relationship I used:
alter table car_detail add constraint detailFK(CAR_ID) references
car_master(CAR_ID);

****************************
The c# DataGrid updatecommand code:

TextBox tb;

string key = dgDTMgmt.DataKeys[e.Item.ItemIndex].ToString();
//Year column
tb = (TextBox)e.Item.Cells[1].Controls[0];
string stYr = tb.Text.Trim().ToString();
int.Parse(stYr);

//Vehicle column
tb = (TextBox)e.Item.Cells[2].Controls[0];
string stVhcle = tb.Text.Trim().ToUpper();

TextBox price = (TextBox)e.Item.FindControl("txtPrice");

string stPrice = price.Text;
//decimal pr = Convert.ToDecimal(stPrice);
//string stPriceVal = pr.ToString();

//Mile column
tb = (TextBox)e.Item.Cells[5].Controls[0];
string dMile = tb.Text;

//Body column
tb = (TextBox)e.Item.Cells[6].Controls[0];
string strBody = tb.Text.Trim().ToUpper();

string sql = "update car_detail set PRICE ='" + stPrice + "',MILEAGE='" +
dMile + "',BODY= '" + strBody + "' where car_id = " + key;

try
{
if (oracleConnection1.State == ConnectionState.Closed)
{
oracleConnection1.Open();
}
OracleCommand cmd = new OracleCommand(sql,oracleConnection1);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{}

String sql2 = "update car_master set YEAR = '" + stYr + "',VEHICLE= '" +
stVhcle + "' where car_id = " + key;

try
{
if (oracleConnection1.State == ConnectionState.Closed)
{
oracleConnection1.Open();
}

OracleCommand cmd2 = new OracleCommand(sql2,oracleConnection1);
cmd2.ExecuteNonQuery();
}
catch (Exception ex)
{}

finally
{
if (oracleConnection1.State==ConnectionState.Open)
{
//DataGrid Non-Editable Mode
DataGrid1.EditItemIndex = -1;
oracleDataAdapter1.Fill(dsCarList1);
DataGrid1.DataBind();
oracleConnection1.Close();
}

}
}

**************************
I'm having trouble updating just the price column. The only way to update
the price column is if the user edits several columns and then the price
column updates.

I want the user to be able to update a single column at a time and not have
to update multiple columns at the same time.

I don't know what I'm doing wrong. I'm thinking that my select commandtext
statement is the problem.

Any suggestions would be greatly appreciated.

If there are better ways of updating individual datagrid cells back to the
database, please provide some c# code examples.

Thanks.

bebop

Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.