Hi Guys I am trying to understand how to save and edited row to the database
private void BudgetGrid_RowEditEnding(object sender,
DataGridRowEditEndingEventArgs e)
{
SqlCommand gridcmd = new SqlCommand();
SqlConnection rwConn = null;
rwConn = new SqlConnection("server=localhost;" +
"Trusted_Connection=yes;" + "database=Production; " + "connection
timeout=30");
gridcmd.Connection = rwConn;
rwConn.Open();
//gridcmd.CommandText =
//"SELECT Id, Name, Quantity, Rate, Time FROM Budget";
gridcmd.CommandText =
"UPDATE Budget SET Id = @id, Name = @Name, " +
"Quantity = @Qty, Rate = @Rte WHERE Time = @Time";
SqlDataAdapter gridda = new SqlDataAdapter(gridcmd);
string strId = "@id".ToString();
int intID;
bool bintID = Int32.TryParse(strId, out intID);
string strName = "@Name".ToString();
string strQty = "@Qty".ToString();
int intQty;
bool bintQty = Int32.TryParse(strQty, out intQty);
string strRte = "@Rte".ToString();
int intRte;
bool bintRte = Int32.TryParse(strRte, out intRte);
string strTime = "@Time".ToString();
gridda.SelectCommand.Parameters.Add(
new SqlParameter("@id", SqlDbType.Int));
gridda.SelectCommand.Parameters["@id"].SqlValue = intID;
gridda.SelectCommand.Parameters.Add(
new SqlParameter("@Name", SqlDbType.VarChar));
gridda.SelectCommand.Parameters["@Name"].SqlValue = strName;
gridda.SelectCommand.Parameters.Add(
new SqlParameter("@Qty", SqlDbType.Int));
gridda.SelectCommand.Parameters["@Qty"].SqlValue = strQty;
gridda.SelectCommand.Parameters.Add(
new SqlParameter("@Rte", SqlDbType.Int));
gridda.SelectCommand.Parameters["@Rte"].SqlValue = strRte;
gridda.SelectCommand.Parameters.Add(
new SqlParameter("@Time", SqlDbType.VarChar));
gridda.SelectCommand.Parameters["@Time"].SqlValue = strTime;
DataTable griddt = new DataTable("Budget");
gridda.Fill(griddt);
gridda.UpdateCommand =
new SqlCommandBuilder(gridda).GetUpdateCommand();
BudgetGrid.ItemsSource = griddt.DefaultView;
gridda.Update(griddt);
rwConn.Close();
}
it displays fine. I can edit its but when I click on the other tab it does not update it goes back to the original data.
Most of the code I have been going through its either out dated.. or not what I am looking for.
so basically if i hit tab to the next row. under the event BudgetGrid_RowEditEnding
it should update the database.. but now its not.