Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
2
Reply
Error Handling in C# Asp.net
Michelle Carthy
Nov 2 2017 6:13 PM
168
Reply
I am unsure how to error handle my code as I am new to coding and C#, however I would like to use a try catch to stop my program from crashing when the wrong ID is entered into to textbox. It is used to add a new property to the database however if you do not enter the correct landlord ID the system crashes. I would like a textbox to show saying the error and not the system to completely crash. This is my code for behind the button
protected
void
btnLandlordSave_Click(
object
sender, EventArgs e)
{
//Creating a connection to my database using the connection string
string
cs = System.Configuration.ConfigurationManager.ConnectionStrings[
"rent-dbConnectionString1"
].ConnectionString;
SqlConnection con =
new
SqlConnection(cs);
//preparing a query which will insert the data entered in the textboxes to the database
SqlCommand cmd =
new
SqlCommand(
"INSERT INTO Properties(Landlord_Id, Property_Address, Property_Num_Of_Tenants, Property_Vacant) values('"
+
this
.txtLandlordId.Text +
"','"
+
this
.txtPropertyAddress.Text +
"','"
+
this
.txtNumOfTenants.Text +
"','"
+
this
.chkVacant.Checked +
"')"
, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(
"LandlordIndex.aspx"
);
}
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
How to use a join in an insert statement and sql server
Amadeus API Code Samples in c#