将数据从数据库加载到Web表单中

时间:2012-07-02 18:18:54

标签: vb.net webforms postback vb.net-2010

我正在创建一个两步网页表单。第一步是初始创建应用程序(如申请某些内容,而不是程序),第二步是允许用户修改/重新提交此应用程序。

来自原始应用程序提交的数据正在被移植到数据库,并且正在按预期工作。当用户更新他们的应用程序时,应该将来自数据库的数据加载到在线表单中以允许编辑/审阅。目前,我知道数据被带入应用程序变量,但表单没有显示正在加载的数据。

表单包括文本框和复选框列表。

我对回发一无所知,我只创建了可以创建对象的本地webforms(例如user对象,但由于预编译错误,这是不可能的。

我使用的语言是VB.NET

任何帮助都将不胜感激。

编辑:代码

从连接开始的是什么:

Protected Sub btnLoad_Click(sender As Object, e As System.EventArgs) Handles btnLoad.Click

    business_name = txtName.Text

    dbConnect()
    dbGet()
    dbDisconnect()
End Sub

数据库连接字符串:

Public Sub dbConnect()
    connection = New SqlConnection("Server=localhost;Database=greenapplication;Trusted_Connection=True")
    connection.Open()
End Sub

Public Sub dbGet()
    general_read = New SqlCommand("SELECT * FROM general WHERE Business_Name = '" & business_name & "';", connection)
    energy_read = New SqlCommand("SELECT * FROM energy WHERE ApplicationID = '" & AppID & "';", connection)
    purchasing_read = New SqlCommand("SELECT * FROM purchasing WHERE ApplicationID = '" & AppID & "';", connection)
    recycling_read = New SqlCommand("SELECT * FROM recycling WHERE ApplicationID = '" & AppID & "';", connection)
    transportation_read = New SqlCommand("SELECT * FROM transportation WHERE ApplicationID = '" & AppID & "';", connection)
    social_read = New SqlCommand("SELECT * FROM social WHERE ApplicationID = '" & AppID & "';", connection)
    waste_read = New SqlCommand("SELECT * FROM waste WHERE ApplicationID = '" & AppID & "';", connection)
    water_read = New SqlCommand("SELECT * FROM water WHERE ApplicationID = '" & AppID & "';", connection)

    PostBackTrigger()
    form_load()

End Sub

Public Sub dbDisconnect()
    connection.Close()
End Sub

回发触发器:

Private Sub PostBackTrigger()
    'Throw New NotImplementedException
    generalReader = general_read.ExecuteReader
    While generalReader.Read
        AppID = generalReader.GetValue(generalReader.GetOrdinal("ApplicationID"))
        business_name = generalReader.GetValue(generalReader.GetOrdinal("Business_Name"))
        website = generalReader.GetValue(generalReader.GetOrdinal("Website"))
        description = generalReader.GetValue(generalReader.GetOrdinal("Description"))
        contact = generalReader.GetValue(generalReader.GetOrdinal("Contact"))
        enter_title = generalReader.GetValue(generalReader.GetOrdinal("Title"))
        phone = generalReader.GetValue(generalReader.GetOrdinal("Phone"))
        e_mail = generalReader.GetValue(generalReader.GetOrdinal("E_Mail"))
        street = generalReader.GetValue(generalReader.GetOrdinal("Street"))
        num_employees = generalReader.GetValue(generalReader.GetOrdinal("Num_Employees"))
        own_lease = generalReader.GetValue(generalReader.GetOrdinal("Own_Lease"))
        certs = generalReader.GetValue(generalReader.GetOrdinal("Certifications"))
        category = generalReader.GetValue(generalReader.GetOrdinal("Business_Category"))
    End While
    generalReader.Close()

    energyReader = energy_read.ExecuteReader
    While energyReader.Read
        track_usage = energyReader.GetValue(energyReader.GetOrdinal("usage"))
        develop_strategy = energyReader.GetValue(energyReader.GetOrdinal("policy"))
        perform_audit = energyReader.GetValue(energyReader.GetOrdinal("audit"))
        replace_bulbs = energyReader.GetValue(energyReader.GetOrdinal("lighting"))
        reduce_lighting = energyReader.GetValue(energyReader.GetOrdinal("over_lighting"))
        lighting_controls = energyReader.GetValue(energyReader.GetOrdinal("controls"))
        not_in_use_policy = energyReader.GetValue(energyReader.GetOrdinal("turn_off"))
        energy_star = energyReader.GetValue(energyReader.GetOrdinal("energy_star"))
        weatherize = energyReader.GetValue(energyReader.GetOrdinal("weatherize"))
        program_thermo = energyReader.GetValue(energyReader.GetOrdinal("thermostat"))
        natural_light = energyReader.GetValue(energyReader.GetOrdinal("natural_light"))
        air_quality = energyReader.GetValue(energyReader.GetOrdinal("air"))
        site_assessment = energyReader.GetValue(energyReader.GetOrdinal("renewable_assessment"))
        renewable_power = energyReader.GetValue(energyReader.GetOrdinal("on_site_renewable"))
        HVAC = energyReader.GetValue(energyReader.GetOrdinal("HVAC"))
        renewable_energy = energyReader.GetValue(energyReader.GetOrdinal("renewable_power"))
        energy_programs = energyReader.GetValue(energyReader.GetOrdinal("efficient_enroll"))
        energy_certification = energyReader.GetValue(energyReader.GetOrdinal("efficient_attain"))
        energy_other = energyReader.GetValue(energyReader.GetOrdinal("other"))
    End While
    energyReader.Close()

    purchasingReader = purchasing_read.ExecuteReader
    While purchasingReader.Read
        sustainable_policy = purchasingReader.GetValue(purchasingReader.GetOrdinal("policy"))
        office_paper = purchasingReader.GetValue(purchasingReader.GetOrdinal("paper"))
        maintenance = purchasingReader.GetValue(purchasingReader.GetOrdinal("maintenance"))
        ink_toner_purchase = purchasingReader.GetValue(purchasingReader.GetOrdinal("ink_toner"))
        food_container = purchasingReader.GetValue(purchasingReader.GetOrdinal("food"))
        office_stuff = purchasingReader.GetValue(purchasingReader.GetOrdinal("used_stuffs"))
        unwanted_stuff = purchasingReader.GetValue(purchasingReader.GetOrdinal("unwanted_stuffs"))
        green_vendors = purchasingReader.GetValue(purchasingReader.GetOrdinal("venders"))
        green_businesses = purchasingReader.GetValue(purchasingReader.GetOrdinal("purchase_venders"))
        purchasing_other = purchasingReader.GetValue(purchasingReader.GetOrdinal("other"))
    End While
    purchasingReader.Close()

    recyclingReader = recycling_read.ExecuteReader
    While recyclingReader.Read
        recycle_stuff = recyclingReader.GetValue(recyclingReader.GetOrdinal("law"))
        recycle_batteries = recyclingReader.GetValue(recyclingReader.GetOrdinal("batteries"))
        clean_sweep = recyclingReader.GetValue(recyclingReader.GetOrdinal("sweep"))
        recycle_banned = recyclingReader.GetValue(recyclingReader.GetOrdinal("non_landfill"))
        recycle_pharma = recyclingReader.GetValue(recyclingReader.GetOrdinal("pharma"))
        recycle_electronics = recyclingReader.GetValue(recyclingReader.GetOrdinal("e_waste"))
        recycle_package = recyclingReader.GetValue(recyclingReader.GetOrdinal("packaging"))
        ink_toner_recycle = recyclingReader.GetValue(recyclingReader.GetOrdinal("toner_ink"))
        compost = recyclingReader.GetValue(recyclingReader.GetOrdinal("compost"))
        recycling_other = recyclingReader.GetValue(recyclingReader.GetOrdinal("other"))
    End While
    recyclingReader.Close()

    transportationReader = transportation_read.ExecuteReader
    While transportationReader.Read
        teleconference = transportationReader.GetValue(transportationReader.GetOrdinal("teleconference"))
        non_idling = transportationReader.GetValue(transportationReader.GetOrdinal("non_idling"))
        flexible_schedule = transportationReader.GetValue(transportationReader.GetOrdinal("flex"))
        bike_shower = transportationReader.GetValue(transportationReader.GetOrdinal("bike_shower"))
        space_buy_out = transportationReader.GetValue(transportationReader.GetOrdinal("public_trans"))
        preferred_parking = transportationReader.GetValue(transportationReader.GetOrdinal("carpool_hybrid"))
        regular_tune_up = transportationReader.GetValue(transportationReader.GetOrdinal("tune_ups"))
        hybrid_alternative = transportationReader.GetValue(transportationReader.GetOrdinal("hybrid_alt"))
        bus_bike_route = transportationReader.GetValue(transportationReader.GetOrdinal("bus_bike"))
        fuel_efficiencies = transportationReader.GetValue(transportationReader.GetOrdinal("fuel_efficient"))
        grease_oil = transportationReader.GetValue(transportationReader.GetOrdinal("grease"))
        transportation_other = transportationReader.GetValue(transportationReader.GetOrdinal("other"))
    End While
    transportationReader.Close()

    socialReader = social_read.ExecuteReader
    While socialReader.Read
        healthy_environment = socialReader.GetValue(socialReader.GetOrdinal("health"))
        prevention_programs = socialReader.GetValue(socialReader.GetOrdinal("prevention"))
        volunteer = socialReader.GetValue(socialReader.GetOrdinal("volunteer"))
        carbon_footprint = socialReader.GetValue(socialReader.GetOrdinal("carbon"))
        charitable_organizations = socialReader.GetValue(socialReader.GetOrdinal("charity"))
        social_other = socialReader.GetValue(socialReader.GetOrdinal("other"))
    End While
    socialReader.Close()

    wasteReader = waste_read.ExecuteReader
    While wasteReader.Read
        printing = wasteReader.GetValue(wasteReader.GetOrdinal("paper"))
        bulk_duplicate_mailing = wasteReader.GetValue(wasteReader.GetOrdinal("mail"))
        printed_comms = wasteReader.GetValue(wasteReader.GetOrdinal("marketing"))
        packaging = wasteReader.GetValue(wasteReader.GetOrdinal("operations"))
        bags_containers = wasteReader.GetValue(wasteReader.GetOrdinal("containers"))
        track_waste = wasteReader.GetValue(wasteReader.GetOrdinal("reduction"))
        resuable_products = wasteReader.GetValue(wasteReader.GetOrdinal("products"))
        waste_other = wasteReader.GetValue(wasteReader.GetOrdinal("other"))
    End While
    wasteReader.Close()

    waterReader = water_read.ExecuteReader
    While waterReader.Read
        aerators_shut_off = waterReader.GetValue(waterReader.GetOrdinal("aerators"))
        low_flow = waterReader.GetValue(waterReader.GetOrdinal("devices"))
        pre_rinse = waterReader.GetValue(waterReader.GetOrdinal("pre_rinse"))
        leaks = waterReader.GetValue(waterReader.GetOrdinal("leaks"))
        landscaping = waterReader.GetValue(waterReader.GetOrdinal("vegitation"))
        broom = waterReader.GetValue(waterReader.GetOrdinal("broom"))
        sink_water = waterReader.GetValue(waterReader.GetOrdinal("sink_water"))
        storm_water = waterReader.GetValue(waterReader.GetOrdinal("storm_water"))
        water_assessment = waterReader.GetValue(waterReader.GetOrdinal("water_assessment"))
        irrigation = waterReader.GetValue(waterReader.GetOrdinal("irrigation"))
        fertilize = waterReader.GetValue(waterReader.GetOrdinal("fertilize"))
        ice_melt = waterReader.GetValue(waterReader.GetOrdinal("ice_melt"))
        water_other = waterReader.GetValue(waterReader.GetOrdinal("other"))
    End While
    waterReader.Close()
End Sub

我尝试过使用Business对象,但这让我在浏览器中出现了关于预编译的错误,我也尝试过上面看到的内容,我能描述的最好是回发,但我觉得我错过了什么。

编辑:添加了formLoad()Sub

Public Sub form_load()
    txtName.Text = business_name
    txtWebsite.Text = website
    txtDescription.Text = description
    txtContact.Text = contact
    txtTitle.Text = enter_title
    txtPhone.Text = phone
    txtEmail.Text = e_mail
    txtMailing.Text = street
    txtEmployees.Text = num_employees
    ddlOwnRent.SelectedValue = own_lease
    txtCerts.Text = certs

    Select Case category
        Case "Amusement & Entertainment"
            rblCategory.SelectedValue = "Amusement & Entertainment"
        Case "Apartments"
            rblCategory.SelectedValue = "Apartments"
        Case "Automotive"
            rblCategory.SelectedValue = "Automotive"
        Case "Computer, Internet, Telecom"
            rblCategory.SelectedValue = "Computer, Internet, Telecom"
        Case "Construction"
            rblCategory.SelectedValue = "Construction"
        Case "Diversified"
            rblCategory.SelectedValue = "Diversified"
        Case "Financial: banks & credit unions"
            rblCategory.SelectedValue = "Financial: banks & credit unions"
        Case "Health Care"
            rblCategory.SelectedValue = "Health Care"
        Case "Hotel / Motel"
            rblCategory.SelectedValue = "Hotel / Motel"
        Case "Insurance"
            rblCategory.SelectedValue = "Insurance"
        Case "Manufacturing"
            rblCategory.SelectedValue = "Manufacturing"
        Case "Professional"
            rblCategory.SelectedValue = "Professional"
        Case "Public Utilites"
            rblCategory.SelectedValue = "Public Utilites"
        Case "Publishers / Printers"
            rblCategory.SelectedValue = "Publishers / Printers"
        Case "Radio / Television"
            rblCategory.SelectedValue = "Radio / Television"
        Case "Real Estate"
            rblCategory.SelectedValue = "Real Estate"
        Case "Retail"
            rblCategory.SelectedValue = "Retail"
        Case "Services"
            rblCategory.SelectedValue = "Services"
        Case "Transportation"
            rblCategory.SelectedValue = "Transportation"
        Case "Wholesale / Distribution"
            rblCategory.SelectedValue = "Wholesale / Distribution"
        Case Else
            rblCategory.SelectedValue = "Other"
            txtOther.Visible = True
            txtOther.Text = category
    End Select

    cblEnergy.Items.FindByValue(1).Selected = track_usage
    cblEnergy.Items.FindByValue(2).Selected = develop_strategy
    cblEnergy.Items.FindByValue(3).Selected = perform_audit
    cblEnergy.Items.FindByValue(4).Selected = replace_bulbs
    cblEnergy.Items.FindByValue(5).Selected = reduce_lighting
    cblEnergy.Items.FindByValue(6).Selected = lighting_controls
    cblEnergy.Items.FindByValue(7).Selected = not_in_use_policy
    cblEnergy.Items.FindByValue(8).Selected = energy_star
    cblEnergy.Items.FindByValue(9).Selected = weatherize
    cblEnergy.Items.FindByValue(10).Selected = program_thermo
    cblEnergy.Items.FindByValue(11).Selected = natural_light
    cblEnergy.Items.FindByValue(12).Selected = air_quality
    cblEnergy.Items.FindByValue(13).Selected = site_assessment
    cblEnergy.Items.FindByValue(14).Selected = renewable_power
    cblEnergy.Items.FindByValue(15).Selected = HVAC
    cblEnergy.Items.FindByValue(16).Selected = renewable_energy
    cblEnergy.Items.FindByValue(17).Selected = energy_programs
    cblEnergy.Items.FindByValue(18).Selected = energy_certification
    cblEnergy.Items.FindByValue(19).Selected = energy_other

    cblPurchasing.Items.FindByValue(1).Selected = sustainable_policy
    cblPurchasing.Items.FindByValue(2).Selected = office_paper
    cblPurchasing.Items.FindByValue(3).Selected = maintenance
    cblPurchasing.Items.FindByValue(4).Selected = ink_toner_purchase
    cblPurchasing.Items.FindByValue(5).Selected = food_container
    cblPurchasing.Items.FindByValue(6).Selected = office_stuff
    cblPurchasing.Items.FindByValue(7).Selected = unwanted_stuff
    cblPurchasing.Items.FindByValue(8).Selected = green_vendors
    cblPurchasing.Items.FindByValue(9).Selected = green_businesses
    cblPurchasing.Items.FindByValue(10).Selected = purchasing_other

    cblRecycling.Items.FindByValue(1).Selected = recycle_stuff
    cblRecycling.Items.FindByValue(2).Selected = recycle_batteries
    cblRecycling.Items.FindByValue(3).Selected = clean_sweep
    cblRecycling.Items.FindByValue(4).Selected = recycle_banned
    cblRecycling.Items.FindByValue(5).Selected = recycle_pharma
    cblRecycling.Items.FindByValue(6).Selected = recycle_electronics
    cblRecycling.Items.FindByValue(7).Selected = recycle_package
    cblRecycling.Items.FindByValue(8).Selected = ink_toner_recycle
    cblRecycling.Items.FindByValue(9).Selected = compost
    cblRecycling.Items.FindByValue(10).Selected = recycling_other

    cblTransportation.Items.FindByValue(1).Selected = teleconference
    cblTransportation.Items.FindByValue(2).Selected = non_idling
    cblTransportation.Items.FindByValue(3).Selected = flexible_schedule
    cblTransportation.Items.FindByValue(4).Selected = bike_shower
    cblTransportation.Items.FindByValue(5).Selected = space_buy_out
    cblTransportation.Items.FindByValue(6).Selected = preferred_parking
    cblTransportation.Items.FindByValue(7).Selected = regular_tune_up
    cblTransportation.Items.FindByValue(8).Selected = hybrid_alternative
    cblTransportation.Items.FindByValue(9).Selected = bus_bike_route
    cblTransportation.Items.FindByValue(10).Selected = fuel_efficiencies
    cblTransportation.Items.FindByValue(11).Selected = grease_oil
    cblTransportation.Items.FindByValue(12).Selected = transportation_other

    cblSocial.Items.FindByValue(1).Selected = healthy_environment
    cblSocial.Items.FindByValue(2).Selected = prevention_programs
    cblSocial.Items.FindByValue(3).Selected = volunteer
    cblSocial.Items.FindByValue(4).Selected = carbon_footprint
    cblSocial.Items.FindByValue(5).Selected = charitable_organizations
    cblSocial.Items.FindByValue(6).Selected = social_other

    cblWaste.Items.FindByValue(1).Selected = printing
    cblWaste.Items.FindByValue(2).Selected = bulk_duplicate_mailing
    cblWaste.Items.FindByValue(3).Selected = printed_comms
    cblWaste.Items.FindByValue(4).Selected = packaging
    cblWaste.Items.FindByValue(5).Selected = bags_containers
    cblWaste.Items.FindByValue(6).Selected = track_waste
    cblWaste.Items.FindByValue(7).Selected = resuable_products
    cblWaste.Items.FindByValue(8).Selected = waste_other

    cblWater.Items.FindByValue(1).Selected = aerators_shut_off
    cblWater.Items.FindByValue(2).Selected = low_flow
    cblWater.Items.FindByValue(3).Selected = pre_rinse
    cblWater.Items.FindByValue(4).Selected = leaks
    cblWater.Items.FindByValue(5).Selected = landscaping
    cblWater.Items.FindByValue(6).Selected = broom
    cblWater.Items.FindByValue(7).Selected = sink_water
    cblWater.Items.FindByValue(8).Selected = storm_water
    cblWater.Items.FindByValue(9).Selected = water_assessment
    cblWater.Items.FindByValue(10).Selected = irrigation
    cblWater.Items.FindByValue(11).Selected = fertilize
    cblWater.Items.FindByValue(12).Selected = ice_melt
    cblWater.Items.FindByValue(13).Selected = water_other
End Sub

1 个答案:

答案 0 :(得分:0)

在调用PostBackTrigger()

之前,不会填充AppID
Public Sub dbGet()
    general_read = New SqlCommand("SELECT * FROM general WHERE Business_Name = '" & business_name & "';", connection)

    GetGeneralData()

    energy_read = New SqlCommand("SELECT * FROM energy WHERE ApplicationID = '" & AppID & "';", connection)
    purchasing_read = New SqlCommand("SELECT * FROM purchasing WHERE ApplicationID = '" & AppID & "';", connection)
    recycling_read = New SqlCommand("SELECT * FROM recycling WHERE ApplicationID = '" & AppID & "';", connection)
    transportation_read = New SqlCommand("SELECT * FROM transportation WHERE ApplicationID = '" & AppID & "';", connection)
    social_read = New SqlCommand("SELECT * FROM social WHERE ApplicationID = '" & AppID & "';", connection)
    waste_read = New SqlCommand("SELECT * FROM waste WHERE ApplicationID = '" & AppID & "';", connection)
    water_read = New SqlCommand("SELECT * FROM water WHERE ApplicationID = '" & AppID & "';", connection)

    PostBackTrigger()
    form_load()

End Sub

获取常规数据的新功能

Private Sub GetGeneralData()
    generalReader = general_read.ExecuteReader
    While generalReader.Read
        AppID = generalReader.GetValue(generalReader.GetOrdinal("ApplicationID"))
        business_name = generalReader.GetValue(generalReader.GetOrdinal("Business_Name"))
        website = generalReader.GetValue(generalReader.GetOrdinal("Website"))
        description = generalReader.GetValue(generalReader.GetOrdinal("Description"))
        contact = generalReader.GetValue(generalReader.GetOrdinal("Contact"))
        enter_title = generalReader.GetValue(generalReader.GetOrdinal("Title"))
        phone = generalReader.GetValue(generalReader.GetOrdinal("Phone"))
        e_mail = generalReader.GetValue(generalReader.GetOrdinal("E_Mail"))
        street = generalReader.GetValue(generalReader.GetOrdinal("Street"))
        num_employees = generalReader.GetValue(generalReader.GetOrdinal("Num_Employees"))
        own_lease = generalReader.GetValue(generalReader.GetOrdinal("Own_Lease"))
        certs = generalReader.GetValue(generalReader.GetOrdinal("Certifications"))
        category = generalReader.GetValue(generalReader.GetOrdinal("Business_Category"))
    End While
    generalReader.Close()

End Sub

使用这么多变量,这个应用程序对内存的影响非常大。你真的应该在阅读的同时设置你的字段。

Private Sub GetGeneralData()
    generalReader = general_read.ExecuteReader
    While generalReader.Read
        AppID = generalReader.GetValue(generalReader.GetOrdinal("ApplicationID"))
        txtName.Text = generalReader.GetValue(generalReader.GetOrdinal("Business_Name"))
        txtWebsite.Text = generalReader.GetValue(generalReader.GetOrdinal("Website"))
        txtDescription.Text = generalReader.GetValue(generalReader.GetOrdinal("Description"))
        txtContact.Text = generalReader.GetValue(generalReader.GetOrdinal("Contact"))
        txtTitle.Text = generalReader.GetValue(generalReader.GetOrdinal("Title"))
        txtPhone.Text = generalReader.GetValue(generalReader.GetOrdinal("Phone"))
        txtEmail.Text = generalReader.GetValue(generalReader.GetOrdinal("E_Mail"))
        txtMailing.Text = generalReader.GetValue(generalReader.GetOrdinal("Street"))
        txtEmployees.Text = generalReader.GetValue(generalReader.GetOrdinal("Num_Employees"))
        ddlOwnRent.SelectedValue = generalReader.GetValue(generalReader.GetOrdinal("Own_Lease"))
        txtCerts.Text = generalReader.GetValue(generalReader.GetOrdinal("Certifications"))
        category = generalReader.GetValue(generalReader.GetOrdinal("Business_Category"))
    End While
    generalReader.Close()

End Sub