TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Michelle Carthy
NA
52
11.5k
Error Handling in C# Asp.net
Nov 2 2017 6:13 PM
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"
);
}
Reply
Answers (
2
)
How to use a join in an insert statement and sql server
Amadeus API Code Samples in c#