Darokrithia Darokrithia - 4 months ago 8
ASP.NET (C#) Question

Why does this method not add items to a dropdownlist?

I am trying to add items from a database into a list instead of adding them statically from aspx. I made this method, and although it compiles, it doesn't add any list items to my drop down list. Here is some code, and I am willing to provide more if need be. The method in question is 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>

Answer

Your for loop condition is wrong, its need to be opposite

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