8 Replies - 2724 Views - Last Post: 18 March 2013 - 01:33 AM Rate Topic: -----

#1 egeboy099  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-September 12

Datagridview wont work.

Posted 17 March 2013 - 03:36 AM

I have a problem with my database system.

My system is a Loan system with monthlypayment.

my problem here is the command button schedule.

after putting the details like first name id# , lastname etc. i will input the loan amount followed by interest and the date. and after entering those i will click the calculate button and it works. but the problem is the schedule
i created a datagridview . after calculating the amount i will click the schedule button but nothing works the datagridview wont loan anything like the date that the customer would pay. only blank

so here's my code :

Imports System.Data.OleDb

Public Class Form1
    Public connstring As String = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = D:\Parang database\databasefinal.accdb;persist security info = false"
    Public conn As New OleDbConnection

    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn.ConnectionString = connstring

        If conn.State = ConnectionState.Closed Then
            conn.Open()
            MsgBox("Open")
        Else
            MsgBox("Close")

        End If
    End Sub

    Private Sub btncommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncommit.Click

        Try

            Dim SqlQuery As String = "INSERT INTO clientinfo (ID,Lastname,Firstname,Mi,Address) VALUES ('" & txtid.Text & "','" & txtlname.Text & "','" & txtfname.Text & "','" & txtmi.Text & "','" & txtaddress.Text & "')"
            Dim SqlCommand As New OleDbCommand

            With SqlCommand
                .CommandText = SqlQuery
                .Connection = conn
                .ExecuteNonQuery()
            End With
            MsgBox("One record Successfully added.")

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        btnaddnew.Enabled = True
    End Sub


    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        Try

            Dim SqlQuery As String = "UPDATE tblinfo SET ID = '" & txtid.Text & "', Lastname = '" & txtlname.Text & "', Firstname = '" & txtfname.Text & "', Mi = '" & txtmi.Text & "', Address = '" & txtaddress.Text & "' WHERE ID = " & txtid.Text & ";"
            Dim SqlCommand As New OleDbCommand

            With SqlCommand
                .CommandText = SqlQuery
                .Connection = conn
                .ExecuteNonQuery()
            End With
            MsgBox("Update Success.")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub InterestPayment()
        Dim intAmount As Double = 0
        Dim principal As Double = Me.txtPrincipal.Text
        Dim intInterestRate As Double = Me.txtInterest.Text
        Dim principalPayment As Double = 0
        intAmount = (principal * intInterestRate * 0.01) / 12
        principalPayment = principal / CDbl(Me.txtPeriod.Text)
        txtMonthlyInterestPayment.Text = FormatCurrency(intAmount, 2)

        Me.txtMonthlyPrincipalPayment.Text = principalPayment

        Dim intPay As Double = CDbl(Me.txtMonthlyInterestPayment.Text)
        Dim principalpay As Double = Me.txtMonthlyPrincipalPayment.Text
        Dim TotalPayment As Double = intPay + principalpay

        Me.txtTotalPayment.Text = FormatCurrency(TotalPayment, 2)
    End Sub

    Private Sub btnCalculation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculation.Click
        If Not IsNumeric(Me.txtPrincipal.Text) Then
            MsgBox("Invalid principal value.", MsgBoxStyle.Exclamation, "Invalid Value.")
            Return
        End If

        If Not IsNumeric(Me.txtPeriod.Text) Then
            MsgBox("Invalid period value.", MsgBoxStyle.Exclamation, "Invalid Period.")
            Return
        End If

        If Not IsNumeric(Me.txtInterest.Text) Then
            MsgBox("Invalid interest rate value.", MsgBoxStyle.Exclamation, "Invalid Interest Rate.")
            Return
        End If

        If Not IsNumeric(Me.txtid.Text) Then
            MsgBox("Invalid id value.", MsgBoxStyle.Exclamation, "Please user numbers only.")
            Return
        End If
        InterestPayment()
    End Sub

    Private Sub btnSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSchedule.Click
        G.GenerateScheule()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        conn.Close()
    End Sub

    Private Sub btnaddnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddnew.Click
        btncommit.Enabled = True
        btnaddnew.Enabled = False
        txtaddress.Clear()
        txtfname.Clear()
        txtid.Clear()
        txtInterest.Clear()
        txtlname.Clear()
        txtmi.Clear()
        txtPeriod.Clear()
        txtPrincipal().Clear()
    End Sub

    Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
        conn.Close()
        Me.Close()
    End Sub

    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        txtaddress.Clear()
        txtfname.Clear()
        txtid.Clear()
        txtInterest.Clear()
        txtlname.Clear()
        txtmi.Clear()
        txtPeriod.Clear()
        txtPrincipal().Clear()
    End Sub

    Dim G As New Schedule

    Public Class Schedule

        Dim dPrincipal As Double = 0
        Dim dIntInterestRate As Double = 0
        Dim iPeriod As Integer
        Dim dtValueDate As Date
        Dim sType As String = ""

        Public WriteOnly Property Principal() As Double
            Set(ByVal value As Double)
                dPrincipal = value
            End Set
        End Property

        Public WriteOnly Property InterestRate() As Double
            Set(ByVal value As Double)
                dIntInterestRate = value
            End Set
        End Property

        Public WriteOnly Property Period() As Double
            Set(ByVal value As Double)
                iPeriod = value
            End Set
        End Property

        Public WriteOnly Property ValueDate() As Date
            Set(ByVal value As Date)
                dtValueDate = value
            End Set
        End Property

        Public WriteOnly Property LoanType() As String
            Set(ByVal value As String)
                sType = value
            End Set
        End Property

        Public Sub GenerateScheule()
            Dim monthlyInt As Double = 0
            Dim monthlyPrincipal As Double = 0
            Dim dtDate As Date
            Dim total As Double

            monthlyInt = (dPrincipal * dIntInterestRate * 0.01) / 12
            monthlyPrincipal = dPrincipal / iPeriod

            For i As Integer = 1 To iPeriod
                dtDate = dtValueDate.AddMonths(i)

                total = monthlyPrincipal + monthlyInt

                Form1.dgvLoanSchedule.Rows.Add(i, Format(dtDate, "dd/MMM/yyyy"), FormatCurrency(monthlyInt, 2), FormatCurrency(monthlyPrincipal, 2), FormatCurrency(total, 2))

                dPrincipal -= monthlyPrincipal
                monthlyPrincipal = dPrincipal / iPeriod
                monthlyInt = (dPrincipal * dIntInterestRate * 0.01) / 12

            Next
        End Sub
    End Class
End Class



heres the problem : is there something wrong with this?

Private Sub btnSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSchedule.Click
        G.GenerateScheule()
    End Sub



but when i created a new form and clicking the schedule button my program works perfectly. but when i decided to transfer it to form1 inorder to have a single form the schedule button wont work anymore.

Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview wont work.

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Datagridview wont work.

Posted 17 March 2013 - 04:03 AM

Do you receive any errors or warnings? If so, what are they?

The first thing is to put a MessageBox in the click event to check whether it runs at all. Then read the debugging tutorial linked in my signature so that you can step through your GenerateScheule() method.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Datagridview wont work.

Posted 17 March 2013 - 05:11 AM

One thing I see is that you are referencing Form1 in your GenerateSchedule method of the class. Now with the Default Instance that VB.Net allows you to do, this wouldn't appear to be a problem when the code reside on Form2 but once you move it to Form1. You will receive an error

Something along the lines of Form1 cannot refer to itself, use Me instead.

If you remove the word Form1 from the beginning of the line on line 190 your code will probably begin working as it did on form2.

The problem is, your class now REQUIRES that you have a datagridview named dgvLoanSchedule on the current form for it to work. This is bad design practice.
Was This Post Helpful? 0
  • +
  • -

#4 egeboy099  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-September 12

Re: Datagridview wont work.

Posted 17 March 2013 - 07:30 AM

View Postandrewsw, on 17 March 2013 - 04:03 AM, said:

Do you receive any errors or warnings? If so, what are they?

The first thing is to put a MessageBox in the click event to check whether it runs at all. Then read the debugging tutorial linked in my signature so that you can step through your GenerateScheule() method.


i didn't receive any warning.
not even once my program works. but just what i said the schedule button doesn't work.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Datagridview wont work.

Posted 17 March 2013 - 08:07 AM

Did you use the MessageBox like I suggested, to confirm whether the code runs at all?

If the code in btnSchedule_Click doesn't run at all then I would delete it, delete any left-over event details from the Events section of the Properties window, and double-click the button on the form to start a new event.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 252
  • View blog
  • Posts: 819
  • Joined: 01-February 13

Re: Datagridview wont work.

Posted 17 March 2013 - 09:52 AM

Hello egeboy099,
I would recommend putting in the MessageBox as andrewsw has told you about to see if the event is working. Also i would turn option strict on by adding this line as the very first line of code in your program above the (Imports System.Data.OleDb) line.
Option Strict On


It will catch all the errors like this one where you are trying to assign a string to a double.
        Dim principal As Double = Me.txtPrincipal.Text


After adding that code you will see a blue line with a red spot on the end of it under each of these errors. Put your mouse over the red spot and it will pop up a little red box that you can click on. Then it will tell you how to fix the errors. Such as changing the above line to
        Dim principal As Double = CDbl(Me.txtPrincipal.Text)


:bigsmile:
Was This Post Helpful? 0
  • +
  • -

#7 egeboy099  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-September 12

Re: Datagridview wont work.

Posted 17 March 2013 - 05:03 PM

View PostIronRazer, on 17 March 2013 - 09:52 AM, said:

Hello egeboy099,
I would recommend putting in the MessageBox as andrewsw has told you about to see if the event is working. Also i would turn option strict on by adding this line as the very first line of code in your program above the (Imports System.Data.OleDb) line.
Option Strict On


It will catch all the errors like this one where you are trying to assign a string to a double.
        Dim principal As Double = Me.txtPrincipal.Text


After adding that code you will see a blue line with a red spot on the end of it under each of these errors. Put your mouse over the red spot and it will pop up a little red box that you can click on. Then it will tell you how to fix the errors. Such as changing the above line to
        Dim principal As Double = CDbl(Me.txtPrincipal.Text)


:bigsmile:/>


ok ill try this will be back later.
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 252
  • View blog
  • Posts: 819
  • Joined: 01-February 13

Re: Datagridview wont work.

Posted 17 March 2013 - 06:39 PM

Hey egeboy099,
I just noticed this. You have (iPeriod) declared as an integer and in the Period property you are using double.
        Dim iPeriod As Integer


        Public WriteOnly Property Period() As Double
            Set(ByVal value As Double)
                iPeriod = value
            End Set
        End Property


You may want to set it as integer
        Public WriteOnly Property Period() As Integer
            Set(ByVal value As Integer)
                iPeriod = value
            End Set
        End Property


Also as CharlieMay has stated in his post this line should be changed from this
                Form1.dgvLoanSchedule.Rows.Add(i, Format(dtDate, "dd/MMM/yyyy"), FormatCurrency(monthlyInt, 2), FormatCurrency(monthlyPrincipal, 2), FormatCurrency(total, 2))


to this. Without Form1 on the begining of it.
                dgvLoanSchedule.Rows.Add(i, Format(dtDate, "dd/MMM/yyyy"), FormatCurrency(monthlyInt, 2), FormatCurrency(monthlyPrincipal, 2), FormatCurrency(total, 2))


:bigsmile:/>

This post has been edited by IronRazer: 17 March 2013 - 06:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 egeboy099  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-September 12

Re: Datagridview wont work.

Posted 18 March 2013 - 01:33 AM

View PostIronRazer, on 17 March 2013 - 06:39 PM, said:

Hey egeboy099,
I just noticed this. You have (iPeriod) declared as an integer and in the Period property you are using double.
        Dim iPeriod As Integer


        Public WriteOnly Property Period() As Double
            Set(ByVal value As Double)
                iPeriod = value
            End Set
        End Property


You may want to set it as integer
        Public WriteOnly Property Period() As Integer
            Set(ByVal value As Integer)
                iPeriod = value
            End Set
        End Property


Also as CharlieMay has stated in his post this line should be changed from this
                Form1.dgvLoanSchedule.Rows.Add(i, Format(dtDate, "dd/MMM/yyyy"), FormatCurrency(monthlyInt, 2), FormatCurrency(monthlyPrincipal, 2), FormatCurrency(total, 2))


to this. Without Form1 on the begining of it.
                dgvLoanSchedule.Rows.Add(i, Format(dtDate, "dd/MMM/yyyy"), FormatCurrency(monthlyInt, 2), FormatCurrency(monthlyPrincipal, 2), FormatCurrency(total, 2))


:bigsmile:/>/>


i already corredted the integer to double

but the Form1.dgvLoanSchedule whenever i remove the Form1. and leave only dgvLoanSchedule the program runs an error.

error : Error 1 Reference to a non-shared member requires an object reference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1