TROUBLE UPDATING DataGrid cells back to the database

G

Guest

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.body,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
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top