Reputation: 389
I am making an web application for articles reading.
I have bool property which is set to true if the specific Edit
Button is clicked, there is also a Save
Button which is clicked after filling user information on TextBoxes
and other controls. Whenever I run my application and fills information, it runs fine but after 4 or 5 runs (re-starting the application) it gives an Exception error upon clicking Save
Button:
Input string was not in a correct format
which is due to filling TextBoxes
with Null( firstly, Null is converted into Int).
My problem is that bool property (Managment.IsEditing
) is set to true without any reason ( Edit
Button should be pressed to set bool to true). Why and how is set to true automatically as its code is only being called in EditButton_Click
Event?
Here is a code
protected void EditButton_Click(object sender, EventArgs e)
{
if(EditorList.SelectedIndex > -1) //Just to ensure that Item is selected from ListBox
{
//editor is the poco , EditorManager is the editor table manager
editor = EditorManager.GetEditorInfo(EditorList.SelectedValue);
NameTextBox.Text = editor.Name;
EmailTextBox1.Text = editor.Email;
PasswordTextBox.Text = editor.Password;
EditorIDTextBox.Text = editor.Editor_ID.ToString();
for (int index = 0; index < RoleCheckBoxList.Items.Count; index++)
{
RoleCheckBoxList.Items[index].Selected = editor.RoleList[index];
}
Managment.IsEditing = true; //This flag is responsible for telling "SaveButtton" that editor would be updated.
DeleteButton.Enabled = true;
ResultLabel.Text = "";
}
else
ResultLabel.Text = "Select Editor from list first";
protected void SaveButton_Click(object sender, EventArgs e)
{
if(Managment.IsEditing == false) //it makes sure that new editor is being saved
{
editor.Name = NameTextBox.Text;
string email = EmailTextBox1.Text;
editor.Email = email.ToLower();
editor.Password = PasswordTextBox.Text;
if(EditorManager.IsEditorValid(editor.Email))
{
for (int index = 0; index < RoleCheckBoxList.Items.Count; index++)
{
editor.RoleList[index] = RoleCheckBoxList.Items[index].Selected;
}
EditorManager.Save(editor);
ResultLabel.Text = editor.DataUploadMessage;
}
else
ResultLabel.Text = "Editor with the same Email can't be add!";
FillEditorList();
}
else if(Managment.IsEditing == true) //it determines that existing editor is being updated and problem is that Managment.IsEditing is turned on without being called.
{
editor.Name = NameTextBox.Text;
string email = EmailTextBox1.Text;
editor.Email = email.ToLower();
editor.Password = PasswordTextBox.Text;
editor.Editor_ID = Convert.ToInt32(EditorIDTextBox.Text);// Error occurs at this line
for (int index = 0; index < RoleCheckBoxList.Items.Count; index++)
{
editor.RoleList[index] = RoleCheckBoxList.Items[index].Selected;
}
EditorManager.Edit(editor);
ResultLabel.Text = editor.DataUploadMessage;
Managment.IsEditing = false;
FillEditorList();
}
ClearFields(Form.Controls);
}
Exception occurs on that line:
editor.Editor_ID = Convert.ToInt32(EditorIDTextBox.Text);
and sorry for any inconvenience dear fellows
Upvotes: 2
Views: 542
Reputation: 14816
When the exception occurs on the indicated line, the line that resets your flag won't be run. The page will then likely be in an invalid state.
Try fixing/handling the error and clean up properly, and the problem might go away.
Upvotes: 1