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. Can I have some sample code for that? Do I need class or struct to repopulate values in selected index changed event?

I have posted my code. please do the needful.

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

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