8 Replies - 989 Views - Last Post: 16 February 2012 - 02:05 AM Rate Topic: -----

#1 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

How to make an Error Message with this

Posted 13 February 2012 - 11:13 PM

I was about to make an error message when there is no record both pat_brgyno and pat_famno in the database. I don't know how to make it. What my logic says is something like this:

mtb=maskedtextbox

if mtbbrgyno.text<>pat_brgy and mtbfamno.text<>pat_famno then
msgbox("No Record"
endif


I don't know how to execute it.

Here's my code to call the data. It's working but doesn't have error message yet.

Dim cnn2 As New OleDb.OleDbConnection("provider = Microsoft.jet.oledb.4.0; data source=..\prms_database.mdb")
        Dim acc As String
        Dim da2 As New OleDb.OleDbDataAdapter
        Dim objcommand As New OleDb.OleDbCommand
        Dim ds2 As New DataSet
        'Dim temp As String

        acc = "select * from prms_patient where pat_famno = '" & mtbFamNo.Text & "' and pat_brgyno = '" & mtbBrgyNo.Text & "'"
        
        da2 = New OleDb.OleDbDataAdapter(acc, cnn2)
            da2.Fill(ds2, "temp")
            cnn2.Close()
            If ds2.Tables("temp").Rows.Count > 0 Then
                tbFN.Text = ds2.Tables("temp").Rows(0).Item("pat_fn").ToString
                cbMI.SelectedItem = ds2.Tables("temp").Rows(0).Item("pat_mi").ToString
                tbSN.Text = ds2.Tables("temp").Rows(0).Item("pat_sn").ToString
                tbAdd.Text = ds2.Tables("temp").Rows(0).Item("pat_add").ToString
                If Trim(ds2.Tables("temp").Rows(0).Item("pat_gender").ToString) = "Male" Then
                    rbMale.Select()
                Else
                    rbFemale.Select()
                End If
                cbStat.SelectedItem = ds2.Tables("temp").Rows(0).Item("pat_stat").ToString
                mtbMobile.Text = ds2.Tables("temp").Rows(0).Item("pat_mobile").ToString
                mtbLand.Text = ds2.Tables("temp").Rows(0).Item("pat_landline").ToString
                pbox.ImageLocation = ds2.Tables("temp").Rows(0).Item("pat_pic").ToString


                btnEdit.Enabled = False
                btnDelete.Enabled = False
                btnSearch.Enabled = False
                btnLogoff.Enabled = False
                btnClear.Enabled = True
                btnSave.Enabled = True
                btnPrint.Enabled = True
                btnCancel.Show()
                btnEdit.Enabled = True
                btnDelete.Enabled = True
                btnAdd.Enabled = False
            End If
        'End If


Is This A Good Question/Topic? 0
  • +

Replies To: How to make an Error Message with this

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How to make an Error Message with this

Posted 13 February 2012 - 11:35 PM

If row are zero means there is no record. That means you have to put an else part in your outer if, where if condition ds2.Tables("temp").Rows.Count > 0 is false and give the error message there
Was This Post Helpful? 0
  • +
  • -

#3 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Re: How to make an Error Message with this

Posted 14 February 2012 - 12:31 AM

View Postsmohd, on 13 February 2012 - 11:35 PM, said:

If row are zero means there is no record. That means you have to put an else part in your outer if, where if condition ds2.Tables("temp").Rows.Count > 0 is false and give the error message there


Woah!! Many Thanks Again!!
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How to make an Error Message with this

Posted 14 February 2012 - 12:37 AM

Glad we could help :)
Was This Post Helpful? 0
  • +
  • -

#5 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Re: How to make an Error Message with this

Posted 14 February 2012 - 12:47 AM

View Postsmohd, on 14 February 2012 - 12:37 AM, said:

Glad we could help :)


Since you were here already, mind if I ask another question again?

Here it is :

Basically primary key should not have duplicate. In this case, I have 2 primary keys which should not be duplicated.
My primary keys are pat_brgyno and pat_famno. How am I going to make an error message not to allow my program to save if there are same pat_brgyno and pat_famno already in the database?

Private Sub btnSaveP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveP.Click
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter
        da = New OleDbDataAdapter("select * from prms_patient", con)
        da.Fill(dt)

        Dim newrow As DataRow = dt.NewRow

        'Codes for Adding and Editing Patient Record

        With newrow

            'Codes for Brgy No. and Family No.

            If mtbBrgyNo.Text = "  -" Then
                con.Close()
                Me.ep1.SetError(mtbBrgyNo, "Please enter Barangay number.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(Me.mtbBrgyNo, "")
                .Item(0) = mtbBrgyNo.Text
                con.Close()
            End If
            If mtbFamNo.Text = "  -" Then
                con.Close()
                Me.ep1.SetError(mtbFamNo, "Please enter Family number.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(Me.mtbFamNo, "")
                .Item(1) = mtbFamNo.Text
                con.Close()
            End If

            'Codes for Name

            .Item(12) = tbFN.Text + " " + cbMI.SelectedItem + " " + tbSN.Text

            If tbFN.Text = "" Or IsNumeric(tbFN.Text) Then
                con.Close()
                Me.ep1.SetError(tbSN, "Empty Fields, Numbers and Special Characters are not Allowed.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(tbSN, "")
                .Item(2) = tbFN.Text
                con.Close()
            End If
            If cbMI.SelectedItem = "" Then
                con.Close()
                Me.ep1.SetError(tbSN, "Please select your Middle Initial.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(tbSN, "")
                .Item(3) = cbMI.SelectedItem
                con.Close()
            End If
            con.Close()
            If tbSN.Text = "" Or IsNumeric(tbSN.Text) Then
                Me.ep1.SetError(tbSN, "Empty Fields, Numbers and Special Characters are not Allowed.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(tbSN, "")
                .Item(4) = tbSN.Text
                con.Close()
            End If

            'Codes for Address
            con.Close()
            If tbAdd.Text = "" Then
                Me.ep1.SetError(tbAdd, "Please enter your address.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(tbAdd, "")
                .Item(8) = tbAdd.Text
                con.Close()
            End If

            'Codes for Gender

            If rbFemale.Checked = False And rbMale.Checked = False Then
                con.Close()
                Me.ep1.SetError(lblGender, "Please select your gender.")
                Return

            Else
                con.Open()
                Me.ep1.SetError(lblGender, "")
                If rbMale.Checked Then
                    .Item(5) = rbMale.Text
                Else
                    .Item(5) = rbFemale.Text
                End If
                con.Close()
            End If

            'Codes for Civil Status

            If cbStat.SelectedItem = "" Then
                con.Close()
                Me.ep1.SetError(cbStat, "Please select your Civil Status.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(cbStat, "")
                If cbStat.SelectedIndex = 0 Then
                    .Item(6) = "Single"
                ElseIf cbStat.SelectedIndex = 1 Then
                    .Item(6) = "Married"
                Else
                    .Item(6) = "Widow"
                End If
                con.Close()
            End If

            'Codes for Birthday

            If dtpBday.Value >= Date.Now Then
                con.Close()
                Me.ep1.SetError(dtpBday, "Invalid Birthday.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(dtpBday, "")
                .Item(7) = dtpBday.Value
                con.Close()
            End If

            'Codes for Contact Numbers

            If mtbLand.Text = "(   )    -" Then
                con.Close()
                .Item(9) = ""
            Else
                con.Open()
                Me.ep1.SetError(mtbLand, "")
                .Item(9) = mtbLand.Text
                con.Close()
            End If
            If mtbMobile.Text = "+63 -" Then
                con.Close()
                Me.ep1.SetError(mtbMobile, "Please Enter your Mobile Number.")
                Return
            Else
                con.Open()
                Me.ep1.SetError(mtbMobile, "")
                .Item(10) = mtbMobile.Text
                con.Close()
            End If

            'Codes for Images

            .Item(11) = pbox.ImageLocation
        End With
        dt.Rows.Add(newrow)
        Dim cb As New OleDbCommandBuilder(da)
        da.Update(dt)
        con.Close()
        MessageBox.Show("Record Saved!", "Important Message", MessageBoxButtons.OK)
        clear()
    End Sub

Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How to make an Error Message with this

Posted 14 February 2012 - 12:57 AM

There are two ways to do it:

- First, check if there is a row where pat_brgyno and pat_famno equals to user input, if so give an error message and ask user to input another values. This can be done by first having a SELECT query, then check if it returns any rows, if no rows returned, then INSERT data.

- Another way is to try(although it is not a good one but does the trick), put your ExecuteNonQuery() in try block while INSERTing data, if there is key constraints it will throw exception and you will tell user in catch block that operation failed duplicate key.
Was This Post Helpful? 0
  • +
  • -

#7 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Re: How to make an Error Message with this

Posted 14 February 2012 - 01:47 AM

View Postsmohd, on 14 February 2012 - 12:57 AM, said:

There are two ways to do it:

- First, check if there is a row where pat_brgyno and pat_famno equals to user input, if so give an error message and ask user to input another values. This can be done by first having a SELECT query, then check if it returns any rows, if no rows returned, then INSERT data.

- Another way is to try(although it is not a good one but does the trick), put your ExecuteNonQuery() in try block while INSERTing data, if there is key constraints it will throw exception and you will tell user in catch block that operation failed duplicate key.


Where am I going to insert the codes?
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1539
  • View blog
  • Posts: 4,941
  • Joined: 25-September 09

Re: How to make an Error Message with this

Posted 14 February 2012 - 05:37 AM

Well, you're going to have to do it after you fill the dataset with your query results, otherwise, you won't be able to count the rows of the dataset, and it's going to have to be prior to adding a new row as that will only be allowed if the resulting row count is 0. Determine where these things are happening and work from there.

This post has been edited by CharlieMay: 14 February 2012 - 05:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Re: How to make an Error Message with this

Posted 16 February 2012 - 02:05 AM

This is what I am thinking right now but I don't know yet how to execute it.

if mtbPatNo.text already exist in column("pat_patno")then
msgbox("Patient Number already exist")
return

Else
.item("pat_patno")=mtbPatNo.text
'Till the last field.

dt.Rows.Add(newrow)
 Dim cb As New OleDbCommandBuilder(da)
        da.Update(dt)
        con.Close()
End If



Something like this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1