I am trying to populate / fill a dropdownlist from a TextBox_TextChanged event but I seem to not be able to retrieve the very first record. It completely bypasses it. It goes right to the second record and completely ignores the first record. Any help would be great. I have been wrapping my head around this for hours now and cannot figure it out for the life of me. Thanks in advance.
Here is my code behind:
protected void MyListDataBound(object sender, EventArgs e)
{
ddlSpaceNum.Items.Insert(0, new System.Web.UI.WebControls.ListItem("- Select -", ""));
ddlSpaceNum2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("- Select -", ""));
}
public void populateSpaceNum()
{
string selectSQL = @"Select Id, BU, SpaceNum
From PossessionTurnoverLetter
WHERE LTRIM(RTRIM(BU)) = '" + txtBU.Text + "'";
SqlConnection con = new SqlConnection(cnnString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader = default(SqlDataReader);
try
{
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
ddlSpaceNum2.DataSource = reader;
ddlSpaceNum2.DataTextField = "SpaceNum";
ddlSpaceNum2.DataValueField = "SpaceNum";
ddlSpaceNum2.DataBind();
}
}
catch (Exception ex)
{
//lblSupplier.Text = "Supplier Number is incorrect!!";
}
finally
{
reader.Close();
con.Close();
}
}
protected void txtBU_TextChanged(object sender, EventArgs e)
{
populateSpaceNum();
}
Here is my aspx page:
<asp:UpdatePanel ID="udpRevised" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table style="width:100%" cellpadding="4px">
<tr>
<td colspan="6">
</td>
</tr>
<tr>
<td colspan="6">
<asp:RadioButtonList ID="rblRevision" runat="server" RepeatDirection="Horizontal" TextAlign="Right" RepeatColumns="2" CellSpacing="10" CellPadding="10" ForeColor="#F78222" OnSelectedIndexChanged="rblRevision_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="Initial Possession Turnover"></asp:ListItem>
<asp:ListItem Value="Revised Possession Turnover"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>
<hr />
<asp:UpdatePanel ID="udpShowUpdate" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlRevised" runat="server" Visible="False">
<asp:UpdatePanel ID="udpSearch" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table style="width:100%" cellpadding="4px">
<tr>
<td colspan="4">
<asp:Label ID="lblSearch" runat="server" Text="Search" Font-Bold="True" Font-Underline="True" Font-Size="20px"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblBU" runat="server" Font-Bold="true" Text="Business Unit:"></asp:Label>
</td>
<td>
<asp:Label ID="lblSpace" runat="server" Font-Bold="true" Text="Space #:"></asp:Label>
</td>
<td>
<asp:Label ID="lblTurnoverDate" runat="server" Font-Bold="true" Text="Possession - Turnover Date:"></asp:Label><br />
</td>
<td>
</td>
</tr>
<tr>
<td colspan="6">
<asp:Label ID="lblError" runat="server" Text="You have not specified a valid entry. Please try again!" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtBU" runat="server" CssClass="form-control" OnTextChanged="txtBU_TextChanged" AutoPostBack="True"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="ddlSpaceNum2" runat="server" CssClass="form-control" OnDataBound="MyListDataBound" OnSelectedIndexChanged="ddlSpaceNum2_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtTurnoverDate" runat="server" data-datepicker="true" CssClass="form-control"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="btn btn-default" OnClick="btnSearch_Click" UseSubmitBehavior="False" />
</td>
</tr>
<tr>
<td colspan="6">
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtBU" EventName="TextChanged" />
<asp:AsyncPostBackTrigger ControlID="ddlSpaceNum2" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rblRevision" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Any help would be greatly appreciated. Thanks.
I suspect that the problem is here:
reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
ddlSpaceNum2.DataSource = reader;
There should be no need to call Read
method here. Doing so you shift reader's position to be after first item. And when DDL does its data binding - it starts from the second record, assuming this is the beginning of the reader.
Simply remove the call to Read
and you should be fine.