我有这个名为EVENT_ANNOUNCE的表。单击gridview上的编辑按钮后,单击更新按钮后,我想将今天的日期更新到数据库中的列字段MODIFIEDDATE。如果单击取消,则不会将今天的日期更新为列字段MODIFIEDDATE。我只想要更新。
protected void grdEvent_RowEditing(object sender, GridViewEditEventArgs e)
{
grdEvent.EditIndex = e.NewEditIndex;
bindEventGridView();
}
protected void grdEvent_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int selectedRow = e.RowIndex; //get selected row
// get product id from data key
int id = (int)grdEvent.DataKeys[selectedRow].Value;
// get current grid view row
GridViewRow row = (GridViewRow)grdEvent.Rows[selectedRow];
TextBox eventtype = (TextBox)row.FindControl("txtEventType");
// find text box for txtPrice
TextBox eventname = (TextBox)row.FindControl("txtEventName");
TextBox startdate = (TextBox)row.FindControl("txtStartDate");
TextBox enddate = (TextBox)row.FindControl("txtEndDate");
// Remove $ sign
string strEventType = eventtype.Text;
string strEventName = eventname.Text;
string strStartDate = startdate.Text;
string strEndDate = enddate.Text;
DateTime datStartDate;
DateTime datEndDate;
if (DateTime.TryParseExact(strStartDate, new string[] { "dd/MM/yyyy" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datStartDate)
&&
DateTime.TryParseExact(strEndDate, new string[] { "dd/MM/yyyy" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datEndDate)
)
{
updateEventGridviewRecord(id, strEventType, strEventName, datStartDate, datEndDate);
}
/*
|| DateTime.TryParseExact(strEndDate, new string[] { "dd/MM/yyyy" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datEndDate
*/
else
{
lblError.Visible = true;
lblError.Text = "Invalid Date";
lblSuccess.Visible = false;
}
}
private void updateEventGridviewRecord(int id, string strEventType, string strEventName, DateTime datStartDate, DateTime datEndDate)
{
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "UPDATE EVENT_ANNOUNCE SET [EVENTTYPE]=@EVENTTYPE, [EVENTNAME]=@EVENTNAME, [STARTDATE]=@STARTDATE, [ENDDATE]=@ENDDATE WHERE [ID]=@ID";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@EVENTTYPE", strEventType);
cmd.Parameters.AddWithValue("@EVENTNAME", strEventName);
cmd.Parameters.AddWithValue("@STARTDATE", datStartDate);
cmd.Parameters.AddWithValue("@ENDDATE", datEndDate);
myConnect.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
lblSuccess.Visible = true;
lblSuccess.Text = "Record updated!";
lblError.Visible = false;
}
else
{
lblSuccess.Visible = true;
lblError.Text = "Update fail";
lblError.Visible = false;
}
myConnect.Close();
//Cancel Edit Mode
grdEvent.EditIndex = -1;
bindEventGridView();
}
catch
{
lblError.Visible = true;
lblError.Text = "Please Enter Approximate data";
lblSuccess.Visible = false;
}
}
答案 0 :(得分:3)
设置MODIFIEDDATE = GETDATE()
将SQL更新语句更改为
UPDATE EVENT_ANNOUNCE SET [EVENTTYPE]=@EVENTTYPE, [EVENTNAME]=@EVENTNAME, [STARTDATE]=@STARTDATE, [ENDDATE]=@ENDDATE, MODIFIEDDATE = GETDATE() WHERE [ID]=@ID
答案 1 :(得分:2)
使用In Query
UPDATE EVENT_ANNOUNCE SET [EVENTTYPE]=@EVENTTYPE, [EVENTNAME]=@EVENTNAME, `[STARTDATE]=@STARTDATE, [ENDDATE]=@ENDDATE, MODIFIEDDATE = GETDATE() WHERE [ID]=@ID`
或
在SqlParameter中
cmd.Parameters.AddWithValue("@MODIFIEDDATE", DateTime.Now);