NEVER MIND. FIGURED IT OUT. THANKS!!!
Hello,
I'm stumped. I'm developing a page that lets participants in a program edit their info. Textboxes are filled with participant info on page load and three buttons on the bottom of the page let them edit their info: edit, update, and cancel. I'd really appreciate it if some helpful folks would look at my code below and help me figure out what I'm doing wrong.
This is the code behind file...
Imports System.Data.SqlClient
Public MustInherit Class ParticipantDetailsEdit
Inherits System.Web.UI.UserControl
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLocalAddress1 As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLocalAddress2 As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLocalCity As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLocalState As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLocalZip As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPermZip As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPermState As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPermCity As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPermAddress2 As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPermAddress1 As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPhone1 As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPhone2 As System.Web.UI.WebControls.TextBox
Protected WithEvents txtEmail As System.Web.UI.WebControls.TextBox
Protected WithEvents txtBestTime As System.Web.UI.WebControls.TextBox
Protected WithEvents txtFundraisingGoal As System.Web.UI.WebControls.TextBox
Protected WithEvents txtProfile As System.Web.UI.WebControls.TextBox
Protected WithEvents btnRemove As System.Web.UI.WebControls.Button
Protected WithEvents btnEdit As System.Web.UI.WebControls.Button
Protected WithEvents btnUpdate As System.Web.UI.WebControls.Button
Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
Protected WithEvents lblParticipantId As System.Web.UI.WebControls.Label
Protected WithEvents lblErrorMsg As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
LoadParticipantDetails()
SetEditMode(False)
End If
End Sub
Private Sub LoadParticipantDetails()
Dim participant As New ParticipantAdmin
Dim participantDetails As ParticipantDetails
Dim participantId As String = Convert.ToInt32(Context.Session("LaPazInternationa l_ParticipantID"))
participantDetails = participant.GetParticipantDetails(participantId)
lblParticipantId.Text = participantId
txtFirstName.Text = participantDetails.FirstName
txtLastName.Text = participantDetails.LastName
txtEmail.Text = participantDetails.Email
txtPhone1.Text = participantDetails.Phone1
txtPhone2.Text = participantDetails.Phone2
txtBestTime.Text = participantDetails.BestTime
txtLocalAddress1.Text = participantDetails.LocalAddress1
txtLocalAddress2.Text = participantDetails.LocalAddress2
txtLocalCity.Text = participantDetails.LocalCity
txtLocalState.Text = participantDetails.LocalState
txtLocalZip.Text = participantDetails.LocalZip
txtPermAddress1.Text = participantDetails.PermAddress1
txtPermAddress2.Text = participantDetails.PermAddress2
txtPermCity.Text = participantDetails.PermCity
txtPermState.Text = participantDetails.PermState
txtPermZip.Text = participantDetails.PermZip
txtFundraisingGoal.Text = participantDetails.FundraisingGoal
txtProfile.Text = participantDetails.Profile
btnEdit.Enabled = True
btnUpdate.Enabled = False
btnCancel.Enabled = False
End Sub
Private Sub SetEditMode(ByVal value As Boolean)
txtFirstName.Enabled = value
txtLastName.Enabled = value
txtEmail.Enabled = value
txtPhone1.Enabled = value
txtPhone2.Enabled = value
txtBestTime.Enabled = value
txtLocalAddress1.Enabled = value
txtLocalAddress2.Enabled = value
txtLocalCity.Enabled = value
txtLocalState.Enabled = value
txtLocalZip.Enabled = value
txtPermAddress1.Enabled = value
txtPermAddress2.Enabled = value
txtPermCity.Enabled = value
txtPermState.Enabled = value
txtPermZip.Enabled = value
txtFundraisingGoal.Enabled = value
txtProfile.Enabled = value
btnEdit.Enabled = Not value
btnUpdate.Enabled = value
btnCancel.Enabled = value
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
SetEditMode(True)
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
SetEditMode(False)
LoadParticipantDetails()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim participantDetails As New ParticipantDetails
participantDetails.ParticipantId = lblParticipantId.Text
participantDetails.FirstName = txtFirstName.Text
participantDetails.LastName = txtLastName.Text
participantDetails.Email = txtEmail.Text
participantDetails.Phone1 = txtPhone1.Text
participantDetails.Phone2 = txtPhone2.Text
participantDetails.BestTime = txtBestTime.Text
participantDetails.LocalAddress1 = txtLocalAddress1.Text
participantDetails.LocalAddress2 = txtLocalAddress2.Text
participantDetails.LocalCity = txtLocalCity.Text
participantDetails.LocalState = txtLocalState.Text
participantDetails.LocalZip = txtLocalZip.Text
participantDetails.PermAddress1 = txtPermAddress1.Text
participantDetails.PermAddress2 = txtPermAddress2.Text
participantDetails.PermCity = txtPermCity.Text
participantDetails.PermState = txtPermState.Text
participantDetails.PermZip = txtPermZip.Text
participantDetails.FundraisingGoal = txtFundraisingGoal.Text
participantDetails.Profile = txtProfile.Text
Try
Dim participant As New ParticipantAdmin
participant.UpdateParticipantDetails(participantDe tails)
Catch
lblErrorMsg.Text = "Could not make the update. Please contact the site administrator"
End Try
SetEditMode(False)
LoadParticipantDetails()
End Sub
End Class
This is the
vb file...
Imports System.Data.SqlClient
Public Class ParticipantDetails
Public ParticipantId As String
Public FirstName As String
Public LastName As String
Public Email As String
Public Phone1 As String
Public Phone2 As String
Public BestTime As String
Public LocalAddress1 As String
Public LocalAddress2 As String
Public LocalCity As String
Public LocalState As String
Public LocalZip As String
Public PermAddress1 As String
Public PermAddress2 As String
Public PermCity As String
Public PermState As String
Public PermZip As String
Public Speaking As String
Public ReadingWriting As String
Public InterviewNotes As String
Public FundraisingGoal As String
Public Profile As String
End Class
Public Class ParticipantAdmin
Public Function GetParticipantDetails(ByVal participantId As String) As ParticipantDetails
Dim connection As New SqlConnection(connectionString)
Dim command As New SqlCommand("GetParticipantDetails", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@ParticipantID", SqlDbType.Int)
command.Parameters("@ParticipantID").Value = participantId
connection.Open()
Dim reader As SqlDataReader
reader = command.ExecuteReader(CommandBehavior.CloseConnect ion)
Dim participantDetails As New ParticipantDetails
If reader.Read() Then
participantDetails.FirstName = reader("FirstName").ToString
participantDetails.LastName = reader("LastName").ToString
participantDetails.Email = reader("Email").ToString
participantDetails.Phone1 = reader("Phone1").ToString
participantDetails.Phone2 = reader("Phone2").ToString
participantDetails.BestTime = reader("BestTime").ToString
participantDetails.LocalAddress1 = reader("LocalAddress1").ToString
participantDetails.LocalAddress2 = reader("LocalAddress2").ToString
participantDetails.LocalCity = reader("LocalCity").ToString
participantDetails.LocalState = reader("LocalState").ToString
participantDetails.LocalZip = reader("LocalZip").ToString
participantDetails.PermAddress1 = reader("PermAddress1").ToString
participantDetails.PermAddress2 = reader("PermAddress2").ToString
participantDetails.PermCity = reader("PermCity").ToString
participantDetails.PermState = reader("PermState").ToString
participantDetails.PermZip = reader("PermZip").ToString
participantDetails.Speaking = reader("Speaking").ToString
participantDetails.ReadingWriting = reader("ReadingWriting").ToString
participantDetails.InterviewNotes = reader("InterviewNotes").ToString
participantDetails.FundraisingGoal = reader("FundraisingGoal").ToString
participantDetails.Profile = reader("Profile").ToString
reader.Close()
connection.Close()
End If
Return participantDetails
End Function
Public Sub UpdateParticipantDetails(ByVal participantDetails As ParticipantDetails)
Dim connection As New SqlConnection(connectionString)
Dim command As New SqlCommand("UpdateParticipantDetails", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@ParticipantID", SqlDbType.Int)
command.Parameters("@ParticipantID").Value = participantDetails.ParticipantId
command.Parameters.Add("@FirstName", SqlDbType.VarChar, 50)
command.Parameters("@FirstName").Value = participantDetails.FirstName
command.Parameters.Add("@LastName", SqlDbType.VarChar, 50)
command.Parameters("@LastName").Value = participantDetails.LastName
command.Parameters.Add("@Email", SqlDbType.VarChar, 50)
command.Parameters("@Email").Value = participantDetails.Email
command.Parameters.Add("@Phone1", SqlDbType.VarChar, 50)
command.Parameters("@Phone1").Value = participantDetails.Phone1
If participantDetails.Phone2.Trim <> "" Then
command.Parameters.Add("@Phone2", SqlDbType.VarChar, 50)
command.Parameters("@Phone2").Value = participantDetails.Phone2
End If
command.Parameters.Add("@BestTime", SqlDbType.VarChar, 200)
command.Parameters("@BestTime").Value = participantDetails.BestTime
command.Parameters.Add("@LocalAddress1", SqlDbType.VarChar, 50)
command.Parameters("@LocalAddress1").Value = participantDetails.LocalAddress1
If participantDetails.LocalAddress2.Trim <> "" Then
command.Parameters.Add("@LocalAddress2", SqlDbType.VarChar, 100)
command.Parameters("@LocalAddress2").Value = participantDetails.LocalAddress2
End If
command.Parameters.Add("@LocalCity", SqlDbType.VarChar, 100)
command.Parameters("@LocalCity").Value = participantDetails.LocalCity
command.Parameters.Add("@LocalState", SqlDbType.VarChar, 50)
command.Parameters("@LocalState").Value = participantDetails.LocalState
command.Parameters.Add("@LocalZip", SqlDbType.VarChar, 50)
command.Parameters("@LocalZip").Value = participantDetails.LocalZip
If participantDetails.PermAddress1.Trim <> "" Then
command.Parameters.Add("@PermAddress1", SqlDbType.VarChar, 100)
command.Parameters("@PermAddress1").Value = participantDetails.PermAddress1
End If
If participantDetails.PermAddress2.Trim <> "" Then
command.Parameters.Add("@PermAddress2", SqlDbType.VarChar, 100)
command.Parameters("@PermAddress2").Value = participantDetails.PermAddress2
End If
If participantDetails.PermCity.Trim <> "" Then
command.Parameters.Add("@PermCity", SqlDbType.VarChar, 50)
command.Parameters("@PermCity").Value = participantDetails.PermCity
End If
If participantDetails.PermState.Trim <> "" Then
command.Parameters.Add("@PermState", SqlDbType.VarChar, 50)
command.Parameters("@PermState").Value = participantDetails.PermState
End If
If participantDetails.PermZip.Trim <> "" Then
command.Parameters.Add("@PermZip", SqlDbType.VarChar, 50)
command.Parameters("@PermZip").Value = participantDetails.PermZip
End If
If participantDetails.Speaking.Trim <> "" Then
command.Parameters.Add("@Speaking", SqlDbType.VarChar, 500)
command.Parameters("@Speaking").Value = participantDetails.Speaking
End If
If participantDetails.ReadingWriting.Trim <> "" Then
command.Parameters.Add("@ReadingWriting", SqlDbType.VarChar, 500)
command.Parameters("@ReadingWriting").Value = participantDetails.ReadingWriting
End If
If participantDetails.InterviewNotes.Trim <> "" Then
command.Parameters.Add("@InterviewNotes", SqlDbType.VarChar, 8000)
command.Parameters("@InterviewNotes").Value = participantDetails.InterviewNotes
End If
If participantDetails.FundraisingGoal.Trim <> "" Then
command.Parameters.Add("@FundraisingGoal", SqlDbType.VarChar, 50)
command.Parameters("@FundraisingGoal").Value = participantDetails.FundraisingGoal
End If
If participantDetails.Profile.Trim <> "" Then
command.Parameters.Add("@Profile", SqlDbType.VarChar, 8000)
command.Parameters("@Profile").Value = participantDetails.Profile
End If
connection.Open()
command.ExecuteNonQuery()
connection.Close()
End Sub
End Class
And finally the SQL code...
ALTER PROCEDURE UpdateParticipantDetails
(
@ParticipantID int,
@FirstName varchar(50),
@LastName varchar(50),
@Email varchar(50),
@Phone1 varchar(50),
@Phone2 varchar(50),
@BestTime varchar(50),
@LocalAddress1 varchar(50),
@LocalAddress2 varchar(50),
@LocalCity varchar(50),
@LocalState varchar(50),
@LocalZip varchar(50),
@PermAddress1 varchar(50),
@PermAddress2 varchar(50),
@PermCity varchar(50),
@PermState varchar(50),
@PermZip varchar(50),
@Speaking varchar(50),
@ReadingWriting varchar(50),
@InterviewNotes varchar(8000),
@FundraisingGoal varchar(50),
@Profile varchar(8000)
)
AS
UPDATE Participant
SET FirstName = @FirstName, LastName = @LastName, Email = @Email, Phone1 = @Phone1,
Phone2 = @Phone2, BestTime = @BestTime, LocalAddress1 = @LocalAddress1, LocalAddress2 = @LocalAddress2,
LocalCity = @LocalCity, LocalState = @LocalState, LocalZip = @LocalZip,
PermAddress1 = @PermAddress1, PermAddress2 = @PermAddress2, PermCity = @PermCity,
PermState = @PermState, PermZip = @PermZip, Speaking = @Speaking, ReadingWriting = @ReadingWriting,
InterviewNotes = @InterviewNotes, FundraisingGoal = @FundraisingGoal, Profile = @Profile
WHERE ParticipantID = @ParticipantID