Saving Records Problem

dg_pp

Member
Joined
Oct 24, 2012
Messages
6
Programming Experience
1-3
Hi,

I have a ListBox in winform which displays records on the textbox in the listbox click event. but when saving the records it will save only the last edited record. ie a list box with usernames populated from database. when click on each username it will display the userdetails like firstname,lastname etc on the corresponding textboxes. I want to edit all the userdetails in one shot and finally to update the records on update button click event. How can I do that?
 
Hi,

Without writing the full code for you, it's difficult to say what changes you need to make to what you already have. A couple of questions to start with are:-

1) How are you accessing your database for read and write processes?
2) How are you populating your listbox - is this just a string or is this a username class that you have defined elsewhere?

It may be best to post what you already have so that we can see better what you need.

Cheers,

Ian
 
Hi,

I have posted my code. I want to save the records in one shot after updating all the user details.
Imports System.Configuration

Public Class frmUsers

    Private Enum Modesection
        READ_MODE = 0
        APPEND_MODE = 1
        UPDATE_MODE = 2
    End Enum
    Dim app_Mode As Modesection
    Dim errorLog As New ErrorLog
    Dim LoadedStatus As Boolean = False
    Dim blnChange As Boolean = False

    
    Private Sub LoadUsers()
        Try
            Dim objDA As New PS_DA.PSDA(ConfigurationSettings.AppSettings("Server1").ToString)
            Dim dtTableUsers As DataTable
            dtTableUsers = objDA.getUsers

            lstUsers.DataSource = dtTableUsers
            lstUsers.DisplayMember = "UserName"
            lstUsers.ValueMember = "UserID"
            lstUsers.SelectedIndex = 0
        Catch ex As Exception
            errorLog.WriteErrorLog("frmSUsers: LoadUsers ", ex.Message)

        End Try
       

    End Sub



    Private Sub frmUsers_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            LoadUsers()
            app_Mode = Modesection.READ_MODE
            LoadedStatus = True

        Catch ex As Exception
            errorLog.WriteErrorLog("frmUsers: frmUsers_Load ", ex.Message)
        End Try
        
    End Sub

    Private Sub lstUsers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstUsers.Click
        Try
            If lstUsers.SelectedValue = 0 Then
                app_Mode = Modesection.APPEND_MODE
                RefreshControls()
            Else
                app_Mode = Modesection.UPDATE_MODE
                LoadUserDetails(lstUsers.SelectedValue)

            End If

        Catch ex As Exception
            errorLog.WriteErrorLog("frmUsers: lstUsers_Click ", ex.Message)

        End Try
       
    End Sub

     Private Sub lstUsers_SelectedIndexChanged(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  lstUsers.SelectedIndexChanged

        If LoadedStatus Then

            Dim strName(4) As String

            strName(0) = txtFirstName.Text
            strName(1) = txtFamilyName.Text
            strName(2) = txtMobile1.Text
            strName(3) = txtMobile2.Text




        End If
    End Sub

    Public Structure Users
        Public FirstName As String
        Public FamilyName As String
        Public Mobile1 As String
    End Structure


    Private Sub RefreshControls()
        Try
            txtFamilyName.Text = ""
            txtFirstName.Text = ""
            txtMobile1.Text = ""
            txtMobile2.Text = ""
        Catch ex As Exception
            errorLog.WriteErrorLog("frmUsers: RefreshControls ", ex.Message)
        End Try
       

    End Sub
    Private Sub LoadUserDetails(ByVal userId As Integer)
        Dim objDA As New PS_DA.PSDA(ConfigurationSettings.AppSettings("Server1").ToString)
        Dim dtTableUsers As DataTable
        Try
            dtTableUsers = objDA.getUserDetails(userId)
            If IsNothing(dtTableUsers) = False Then
                txtFirstName.Text = dtTableUsers.Rows(0)("FirstName")
                txtFamilyName.Text = dtTableUsers.Rows(0)("FamilyName")
                txtMobile1.Text = IIf(IsDBNull(dtTableUsers.Rows(0)("Mobile1")), "", dtTableUsers.Rows(0)("Mobile1"))
                txtMobile2.Text = IIf(IsDBNull(dtTableUsers.Rows(0)("Mobile2")), "", dtTableUsers.Rows(0)("Mobile2"))

                chkSendMobile1.Checked = Convert.ToBoolean(dtTableUsers.Rows(0)("send_to_mobile1"))


            End If
        Catch ex As Exception
            errorLog.WriteErrorLog("frmUsers: LoadUserDetails ", ex.Message)
        End Try
        
    End Sub

     Private Sub lstUsers_SelectedValueChanged(ByVal sender As Object, ByVal  e As System.EventArgs) Handles lstUsers.SelectedValueChanged



    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If txtFirstName.Text.Trim = "" Then
            infomsg = New popupmsg("First Name cannot be blank", 0, 0)

            infomsg.Show()
            txtFirstName.Focus()
            Exit Sub
        ElseIf txtFamilyName.Text.Trim = "" Then
            infomsg = New popupmsg("Family Name cannot be blank", 0, 0)

            infomsg.Show()
            txtFamilyName.Focus()
            Exit Sub
        ElseIf chkSendMobile1.Checked = True And txtMobile1.Text.Trim = "" Then
            infomsg = New popupmsg("Mobile 1 cannot be blank", 0, 0)

            infomsg.Show()
            txtMobile1.Focus()
            Exit Sub
        ElseIf chkSendmobile2.Checked = True And txtMobile2.Text.Trim = "" Then
            infomsg = New popupmsg("Mobile 2 cannot be blank", 0, 0)

            infomsg.Show()
            txtMobile2.Focus()
            Exit Sub
        End If
        If txtMobile1.Text.Trim <> "" And IsNumeric(txtMobile1.Text) = False Then
            infomsg = New popupmsg("Mobile 1 cannot be a non numeric", 0, 0)

            infomsg.Show()
            txtMobile1.Focus()
            Exit Sub
        End If

        Try



            Dim objDA As New PS_DA.PSDA(ConfigurationSettings.AppSettings("Server1").ToString)
            If app_Mode = Modesection.APPEND_MODE Then
                 If objDA.SaveNewSMSUser(txtFirstName.Text.Replace("'", "''"),  txtFamilyName.Text.Replace("'", "''"), txtMobile1.Text.Replace("'",  "''"), chkSendMobile1.Checked) Then
                    infomsg = New popupmsg("Successfully saved", 0, 0)

                    infomsg.Show()
                    Me.Close()
                End If
            ElseIf app_Mode = Modesection.UPDATE_MODE Then
                 If objDA.UpdateSMSUser(txtFirstName.Text.Replace("'", "''"),  txtFamilyName.Text.Replace("'", "''"), txtMobile1.Text,  chkSendMobile1.Checked, lstUsers.SelectedValue) Then
                    infomsg = New popupmsg("Successfully saved", 0, 0)

                    infomsg.Show()
                    Me.Close()
                End If
            End If

        Catch ex As Exception
            errorLog.WriteErrorLog("frmUsers : btnSave_Click ", ex.Message)

        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub


End Class
 
Last edited by a moderator:
You don't need any code to display the data for editing. Just use a data adapter populate your DataTable and bind it to the ListBox and the TextBoxes. When the user selects a record in the ListBox its fields will be displayed in the ListBox automatically. When you're done use the same data adapter to save the changes from the DataTable back to the database. Generally things like this don't work because people over-complicate them.
Private adapter As SqlDataAdapter
Private table As DataTable

Private Sub Load()
    adapter.Fill(table)

    With ListBox1
        .DisplayMember = "Name"
        .ValueMember = "ID"
        .DataSource = table
    End With

    TextBox1.DataBindings.Add("Text", table, "Description")
End Sub

Private Sub Save()
    adapter.Update(table)
End Sub
There are a few details to flesh out there but that's basically how it works.
 
Hi,


I have changed the code to store user details into an array. but this is not working. when I select second user first user details edited disappears. I want to store the updated user details into an array and finally save the details. please Help.


Public Structure Users
Public FirstName As String
Public FamilyName As String
Public Mobile1 As String
End Structure



Private Sub lstUsers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsers.Click
Try
If lstUsers.SelectedValue = 0 Then
app_Mode = Modesection.CREATE
RefreshControls()
Else
app_Mode = Modesection.UPDATE
LoadUserDetails(lstUsers.SelectedValue)

Dim Entry As New Users()
Dim intIndex As Integer
Dim i As Integer
intIndex = CountUserDetails(lstUsers.SelectedValue)
For i = 0 To intIndex - 1

Entry.FirstName = txtFirstName.Text
Entry.FamilyName = txtFamilyName.Text
Entry.Mobile1 = txtMobile.Text
arr.Add(Entry)

Next
End If

Catch ex As Exception
Throw ex
End Try
End Sub
 
I cannot recommend strongly enough that that you abandon that code altogether and use data-binding. If you're determined to go that way though, you should be handling the SelectedIndexChanged event, not the Click event.

By the way, your Try...Catch block is very bad. If you're not going to do anything other than rethrow the exception in the Catch block then it's pointless and yours is worse than pointless because you're also removing the entire stack trace. If you do want to rethrow an exception, which you should only do after processing it in some way, then you just use the Throw keyword alone, without specifying the exception. What exactly are you trying to achieve with that anyway? The app is still going to crash if the rethrown exception is not caught somewhere else and you're not logging the issue in any way so what use is it?
 
Hi,

The code is not working in ListBox click event and selectedindexchanged event. I can get the modified value only in the textbox textchanged event. i want to add the modified records for each user to array and save the records. pls Help.
 
Last edited:
You can get the modified values any time you like. They are sitting there in the TextBoxes, waiting for you to get them. When the user selects another record the SelectedIndexChanged event is raised. At that point, you can get the data from the TextBoxes and update the previously displayed record, then display the currently selected record. That means that you will need to store a reference to the previously selected record, or perhaps just its index, so that you can access it once it's deselected. When a record is selected you store its index in a variable. When another record is selected, you use that variable to access the previous record, then store the index of the new record.

Of course, you could just use data-binding. That way none of this messing about would be required.
 
Back
Top