Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have done a class and a function for populating database with objects of class
and also a function for deleting objects. Now a need a solution for modifying objects. that means returning object from database modifying it and save it modified. That's why a need a function that fill object attributes with data from database. Thank you !
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class Salariat
    Dim m_cnp As String
    Dim m_nume As String
    Dim m_prenume As String
    Dim m_adresa As String
    Dim m_telefon As String
    Dim m_calificare As String
    Dim m_contBancar As String

    Public Property cnp() As String
        Get
            Return m_cnp
        End Get
        Set(ByVal value As String)
            If Len(value) = 13 Then
                m_cnp = value
            Else
                Throw New Exception("CNP incorect!")
            End If
        End Set
    End Property
    Public Property nume() As String
        Get
            Return m_nume
        End Get
        Set(ByVal value As String)
            If value <> "" Then
                m_nume = value
            Else
                Throw New Exception("Completati Numele!")
            End If

        End Set
    End Property
    Public Property prenume() As String
        Get
            Return m_prenume
        End Get
        Set(ByVal value As String)
            If value <> "" Then
                m_prenume = value
            Else
                Throw New Exception("Completati Prenumele!")
            End If

        End Set
    End Property
    Public Property adresa() As String
        Get
            Return m_adresa
        End Get
        Set(ByVal value As String)
            m_adresa = value
        End Set
    End Property
    Public Property telefon() As String
        Get
            Return m_telefon
        End Get
        Set(ByVal value As String)
            m_telefon = value
        End Set
    End Property
    Public Property calificare() As String
        Get
            Return m_calificare
        End Get
        Set(ByVal value As String)
            m_calificare = value
        End Set
    End Property
    Public Property contBancar() As String
        Get
            Return m_contBancar
        End Get
        Set(ByVal value As String)
            If Len(value) = 24 Then
                m_contBancar = value
            Else
                Throw New Exception("Cont bancar incorect!")
            End If
        End Set
    End Property
    Public Sub Adauga()
        Dim cmd As New SqlCommand
        Dim sCnnection As String = "server=(local);Integrated Security=SSPI;database=crossal"
        Dim cnn As New SqlConnection With {.ConnectionString = sCnnection}
        cnn.Open()
        cmd.Connection = cnn
        cmd.CommandText = "INSERT INTO salariati (CNP, Nume, Prenume, Adresa, Telefon, Calificare, Contbancar) VALUES (@CNP, @Nume, @Prenume, @Adresa, @Telefon, @Calificare, @Contbancar)"
        cmd.Parameters.AddWithValue("@CNP", Me.cnp)
        cmd.Parameters.AddWithValue("@Nume", Me.nume)
        cmd.Parameters.AddWithValue("@Prenume", Me.prenume)
        cmd.Parameters.AddWithValue("@Adresa", Me.adresa)
        cmd.Parameters.AddWithValue("@Telefon", Me.telefon)
        cmd.Parameters.AddWithValue("@Calificare", Me.calificare)
        cmd.Parameters.AddWithValue("@ContBancar", Me.contBancar)
        cmd.ExecuteNonQuery()
    End Sub
    Public Sub Sterge(ByVal pcnp)
        Dim qq
        qq = MsgBox("Doriti sa stergeti salariatul cu CNP-ul " & pcnp & " ?", MsgBoxStyle.YesNo)
        If qq = vbYes Then
            Dim cmd As New SqlCommand
            Dim sCnnection As String = "server=(local);Integrated Security=SSPI;database=crossal"
            Dim cnn As New SqlConnection With {.ConnectionString = sCnnection}
            cnn.Open()
            cmd.Connection = cnn
            cmd.CommandText = " delete from salariati where cnp='" + pcnp + "'"
            cmd.ExecuteNonQuery()
        Else
        End If
    End Sub
    Public Sub Incarca(ByVal pcnp)
        Dim ccnp As New SqlCommand
        Dim sConnection As String = "server=(local);Integrated Security=SSPI;database=Salarizare"
        Dim c As New SqlConnection(sConnection)
        c.Open()
        ccnp.Connection = c
        ccnp.CommandText = " select * from salariati where cnp='" + pcnp + "'"

    End Sub
End Class
Posted
Updated 12-Apr-11 1:07am
v2

1 solution

To do this you will need 2 methods. One that returns the correct object, and another to save the object once it has been modified like this:


VB
Public Function GetSalariat(ByVal pcnp) As Salariat

        Dim sConnection As String = "server=(local);Integrated Security=SSPI;database=Salarizare"
        Dim c As New SqlConnection(sConnection)
        Try
            Dim ccnp As New SqlCommand
            c.Open()
            ccnp.Connection = c
            'don't use * in SQL Query - better to list all fields you need by name
            ccnp.CommandText = "SELECT cnp, nume, prenume etc FROM salariati WHERE cnp = @cnp"
            ccnp.Parameters.AddWithValue("@cnp", pcnp)
            Dim drSalariats As SqlDataReader = ccnp.ExecuteReader()
            Dim newSalariat As New Salariat()
            While drSalariats.Read()
                newSalariat.cnp = drSalariats.GetInt32(0)
                newSalariat.nume = drSalariats.GetString(1)
                newSalariat.prenume = drSalariats.GetString(2)
                'add rest of members here
            End While
            Return newSalariat
        Catch ex As SqlException
            Throw ex 'should be handled better
        Finally
            If c.State = ConnectionState.Open Then
                c.Close()
            End If
        End Try

    End Function


and to save:

VB
Public Shared Function SaveSalariat(ByVal salariatToSave As Salariat) As Integer

        Dim sCnnection As String = "server=(local);Integrated Security=SSPI;database=crossal"
        Dim cnn As New SqlConnection With {.ConnectionString = sCnnection}
        Try
            Dim cmd As New SqlCommand
            cnn.Open()
            cmd.Connection = cnn
            cmd.CommandText = "UPDATE tableName Set Nume = @Nume, PreNume = @Prenume, etc WHERE cnp = @Cnp"
            cmd.Parameters.AddWithValue("@Nume", salariatToSave.nume)
            cmd.Parameters.AddWithValue("@PreNume", salariatToSave.prenume)
            'add rest of parameters here
            Return cmd.ExecuteNonQuery()
        Catch ex As SqlException
            Throw ex 'should be handled better
        Finally
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
        End Try

    End Function


Hope this helps
 
Share this answer
 
v2
Comments
Daniel Crosman 12-Apr-11 13:57pm    
Thank you again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900