为什么此方法不会将项添加到下拉列表中?

时间:2016-08-02 19:29:31

标签: c# sql asp.net visual-studio linq

我正在尝试将数据库中的项目添加到列表中,而不是从aspx中静态添加它们。我创建了这个方法,虽然它编译,但它不会向我的下拉列表中添加任何列表项。这是一些代码,如果需要,我愿意提供更多代码。有问题的方法是addListItemsToPage()。

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EndophthalmitisDatabase {
    public partial class DataEntry : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            addListItemsToPage();
        }

        protected void addListItemsToPage() {
            EndophthalmitisDBDataContext dbContext = new EndophthalmitisDBDataContext();

            for (int i=0;  dbContext.Hospital_Datas.Count() < i; i++){
                int id = dbContext.Hospital_Datas.ElementAt(i).HospitalID;
                string str = dbContext.Hospital_Datas.ElementAt(i).Hospital_Name;

                ListItem li = new ListItem();
                li.Text = str;
                li.Value = id.ToString();

                hospitalDDL.Items.Add(li);
            }

        }

        protected void submitEndoDataBtn_Click(object sender, EventArgs e)
        {
            using (EndophthalmitisDBDataContext dbContext = new EndophthalmitisDBDataContext()) {
                Occurrence_Data occData = new Occurrence_Data
                {
                    LastName = lastNameTB.Text,
                    FirstName = firstNameTB.Text,
                    MRN = int.Parse(mrnTB.Text),
                    DateOfOccurence = DateOfOccurenceCal.SelectedDate,
                    InterventionProcedureDone = int.Parse(InterventionDDL.SelectedValue),
                    PreOccurrencePhysician = int.Parse(InterventionSurgeonDDL.SelectedValue),
                    LabsSent = labsSentCB.Checked,
                    PrecipitatingCausePreviousSurgery = int.Parse(preCauseDDL.SelectedValue),
                    PostOccurrencePhysician = int.Parse(preSurgeonDDL.SelectedValue),
                    DateOfPreviousSurgery = datePrecipitatingCal.SelectedDate,
                    Hospital = int.Parse(hospitalDDL.SelectedValue),
                    AdditionalInformation = moreInfoTB.Text
                };

                lastNameTB.Text = "";
                firstNameTB.Text = "";
                mrnTB.Text = "";
                labsSentCB.Checked = false;
                moreInfoTB.Text = "";

                dbContext.Occurrence_Datas.InsertOnSubmit(occData);
                dbContext.SubmitChanges();
            }
        }
    }
}

ASPX

<%@ Page Title="" Language="C#" MasterPageFile="~/MainSite.Master" AutoEventWireup="true" CodeBehind="DataEntry.aspx.cs" Inherits="EndophthalmitisDatabase.DataEntry" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" runat="server">
     <div class="formBox">
        <p style="text-align: center">Please use the area below to enter Data.</p>
        <div class = "dataEntryForm">
            <form id = "occurrenceForm" runat = "server">
                <div class ="dataRow">
                    <div class ="dataRowText">Last Name</div>
                    <div class ="dataRowEntrance">
                        <asp:TextBox ID = "lastNameTB" runat = "server"></asp:TextBox>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Last Name</div>
                    <div class ="dataRowEntrance">
                        <asp:TextBox ID = "firstNameTB" runat = "server"></asp:TextBox>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">MRN</div>
                    <div class ="dataRowEntrance">
                        <asp:TextBox ID = "mrnTB" runat = "server"></asp:TextBox>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Date of Occurence</div>
                    <div class ="dataRowEntrance">
                        <asp:Calendar ID="DateOfOccurenceCal" runat="server"></asp:Calendar>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Intervention</div>
                    <div class ="dataRowEntrance">
                        <asp:DropDownList ID="InterventionDDL" runat="server">
                            <asp:ListItem Value ="1">Phaco</asp:ListItem>
                            <asp:ListItem Value ="2">Injection</asp:ListItem>
                            <asp:ListItem Value ="3">Retina Surgery</asp:ListItem>
                            <asp:ListItem Value ="4">Transplant</asp:ListItem>
                            <asp:ListItem Value ="5">Other</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Intervention Surgeon</div>
                    <div class ="dataRowEntrance">
                        <asp:DropDownList ID="InterventionSurgeonDDL" runat="server">
                            <asp:ListItem Value ="8">Tabin</asp:ListItem>
                            <asp:ListItem Value ="9">Crandal</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Labs sent</div>
                    <div class ="dataRowEntrance">
                        <asp:CheckBox ID="labsSentCB" runat="server" />
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Precipitating Cause</div>
                    <div class ="dataRowEntrance">
                        <asp:DropDownList ID="preCauseDDL" runat="server">
                            <asp:ListItem Value ="1">Phaco</asp:ListItem>
                            <asp:ListItem Value ="2">Injection</asp:ListItem>
                            <asp:ListItem Value ="3">Retina Surgery</asp:ListItem>
                            <asp:ListItem Value ="4">Transplant</asp:ListItem>
                            <asp:ListItem Value ="5">Other</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Precipitating Surgeon</div>
                    <div class ="dataRowEntrance">
                        <asp:DropDownList ID="preSurgeonDDL" runat="server">
                            <asp:ListItem Value ="8">Tabin</asp:ListItem>
                            <asp:ListItem Value ="9">Crandal</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Data of Precipitating Cause</div>
                    <div class ="dataRowEntrance">
                        <asp:Calendar ID ="datePrecipitatingCal" runat="server"></asp:Calendar>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Hospital</div>
                    <div class ="dataRowEntrance">
                        <asp:DropDownList ID ="hospitalDDL" runat="server">
                        </asp:DropDownList>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">Additional Information</div>
                    <div class ="dataRowEntrance">
                        <asp:TextBox ID ="moreInfoTB" runat="server" Rows ="100"></asp:TextBox>
                    </div>
                </div>
                <div class ="dataRow">
                    <div class ="dataRowText">
                        <asp:Button ID ="submitEndoDataBtn" runat="server" Text="Submit" OnClick ="submitEndoDataBtn_Click"/>
                    </div>
                </div>
            </form>
        </div>
    </div>
</asp:Content>

3 个答案:

答案 0 :(得分:5)

您的for循环条件错误,需要相反

for (int i=0;  i < dbContext.Hospital_Datas.Count(); i++)

答案 1 :(得分:3)

  1. 始终将using创建包装在for语句中,或者如果它位于类级别,则将其置于包含的类中。你的方法是在方法范围内,你的代码在没有处理它的情况下抛弃它(不好的做法!)
  2. 您的for语句错误,但您确实不需要foreach,您可以使用Lambda,这将更容易阅读或使用Ashkan建议的数据绑定。
  3. 您也可以将其压缩为单个语句,请参阅代码中下面注释中的OR。这使用protected void addListItemsToPage() { // wrap this in a using statement so the connection is closed and disposed using(EndophthalmitisDBDataContext dbContext = new EndophthalmitisDBDataContext()) { // replaced your wrong for statement with a foreach because you do not need a for statement (you are not using the indexer in a way that added any benifit) foreach(var hospital in dbContext.Hospital_Datas) ListItem li = new ListItem(); li.Text = hospital.Hospital_Name; li.Value = hospital.HospitalID; hospitalDDL.Items.Add(li); } // OR you could write it as a single statement hospitalDDL.AddRange(dbContext.Hospital_Datas.Select(hospital => new ListItem(){Text = hospital.Hospital_Name, Value = hospital.HospitalID})); } } 语句。
  4. 代码:

    M-n-e

答案 2 :(得分:1)

OMG,这是非常糟糕的做法。每次进入循环时都会调用DB。

 protected void addListItemsToPage() {
    EndophthalmitisDBDataContext dbContext = new   EndophthalmitisDBDataContext();

   hospitalDDL.DataTextField = "Hospital_Name";
    hospitalDDL.DataValueField = "HospitalID"

    hospitalDDL.DataSource = dbContext.Hospital_Datas.ToList();
    hospitalDDL.DataBind();

  }