keith aranjuez

keith aranjuez

  • NA
  • 7
  • 1.4k

refactor code

Dec 29 2012 10:58 AM
hey guys..noob code here. how can i possibly refactor this long code? o_o

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        If btnNew.Text = "New" Then
            btnNew.Text = "Save"
            enable()
        ElseIf btnNew.Text = "Save" Then
            If txtDiagnosis.Text = String.Empty Then
                MessageBox.Show("Error encountered! No diagnosis.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            ElseIf pID = String.Empty Then
                MessageBox.Show("Error encountered! No patient information.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            Else
                If MessageBox.Show("Save information?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Using dbconn As New SqlConnection(conn)
                            dbconn.Open()
                            Dim dbadd As New SqlCommand("insert into tblcheckups(checkdate, patientid, employeeid, diagnosis, comments, prescription) values(@checkdate, @patientid, @empID, @diagnosis, @comments, @prescription)")
                            With dbadd
                                .Parameters.AddWithValue("@checkdate", Now.ToShortDateString.Trim)
                                .Parameters.AddWithValue("@patientid", pID.Trim)
                                .Parameters.AddWithValue("@empID", emp.Trim)
                                .Parameters.AddWithValue("@diagnosis", txtDiagnosis.Text.Trim.Replace("'", "''"))
                                .Parameters.AddWithValue("@comments", txtComment.Text.Trim.Replace("'", "''"))
                                .Parameters.AddWithValue("@prescription", txtPrescribe.Text.Trim.Replace("'", "''"))
                                .Connection = dbconn
                                .ExecuteNonQuery()
                            End With
                            dbconn.Close()
                        End Using
                        MessageBox.Show("Information saved!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        disable()
                        getNames()
                        clear()
                        btnNew.Text = "New"
                    Catch ex As Exception
                        Beep()
                        MessageBox.Show(String.Format("Error encountered! {0}. Source: {1}", ex.Message, ex.Source), "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    End Try
                End If
            End If
        End If
    End Sub