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
DataSourceID="SelectProject" DataTextField="ProjectName" OnDataBound="DDL1_SelectedIndexChanged_DataBound"
DataValueField="ProjectId" AutoPostBack="True" onselectedindexchanged="DDL1_SelectedIndexChanged" >
id="ddlreq" ControlToValidate="DDL1"
ErrorMessage = "Please Select A Project"
display="Dynamic" ForeColor="Red" />
ConnectionString="<%$ ConnectionStrings:ProjectsAndTasksTestConnectionString %>"
SelectCommand="SELECT [ProjectName], [ProjectId], [ProjectDescription], [DateAssigned], [DueDate] FROM [Projects]">
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 { }
}
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question.
Khargesh RajputPosted Feb 18, 2015, 11:21 PM
you are getting data based on projectname
ans your code is
SqlCommand command = new SqlCommand("SELECT * FROM Projects WHERE ProjectName='" + DDL1.SelectedItem.Value + "'", connection);
DDL1.SelectedItem.Value should be DDL1.SelectedItem.Text if you search by project name
if you are getting data based on
DDL1.SelectedItem.Value then query sholud be
SELECT * FROM Projects WHERE Projectid='" + DDL1.SelectedItem.Value
gerald vincePosted Feb 25, 2015, 3:01 PM