0
votes

So I'm writing a small VB webform that will allow users to look up data in a MySQL DB utilizing a DataList with ItemTemplate and edit that data using the EditItemTemplate.

I've got everything working except the Update portion of the app. When the user goes to update the edits they've made, they are greeted with

Unable to cast object of type 'System.Web.UI.WebControls.CommandEventArgs' to type 'System.Web.UI.WebControls.DataListCommandEventArgs'.

I believe this is due to the linkbutton having the OnCommand= attribute set. I recived this exact error when trying to setup the Cancel command, and what I did was change

Protected Sub DataList1_CancelCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)

to

Protected Sub DataList1_CancelCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)

However, I cannot do the same with the update command as it is getting the edit fields from the EditItemTemplate which needs the DataListCommandEventArgs to be set.

Below is my code; I need a way around this error with my update command functioning. Any help would be awesome!

ASPx Page

<form id="SearchForm" runat="server">
    <asp:SqlDataSource
    ID='FarmStandSource' runat='server'
    ConnectionString='<%$ ConnectionStrings:FSDATA3ConnectionString %>'
    SelectCommand='SelectFarmStand'
    SelectCommandType='StoredProcedure'>
  </asp:SqlDataSource>
<asp:DataList ID="DataList1" runat="server" ItemStyle-Width="95%" RepeatLayout="Flow"
        OnEditCommand="DataList1_EditCommand"
        OnCancelCommand="DataList1_CancelCommand"
        OnUpdateCommand="DataList1_UpdateCommand" DataKeyField="PKF">
<ItemTemplate>
...Fields...
</ItemTemplate>
<EditItemTemplate>
...Fields...
</EditItemTemplate>
</DataList>
</form>

CODE BEHIND VB

Protected Sub DataList1_UpdateCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)

    Dim PKF As Int32 = Convert.ToInt32(DataList1.DataKeys(e.Item.ItemIndex))
    Dim FarmBusinessName As TextBox = e.Item.FindControl("FarmBusinessNameEdit")
    Dim FarmOwners As TextBox = e.Item.FindControl("FarmOwnersEdit")
    Dim FullFarmAddress As TextBox = e.Item.FindControl("FullFarmAddressEdit")
    Dim Address1 As TextBox = e.Item.FindControl("Address1Edit")
    Dim City As TextBox = e.Item.FindControl("CityEdit")
    Dim State As TextBox = e.Item.FindControl("StateEdit")
    Dim Zip As TextBox = e.Item.FindControl("ZipEdit")
    Dim County As TextBox = e.Item.FindControl("CountyEdit")
    Dim Phone As TextBox = e.Item.FindControl("PhoneEdit")
    Dim Website As TextBox = e.Item.FindControl("WebsiteEdit")
    Dim Email As TextBox = e.Item.FindControl("EmailEdit")
    Dim DatesOpen As TextBox = e.Item.FindControl("DatesOpenEdit")
    Dim DatesClosed As TextBox = e.Item.FindControl("DatesClosedEdit")
    Dim StandOpenTime As TextBox = e.Item.FindControl("StandOpenTimeEdit")
    Dim StandCloseTime As TextBox = e.Item.FindControl("StandCloseTimeEdit")
    Dim BIO As TextBox = e.Item.FindControl("BIOEdit")
    Dim OpenEndedResponse As TextBox = e.Item.FindControl("OpenEndedResponseEdit")
    Dim Vegetables As CheckBox = e.Item.FindControl("VegetablesEdit")
    Dim Grains As CheckBox = e.Item.FindControl("GrainsEdit")
    Dim BreadBakedGoods As CheckBox = e.Item.FindControl("BreadBakedGoodsEdit")
    Dim Eggs As CheckBox = e.Item.FindControl("EggsEdit")
    Dim MilksRawMilk As CheckBox = e.Item.FindControl("MilksRawMilkEdit")
    Dim Cheese As CheckBox = e.Item.FindControl("CheeseEdit")
    Dim CiderApples As CheckBox = e.Item.FindControl("CiderApplesEdit")
    Dim Honey As CheckBox = e.Item.FindControl("HoneyEdit")
    Dim WineHardCider As CheckBox = e.Item.FindControl("WineHardCiderEdit")
    Dim Fruits As CheckBox = e.Item.FindControl("FruitsEdit")
    Dim Berries As CheckBox = e.Item.FindControl("BerriesEdit")
    Dim Maples As CheckBox = e.Item.FindControl("MaplesEdit")
    Dim Chicken As CheckBox = e.Item.FindControl("ChickenEdit")
    Dim Turkey As CheckBox = e.Item.FindControl("TurkeyEdit")
    Dim Beef As CheckBox = e.Item.FindControl("BeefEdit")
    Dim Pork As CheckBox = e.Item.FindControl("PorkEdit")
    Dim Lamb As CheckBox = e.Item.FindControl("LambEdit")
    Dim Goat As CheckBox = e.Item.FindControl("GoatEdit")
    Dim WoolFiber As CheckBox = e.Item.FindControl("WoolFiberEdit")
    Dim WoodProducts As CheckBox = e.Item.FindControl("WoodProductsEdit")
    Dim Flowers As CheckBox = e.Item.FindControl("FlowersEdit")
    Dim CannedBottledGoods As CheckBox = e.Item.FindControl("CannedBottledGoodsEdit")
    Dim PlantSeedlingsStarts As CheckBox = e.Item.FindControl("PlantSeedlingsStartsEdit")
    Dim PlantsTrees As CheckBox = e.Item.FindControl("PlantsTreesEdit")
    Dim Other As TextBox = e.Item.FindControl("OtherEdit")
    Dim CertifiedOrganic As TextBox = e.Item.FindControl("CertifiedOrganicEdit")
    Dim OrganicCertifier As TextBox = e.Item.FindControl("OrganicCertifierEdit")
    Dim OtherFarmsLocal As TextBox = e.Item.FindControl("OtherFarmsLocalEdit")
    Dim SupplementalCatagories As TextBox = e.Item.FindControl("SupplementalCatagoriesEdit")
    Dim BeginOperation As TextBox = e.Item.FindControl("BeginOperationEdit")
    Dim PickYourOwn As CheckBox = e.Item.FindControl("PickYourOwnEdit")
    Dim EBT As CheckBox = e.Item.FindControl("EBTEdit")
    Dim Staffed As TextBox = e.Item.FindControl("StaffedEdit")
    Dim CustomersPeakSeason As TextBox = e.Item.FindControl("CustomersPeakSeasonEdit")
    Dim Customers2015 As TextBox = e.Item.FindControl("Customers2015Edit")
    Dim Customers2014 As TextBox = e.Item.FindControl("Customers2014Edit")
    Dim PercentSelfProduced As TextBox = e.Item.FindControl("PercentSelfProducedEdit")
    Dim SellOtherFarms As CheckBox = e.Item.FindControl("SellOtherFarmsEdit")
    Dim LiabilityInsurance As CheckBox = e.Item.FindControl("LiabilityInsuranceEdit")

    Dim FarmBusinessNameValue = ""
    If FarmBusinessName.Text.Trim().Length > 0 Then
        FarmBusinessNameValue = FarmBusinessName.Text.Trim()
    End If
    Dim FarmOwnersValue = ""
    If FarmOwners.Text.Trim().Length > 0 Then
        FarmOwnersValue = FarmOwners.Text.Trim()
    End If
    Dim FullFarmAddressValue = ""
    If FullFarmAddress.Text.Trim().Length > 0 Then
        FullFarmAddressValue = FullFarmAddress.Text.Trim()
    End If
    Dim Address1Value = ""
    If Address1.Text.Trim().Length > 0 Then
        Address1Value = Address1.Text.Trim()
    End If
    Dim CityValue = ""
    If City.Text.Trim().Length > 0 Then
        CityValue = City.Text.Trim()
    End If
    Dim StateValue = ""
    If State.Text.Trim().Length > 0 Then
        StateValue = State.Text.Trim()
    End If
    Dim ZipValue = ""
    If Zip.Text.Trim().Length > 0 Then
        ZipValue = Zip.Text.Trim()
    End If
    Dim CountyValue = ""
    If County.Text.Trim().Length > 0 Then
        CountyValue = County.Text.Trim()
    End If
    Dim PhoneValue = ""
    If Phone.Text.Trim().Length > 0 Then
        PhoneValue = Phone.Text.Trim()
    End If
    Dim WebsiteValue = ""
    If Website.Text.Trim().Length > 0 Then
        WebsiteValue = Website.Text.Trim()
    End If
    Dim EmailValue = ""
    If Email.Text.Trim().Length > 0 Then
        EmailValue = Email.Text.Trim()
    End If
    Dim DatesOpenValue = ""
    If DatesOpen.Text.Trim().Length > 0 Then
        DatesOpenValue = DatesOpen.Text.Trim()
    End If
    Dim DatesClosedValue = ""
    If DatesClosed.Text.Trim().Length > 0 Then
        DatesClosedValue = DatesClosed.Text.Trim()
    End If
    Dim StandOpenTimeValue = ""
    If StandOpenTime.Text.Trim().Length > 0 Then
        StandOpenTimeValue = StandOpenTime.Text.Trim()
    End If
    Dim StandCloseTimeValue = ""
    If StandCloseTime.Text.Trim().Length > 0 Then
        StandCloseTimeValue = StandCloseTime.Text.Trim()
    End If
    Dim BIOValue = ""
    If BIO.Text.Trim().Length > 0 Then
        BIOValue = BIO.Text.Trim()
    End If
    Dim OpenEndedResponseValue = ""
    If OpenEndedResponse.Text.Trim().Length > 0 Then
        OpenEndedResponseValue = OpenEndedResponse.Text.Trim()
    End If
    Dim VegetablesValue = ""
    If Vegetables.Checked <> "" Then
        VegetablesValue = Vegetables.Checked
    End If
    Dim GrainsValue = ""
    If Grains.Checked <> "" Then
        GrainsValue = Grains.Checked
    End If
    Dim BreadBakedGoodsValue = ""
    If BreadBakedGoods.Checked <> "" Then
        BreadBakedGoodsValue = BreadBakedGoods.Checked
    End If
    Dim EggsValue = ""
    If Eggs.Checked <> "" Then
        EggsValue = Eggs.Checked
    End If
    Dim MilksRawMilkValue = ""
    If MilksRawMilk.Checked <> "" Then
        MilksRawMilkValue = MilksRawMilk.Checked
    End If
    Dim CheeseValue = ""
    If Cheese.Checked <> "" Then
        CheeseValue = Cheese.Checked
    End If
    Dim CiderApplesValue = ""
    If CiderApples.Checked <> "" Then
        CiderApplesValue = CiderApples.Checked
    End If
    Dim HoneyValue = ""
    If Honey.Checked <> "" Then
        HoneyValue = Honey.Checked
    End If
    Dim WineHardCiderValue = ""
    If WineHardCider.Checked <> "" Then
        WineHardCiderValue = WineHardCider.Checked
    End If
    Dim FruitsValue = ""
    If Fruits.Checked <> "" Then
        FruitsValue = Fruits.Checked
    End If
    Dim BerriesValue = ""
    If Berries.Checked <> "" Then
        BerriesValue = Berries.Checked
    End If
    Dim MaplesValue = ""
    If Maples.Checked <> "" Then
        MaplesValue = Maples.Checked
    End If
    Dim ChickenValue = ""
    If Chicken.Checked <> "" Then
        ChickenValue = Chicken.Checked
    End If
    Dim TurkeyValue = ""
    If Turkey.Checked <> "" Then
        TurkeyValue = Turkey.Checked
    End If
    Dim BeefValue = ""
    If Beef.Checked <> "" Then
        BeefValue = Beef.Checked
    End If
    Dim PorkValue = ""
    If Pork.Checked <> "" Then
        PorkValue = Pork.Checked
    End If
    Dim LambValue = ""
    If Lamb.Checked <> "" Then
        LambValue = Lamb.Checked
    End If
    Dim GoatValue = ""
    If Goat.Checked <> "" Then
        GoatValue = Goat.Checked
    End If
    Dim WoolFiberValue = ""
    If WoolFiber.Checked <> "" Then
        WoolFiberValue = WoolFiber.Checked
    End If
    Dim WoodProductsValue = ""
    If WoodProducts.Checked <> "" Then
        WoodProductsValue = WoodProducts.Checked
    End If
    Dim FlowersValue = ""
    If Flowers.Checked <> "" Then
        FlowersValue = Flowers.Checked
    End If
    Dim CannedBottledGoodsValue = ""
    If CannedBottledGoods.Checked <> "" Then
        CannedBottledGoodsValue = CannedBottledGoods.Checked
    End If
    Dim PlantSeedlingsStartsValue = ""
    If PlantSeedlingsStarts.Checked <> "" Then
        PlantSeedlingsStartsValue = PlantSeedlingsStarts.Checked
    End If
    Dim PlantsTreesValue = ""
    If PlantsTrees.Checked <> "" Then
        PlantsTreesValue = PlantsTrees.Checked
    End If
    Dim OtherValue = ""
    If Other.Text.Trim().Length > 0 Then
        OtherValue = Other.Text.Trim()
    End If
    Dim CertifiedOrganicValue = ""
    If CertifiedOrganic.Text.Trim().Length > 0 Then
        CertifiedOrganicValue = CertifiedOrganic.Text.Trim()
    End If
    Dim OrganicCertifierValue = ""
    If OrganicCertifier.Text.Trim().Length > 0 Then
        OrganicCertifierValue = OrganicCertifier.Text.Trim()
    End If
    Dim OtherFarmsLocalValue = ""
    If OtherFarmsLocal.Text.Trim().Length > 0 Then
        OtherFarmsLocalValue = OtherFarmsLocal.Text.Trim()
    End If
    Dim SupplementalCatagoriesValue = ""
    If SupplementalCatagories.Text.Trim().Length > 0 Then
        SupplementalCatagoriesValue = SupplementalCatagories.Text.Trim()
    End If
    Dim BeginOperationValue = ""
    If BeginOperation.Text.Trim().Length > 0 Then
        BeginOperationValue = BeginOperation.Text.Trim()
    End If
    Dim PickYuorOwnValue = ""
    If PickYourOwn.Checked <> "" Then
        PickYuorOwnValue = PickYourOwn.Checked
    End If
    Dim EBTValue = ""
    If EBT.Checked <> "" Then
        EBTValue = EBT.Checked
    End If
    Dim StaffedValue = ""
    If Staffed.Text.Trim().Length > 0 Then
        StaffedValue = Staffed.Text.Trim()
    End If
    Dim CustomersPeakSeasonValue = ""
    If CustomersPeakSeason.Text.Trim().Length > 0 Then
        CustomersPeakSeasonValue = CustomersPeakSeason.Text.Trim()
    End If
    Dim Customers2015Value = ""
    If Customers2015.Text.Trim().Length > 0 Then
        Customers2015Value = Customers2015.Text.Trim()
    End If
    Dim Customers2014Value = ""
    If Customers2014.Text.Trim().Length > 0 Then
        Customers2014Value = Customers2014.Text.Trim()
    End If
    Dim PercentSelfProducedValue = ""
    If PercentSelfProduced.Text.Trim().Length > 0 Then
        PercentSelfProducedValue = PercentSelfProduced.Text.Trim()
    End If
    Dim SellOtherFarmsValue = ""
    If SellOtherFarms.Checked <> "" Then
        SellOtherFarmsValue = SellOtherFarms.Checked
    End If
    Dim LiabilityInsuranceValue = ""
    If LiabilityInsurance.Checked <> "" Then
        LiabilityInsuranceValue = LiabilityInsurance.Checked
    End If

    Dim sqlConnection3 As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FSDATA").ConnectionString)
    Dim cmd As New SqlCommand
    cmd.CommandText = "UpdateFarmStand"
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = sqlConnection3

    sqlConnection3.Open()
    cmd.ExecuteNonQuery()
    sqlConnection3.Close()

    DataList1.EditItemIndex = -1

    hiddenindexfield.Text = ""

    DataList1.DataBind()

    SearchPanel.Visible = "True"

End Sub

UPDATE: I noticed that the UpdateCommand isn't firing at all, so I used codebreaks to find specifically where it was failing. Turns out it's dropping when it reaches the nested IF statement in my PostBack Checker in the Page_Load Handler. This checks to see if the user was in the edit view after PostBack, then does something based on that. Cutting off the IF statement and just letting the Page_Load handle the select statement throws a postback error.

Page_Load Handler

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load


    Dim sqlConnection1 As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FSDATA").ConnectionString)
    Dim cmd As New SqlCommand

    cmd.CommandText = "SelectFarmStand"
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = sqlConnection1

    Dim dt As New DataTable
    Dim retData As New SqlDataAdapter

    If Not IsPostBack Then
        retData.SelectCommand = cmd
        retData.Fill(dt)
        DataList1.DataSource = dt
        DataList1.DataBind()
    Else
        If hiddenindexfield.Text <> "" Then 
            DataList1.EditItemIndex = Convert.ToInt32(hiddenindexfield.Text)
            DisplayPanel.Visible = True '<- FAILS HERE
        Else
            retData.SelectCommand = cmd
            retData.Fill(dt)
            DataList1.DataSource = dt
            DataList1.DataBind()
        End If
    End If
End Sub
1

1 Answers

1
votes

DataListCommandEventArgs is derived from CommandEventArgs. So if the event arg was of the correct subtype it should cast. I think problem is you are trying to use the same handler for different types of events. (I didn't see the handler for the Link button you mentioned).

You can do that, but then you have to test the argument subtype. One way to do that is with the TryCast operator. For example:

Sub CommandEventHandler(sender as Object, e as CommandEventArgs)
    Dim e1 as DataListCommandEventArgs = TryCast(e, DataListCommandEventArgs)
    If e1 IsNot Nothing Then
        DataListEventAHander(sender, e1)
        Return
    End if
    ' Repeat for other subtypes as needed.
End Sub

Sub DataListEventHandler(sender as Object, e as DataListCommandEventArgs) 
    ' Your code for DataListEvents
End Sub

If your handlers are wired up correctly you should be able dispatch directly to the correct handler, either in the mark up or with the handles clause in the code.

Sub EventSubTypeHander(sender as Object, e as EventSubTypeArgs) handles object.EventSubType
   ' Event specific code here.
End Sub