Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update row in datagrid

Status
Not open for further replies.

flnMichael

Programmer
Nov 13, 2003
96
US
Can somebody please tell me what I have wrong here:
Code:
		private void KPC_Data_Grid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			sqlDataAdapter1.SelectCommand.Connection.Open();
			sqlDataAdapter1.SelectCommand.CommandText = "SELECT name,description,lsl,usl,units FROM KPC where OPID ='" + myOPID + "'";	
			sqlDataAdapter1.SelectCommand.ExecuteReader();
			sqlDataAdapter1.SelectCommand.Connection.Close();


			sqlDataAdapter1.Fill(kpC_Data_Set1, "KPC");
			sqlDataAdapter1.SelectCommand.Connection.Open();		
				
			TextBox name, desc, lsl, usl, unit;

			name = (TextBox)e.Item.Cells[2].Controls[0];
			desc = (TextBox)e.Item.Cells[3].Controls[0];
			lsl = (TextBox)e.Item.Cells[4].Controls[0];
			usl = (TextBox)e.Item.Cells[5].Controls[0];
			unit = (TextBox)e.Item.Cells[6].Controls[0];

			KPC_Data_Set.KPCDataTable myNewTable = kpC_Data_Set1.KPC;
			KPC_Data_Set.KPCRow rowToUpdate = (KPC_Data_Set.KPCRow)myNewTable.Rows[e.Item.ItemIndex];

			rowToUpdate.name =  name.Text;
			rowToUpdate.description = desc.Text;
			rowToUpdate.lsl = Convert.ToInt32(lsl.Text);
			rowToUpdate.usl = Convert.ToInt32(usl.Text);
			rowToUpdate.units =  unit.Text;

			sqlDataAdapter1.SelectCommand.Connection.Close();

			KPC_Data_Grid.DataSource = kpC_Data_Set1.KPC.DefaultView;
			KPC_Data_Grid.EditItemIndex=-1;
			KPC_Data_Grid.DataBind();

			sqlDataAdapter1.Update(kpC_Data_Set1);
			BindGrid();
		}

the error is:

Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

KPC_Data_Grid.DataBind();
sqlDataAdapter1.Update(kpC_Data_Set1);
BindGrid();

This is really getting to me...can someone shed some light on this for me???

Thanks
Mike
 
My guess is that you haven't created the CommandText for the UpdateCommand (see where you have created a SELECT statement for the SelectCommand, well the UpdateCommand needs one too).

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
makes sense....did that:

Code:
sqlDataAdapter1.UpdateCommand.Connection.Open();

sqlDataAdapter1.UpdateCommand.CommandText = "UPDATE KPC set name=" + name.Text + ",description=" + desc.Text + ",lsl=" + lsl.Text + ",usl=" + usl.Text + ",units=" + unit.Text + " where OPID ='" + myOPID + "'";	

sqlDataAdapter1.UpdateCommand.ExecuteNonQuery();

sqlDataAdapter1.UpdateCommand.Connection.Close();

sqlDataAdapter1.Update(kpC_Data_Set1);

Now it tells me:
Object reference not set to an instance of an object.

KPC_Data_Grid.DataBind();
sqlDataAdapter1.UpdateCommand.Connection.Open();


Am I doing the connect for the update wrong?
 
You would have to create a new instance of a command first. e.g.
Code:
sqlDataAdapter1.UpdateCommand = New SQLClient.OracleCommand(strSQL, MyConnection)

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
ok, please let me know what I have going on here, I have placed a new connection to update:

Code:
sqlDataAdapter1.SelectCommand.Connection.Open();

sqlDataAdapter1.SelectCommand.CommandText = "SELECT name,description,lsl,usl,units FROM KPC where OPID ='" + myOPID + "'";	
		
sqlDataAdapter1.SelectCommand.ExecuteReader();
		
sqlDataAdapter1.SelectCommand.Connection.Close();

sqlDataAdapter1.Fill(kpC_Data_Set1, "KPC");
	
sqlDataAdapter1.SelectCommand.Connection.Open();		
				
TextBox name, desc, lsl, usl, unit;

name = (TextBox)e.Item.Cells[2].Controls[0];
desc = (TextBox)e.Item.Cells[3].Controls[0];
lsl = (TextBox)e.Item.Cells[4].Controls[0];
usl = (TextBox)e.Item.Cells[5].Controls[0];
unit = (TextBox)e.Item.Cells[6].Controls[0];

KPC_Data_Set.KPCDataTable myNewTable = kpC_Data_Set1.KPC;
KPC_Data_Set.KPCRow rowToUpdate = (KPC_Data_Set.KPCRow)myNewTable.Rows[e.Item.ItemIndex];

rowToUpdate.name =  name.Text;
rowToUpdate.description = desc.Text;
rowToUpdate.lsl = Convert.ToInt32(lsl.Text);
rowToUpdate.usl = Convert.ToInt32(usl.Text);
rowToUpdate.units =  unit.Text;

sqlDataAdapter1.SelectCommand.Connection.Close();

KPC_Data_Grid.DataSource = kpC_Data_Set1.KPC.DefaultView;
KPC_Data_Grid.EditItemIndex = -1;
KPC_Data_Grid.DataBind();

String UpdateCmd = "UPDATE KPC set name=" + name.Text + ",description=" + desc.Text + ",lsl=" + lsl.Text + ",usl=" + usl.Text + ",units=" + unit.Text + " where OPID ='" + myOPID + "'";

SqlCommand myCommand = new SqlCommand(UpdateCmd, Conn);
myCommand.Connection = Conn;

try
{
  myCommand.ExecuteNonQuery();
  ShowMessage("Row Updated.");
  myCommand.Connection.Close();
}

catch
{
[COLOR=red]  myCommand.Connection.Close(); [/color]
  ShowMessage("Problem Updating Row.  Please Contact ...");
			}
sqlDataAdapter1.Update(kpC_Data_Set1);
BindGrid();

and now it tells me:
Object reference not set to an instance of an object
myCommand.Connection.Close();

The only instance I have for Conn outside of this is up at the top:

protected System.Data.SqlClient.SqlConnection Conn;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top