Reputation: 401
I have this table called EVENT_ANNOUNCE that I used to display the data in the gridview. Currently, I can update EVENTTYPE, EVENTNAME and STARTDATE fine. But now I would like to add in another ENDDATE column code so that ENDDATE column can be updated. How do I add in the ENDDATE column code? as you can see from the 3rd screenshot, I got error when putting in. For the codes below are codes that are without the ENDDATE codes. And from the 3rd screenshot are the codes after I have add in the ENDDATE codes. Thanks
ERROR when I tried to put in the code for ENDDATE column field.
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]=@EVENTTYPE, [EVENTNAME]=@EVENTNAME, [STARTDATE]=@STARTDATE 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);
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;
}
}
Upvotes: 0
Views: 49
Reputation: 457
Like this.
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);
}
Upvotes: 1