Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > ASP.NET > ASP.NET Syntax > ASP.NET Try Catch Exception



Clound SSD Virtual Server

ASP.NET Try Catch Exception

ASP.NET Try Catch Exception ใน .NET Framework เป็นรูปแบบคำสั่งใช้ดักข้อผิดพลาด (Error) ของโปรแกรม โดยเราสามารถใช้คำสั่ง Try ครอบในส่วนของ คำสั่ง โดยเมื่อเกิดการผิดพลาดในส่วนของคำสั่งโปรแกรมจะวิ่งไปในส่วนของ Catch ex As Exception , catch (Exception ex) เพื่อรอผู้ใช้ตรวจสอบข้อผิดพลาด หรือบันทึกข้อผิดพลาดของโปรแกรม โดยที่โปรแกรมจะยังสามารถทำงานต่อไปได้

Language Code : VB.NET || C#

Syntax (VB.NET)

        Try
            '*** Code & Command ***'
        Catch ex As Exception
            '*** Event เมื่อเกิด Error ***'
        Finally
            '*** (Option) Event เมื่อคำสั่งสมบูรณ์ ***'
        End Try


Sample (VB.NET)

Dim objConn As System.Data.OleDb.OleDbConnection
Dim objCmd As System.Data.OleDb.OleDbCommand
Dim strConnString,strSQL As String
Dim Trans As OleDbTransaction

strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
objConn = New System.Data.OleDb.OleDbConnection(strConnString)
objConn.Open()

'*** Start Transaction ***'
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted)

Try

'*** Query 1 ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
"VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')"
objCmd = New System.Data.OleDb.OleDbCommand()
With objCmd
.Connection = objConn
.Transaction = Trans
.CommandType = CommandType.Text
.CommandText = strSQL
End With
objCmd.ExecuteNonQuery()

'*** Query 2 ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
"VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')"
objCmd = New System.Data.OleDb.OleDbCommand()
With objCmd
.Connection = objConn
.Transaction = Trans
.CommandType = CommandType.Text
.CommandText = strSQL
End With
objCmd.ExecuteNonQuery()

Trans.Commit() '*** Commit Transaction ***'
Me.lblText.Text = "Record is commit"

Catch ex As Exception
Trans.Rollback() '*** RollBack Transaction ***'

Me.lblText.Text = "Record is rollback ("& ex.Message &")"
End Try

objCmd = Nothing
objConn.Close()
objConn = Nothing


คำอธิบาย
จากตัวอย่างโปรแกรมจะทำการ Insert ข้อมูลโดยใช้ Try ครอบคำสั่ง แต่ถ้ามีการเกิด Error โปรแกรมจะทำงานในส่วนของ Catch ex As Exception , catch (Exception ex) ทันที






   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-31 12:22:25 / 2009-06-02 16:33:46
  Download : No files
 Sponsored Links / Related

 
ASP.NET Hello World
Rating :

 
ASP.NET ASP Style
Rating :

 
ASP.NET Code-Behind Style
Rating :

 
ASP.NET Msgbox
Rating :

 
ASP.NET Delegate
Rating :

 
ASP.NET Imports/Using
Rating :

 
ASP.NET ViewState
Rating :

 
ASP.NET Response,Request
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่