0
我有這張名爲EVENT_ANNOUNCE的表,我用它在gridview中顯示數據。目前,我可以更新EVENTTYPE,EVENTNAME和STARTDATE。但是現在我想添加另一個ENDDATE列代碼,以便可以更新ENDDATE列。如何添加ENDDATE列代碼?正如您從第三張屏幕截圖中看到的那樣,我在輸入時出錯。以下代碼是沒有ENDDATE代碼的代碼。從第三張截圖開始,我添加了ENDDATE代碼後的代碼。由於如何添加另一個日期列字段代碼,以便更新可以在gridview中工作?
錯誤,當我試圖把在ENDDATE列字段中的代碼。
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))
{
updateEventGridviewRecord(id, strEventType, strEventName, datStartDate);
}
else
{
lblError.Visible = true;
lblError.Text = "Invalid Date";
lblSuccess.Visible = false;
}
}
private void updateEventGridviewRecord(int id, string strEventType, string strEventName, DateTime datStartDate)
{
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "UPDATE EVENT_ANNOUNCE SET [EVENTTYPE][email protected], [EVENTNAME][email protected], [STARTDATE][email protected] WHERE [ID][email protected]";
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);
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;
}
}
看起來像你試圖一口氣解析strStartDate和strEndDate。只需在if語句中寫一個DateTime.TryParseExact for strEndDate – ray