gerald vince

gerald vince

  • NA
  • 15
  • 7.2k

dropdown list won't populate textboxes

Feb 18 2015 12:18 PM
I have a dropdown list that I want to populate textboxes based on the users choice of a projectname. the DDL works and populates my list fine, but when chosen nothing goes into my textboxes, what am I doing wrong?
 
HTML  
 
<asp:DropDownList ID="DDL1" runat="server" Width="203px"
DataSourceID="SelectProject" DataTextField="ProjectName" OnDataBound="DDL1_SelectedIndexChanged_DataBound"
DataValueField="ProjectId" AutoPostBack="True" onselectedindexchanged="DDL1_SelectedIndexChanged" >
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server"
id="ddlreq" ControlToValidate="DDL1"
ErrorMessage = "Please Select A Project"
display="Dynamic" ForeColor="Red" />
<asp:SqlDataSource ID="SelectProject" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectsAndTasksTestConnectionString %>"
SelectCommand="SELECT [ProjectName], [ProjectId], [ProjectDescription], [DateAssigned], [DueDate] FROM [Projects]">
</asp:SqlDataSource><asp:Label id="lblResults" runat="server" Font-Bold="True"></asp:Label>
 
 
 
 and here is my C# behind it
 
 
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = DDL1.SelectedItem.Value;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectsAndTasksTestConnectionString"].ConnectionString);
using (connection)
{
//string selectSQL;
//selectSQL = "SELECT [ProjectDescription], [DateAssigned], [DueDate] FROM Projects ";
//selectSQL += "WHERE ProjectName='" + DDL1.SelectedItem.Value + "'";
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectsAndTasksTestConnectionString"].ConnectionString);
//SqlCommand cmd = new SqlCommand(selectSQL, con);
//SqlDataReader reader;
SqlCommand command = new SqlCommand("SELECT * FROM Projects WHERE ProjectName='" + DDL1.SelectedItem.Value + "'", connection);
command.Parameters.AddWithValue("@DDL1", selected);
command.CommandType = CommandType.Text;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
using (reader)
{
if (reader.HasRows)
{
reader.Read();
//add as many as needed to fill textboxes
UpdatetxtProjectDesc.Text = reader.GetString(1);
UpdatetxtStartDate.Text = reader.GetString(2);
UpdatetxtEndDate.Text = reader.GetString(3);
}
else { }
}
}
 
 
 
 
 
 

Answers (2)