Reputation:
I am trying to display a success message after I have inserted data into my database, but it's not showing.
This is my code:
protected void BtnSubmit_Click(object sender, EventArgs e)
{
string ProjectName = txtProjectName.Text;
string ProjectCode = txtProjectCode.Text;
string ProjectDetails = txtProjectDetails.Text;
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PROJECTS"].ConnectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO tbl_projects (projectName, projectCode, projectDetails) VALUES (@ProjectName, @ProjectCode, @ProjectDetails)");
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@ProjectName", txtProjectName.Text);
cmd.Parameters.AddWithValue("@ProjectCode", txtProjectCode.Text);
cmd.Parameters.AddWithValue("@ProjectDetails", txtProjectDetails.Text);
cmd.ExecuteNonQuery();
con.Close();
}
catch (SqlException ex)
{
Toastr.ShowToast(ex.Message, "Success", Toastr.Type.Success);
}
}
If have also tried:
catch (Exception ex)
{
Toastr.ShowToast(ex.Message, "Success", Toastr.Type.Success);
}
Not sure why this is not working?
Upvotes: 0
Views: 832
Reputation: 31
Try {
// All statements here are executed until exception is reached, if all statements pass then one can display a success message. Thus at the end of the try statement(s) add the success message there.
}
Catch (Exception ex) {
// When Something goes wrong with the statements in Try then the exception message will be thrown.
}
// I would rather suggest that a success message be thrown at the end of your try statement...```
Upvotes: 1
Reputation: 9600
You are trying to issue a success message when you have an exception.
An Exception is basically an error which has been cause by your code. So instead you should put your success message after your insert, and if no exception is thrown then it has worked.
SqlConnection con = null; //Put this outside your try block
try
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["PROJECTS"].ConnectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO tbl_projects (projectName, projectCode, projectDetails) VALUES (@ProjectName, @ProjectCode, @ProjectDetails)");
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@ProjectName", txtProjectName.Text);
cmd.Parameters.AddWithValue("@ProjectCode", txtProjectCode.Text);
cmd.Parameters.AddWithValue("@ProjectDetails", txtProjectDetails.Text);
cmd.ExecuteNonQuery();
Toastr.ShowToast(ex.Message, "Success", Toastr.Type.Success);
}
catch (SqlException ex)
{
Toastr.ShowToast(ex.Message, "Error: Oh no, it didn't work!", Toastr.Type.Failed);
}
finally
{
//Close you connection (if set) in finally.
//This way it will also be closed if you do have an exception
if(con != null)
con.Close();
}
Also, in your current code the connection to the database will only be closed if your command is successful. Which means if you have an exception then the connection stays open and you have a memory leak and resource problem.
So I moved this into a finally block, which will also be executed, regardless of exception or not. But then you need to check that it is populated before trying to close
Upvotes: 2