Link to home
Start Free TrialLog in
Avatar of metropia
metropiaFlag for United States of America

asked on

how to access listview values on edit, and pass them to stringbuilder

Hello,

Would someone help me figure out how to correct the error on the subject of question?

My code is:

Private Sub lvRecipeSteps_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles lvRecipeSteps.ItemCommand

    If e.CommandName = "Update" Then
        Dim myContext As New OLTPEntities
        Dim item As ListViewItem = e.Item
        Dim tbx As TextBox = TryCast(item.FindControl("Recipe_IdTextBox"), TextBox)
        Dim strRecipeId As String = String.Empty
        strRecipeId = tbx.Text
        Dim myRecipeStepsHistory = myContext.RefineRecipeStep.Where("it.Recipe_Id=" & strRecipeId).FirstOrDefault()
        Dim strRefineRecipeStepRecord = New StringBuilder("")

    If myRecipeStepsHistory IsNot Nothing Then

        ' Recipe_Id
        strRefineRecipeStepRecord.Append("Recipe_Id: " & myRecipeStepsHistory.Recipe_Id & ", ")

        ' Recipe_ProcessType
        If Not String.Equals(myRecipeStepsHistory.ProcessType, TryCast(item.FindControl("ProcessType"), TextBox)) Then
    strRefineRecipeStepRecord.Append("ProcessType: Old= " & myRecipeStepsHistory.Recipe_ItemNumber & ", New= " & TryCast(item.FindControl("ProcessType"), TextBox).ToString() & ", ")
    End If

        ' AdditionOrder
        If Not String.Equals(myRecipeStepsHistory.AdditionOrder, TryCast(item.FindControl("AdditionOrder"), TextBox)) Then 
            strRefineRecipeStepRecord.Append("AdditionOrder: Old= " & myRecipeStepsHistory.AdditionOrder & ", New= " & TryCast(item.FindControl("AdditionOrder"), TextBox).ToString() & ", ")
End If

                ' Log Update
                Dim strId As String = Me.dvRecipeItem.DataKey(0).ToString()
                Dim strModifiedBy As String = System.Web.Security.Membership.GetUser.UserName.Trim
                Dim myAppLog As New AppLogClass(inEventCode:="Update",
                                                inItemCode:=strId,
                                                inDetailCode:=Membership.GetUser.UserName,
                                                inDescription:="Recipe Step Updated By: " & strModifiedBy,
                                                inNote:=strRefineRecipeStepRecord.ToString())
                myAppLog.Save()
            End If
        End If

    End Sub    

Open in new window


Then that error goes away but then i get a Object reference not set to an instance of an object.

I think each on of these fields are empty (nothing)

TryCast(item.FindControl("ProcessType"), TextBox)
TryCast(item.FindControl("AdditionOrder"), TextBox)


Please any help would be much appreciated.
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you show the aspx code?
Avatar of metropia

ASKER

attached there is the aspx code only the listview part.

thanks a lot.
reciper.aspx.txt
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
let me try that!
It worked indeed.
right on target.

thank you very much CodeCruiser!
I would like to extend a little bit on this question.

When I do the e.Command = "Delete"  does not work like it does for the Update.

Perhaps is due to the value coming from a different template?

I tried changing it like this:

        If e.CommandName = "Delete" Then

            Dim myContext As New OLTPEntities
            Dim myRecipeStepsHistory = myContext.RefineRecipeStep.Where("it.Recipe_Id=" & Me.lvRecipeSteps.FindControl("Recipe_Id").ToString())
            Dim strRefineRecipeStepRecord = New StringBuilder("")

            If Not IsNothing(myRecipeStepsHistory) Then
                ' Recipe_Id
                strRefineRecipeStepRecord.Append("Recipe_Id: " & Me.lvRecipeSteps.FindControl("Recipe_Id").ToString() & ", ")

Open in new window

\

I get an Object reference not set to an instnnce of an object message again

Dim myRecipeStepsHistory = myContext.RefineRecipeStep.Where("it.Recipe_Id=" & Me.lvRecipeSteps.FindControl("Recipe_Id").ToString())  << this line