Tuesday, 22 January 2013

Basic error handling in vb.NET for inserting data into database

Try
    conn.Open()

    Dim cmd As New OracleCommand
    cmd.Connection = conn
    cmd.CommandText = "select dname from dept where deptno = " + TextBox1.Text
    cmd.CommandType = CommandType.Text

    If dr.Read() Then
        Label1.Text = dr.Item("dname") ' or use dr.Item(0)
    End If
Catch ex As Exception ' catches any error
    MessageBox.Show(ex.Message.ToString())
Finally
    ' In a real application, put cleanup code here.
End Try




Catch ex As OracleException ' catches only Oracle errors
    Select Case ex.Number
        Case 1
            MessageBox.Show("Error attempting to insert duplicate data.")
        Case 12545
            MessageBox.Show("The database is unavailable.")
        Case Else
            MessageBox.Show("Database error: " + ex.Message.ToString())
    End Select







Catch ex As Exception ' catches any error
    MessageBox.Show(ex.Message.ToString())

catch (Exception ex) // catches any error not previously caught
{
    MessageBox.Show(ex.Message.ToString());
}

No comments:

Post a Comment