gerald vince

gerald vince

  • NA
  • 15
  • 7.1k

Auto populate textboxes from DDL

Feb 10 2015 6:47 PM
I have a dropdownlist, and once the user selects a project name I need the rest of the information to populate so they can potentially update that information in my database, I created a label that helped me get to where the code would not error when clicking on my submit button, but now it just says "Invalid attempt to read when no data is present"... , . I know that the data is there and In my ddl the information actually shows so I am not sure what the error is. here is the code I have....


 public partial class Projects_EditProject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                if (CheckBox_ProjectResults.Checked == true)
                {
                    //CheckBox_ProjectResults.Checked = true;
                    //if (CheckBox_ProjectResults.Checked)
                    TxtActualEnd.Enabled = true;
                    attachmentFileUpload.Enabled = true;
                }


                else if (CheckBox_ProjectResults.Checked == false)
                {
                    TxtActualEnd.Visible = false;
                    attachmentFileUpload.Visible = false;
                }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            string updateSQL;
            updateSQL = "UPDATE Projects SET ";
            updateSQL += "ProjectDescription=@ProjectDescription, ";
            updateSQL += "DateAssigned=@StartDate, DueDate=@DueDate ";
            updateSQL += "WHERE ProjectName=@ProjectName";


            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectsAndTasksTestConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand(updateSQL, con);


            cmd.Parameters.AddWithValue("@ProjectDescription", UpdatetxtProjectDesc.Text);
            cmd.Parameters.AddWithValue("@StartDate", UpdatetxtStartDate.Text);
            cmd.Parameters.AddWithValue("@DueDate", UpdatetxtEndDate.Text);
            cmd.Parameters.AddWithValue("@ProjectName", DDL1.SelectedItem.Value);


            int updated = 0;
            try
            {
                con.Open();
                updated = cmd.ExecuteNonQuery();
                lblResults.Text = updated.ToString() + " record updated.";
            }
            catch (Exception err)
            {
                lblResults.Text = "Error updating. ";
                lblResults.Text += err.Message;
            }
            finally
            {
                con.Close();
            }
        }




        protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectSQL;
            selectSQL = "SELECT [ProjectID], [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;


            try
            {
                con.Open();
                reader = cmd.ExecuteReader();
                reader.Read();


                UpdatetxtProjectID.Text = reader["ProjectID"].ToString();
                UpdatetxtProjectDesc.Text = reader["ProjectDescription"].ToString();
                UpdatetxtStartDate.Text = reader["DateAssigned"].ToString();
                UpdatetxtEndDate.Text = reader["DueDate"].ToString();
                //.Text = reader[""].ToString();
                //.Text = reader[""].ToString();
                //.Text = reader[""].ToString();
                //.Text = reader[""].ToString();
                CheckBox_ProjectResults.Checked = (bool)reader["Checked"];
                reader.Close();
                lblResults.Text = "";
            }
            catch (Exception err)
            {
                lblResults.Text = "Error getting Project. ";
                lblResults.Text += err.Message;
            }
            finally
            {
                con.Close();
            }
        }


        protected void DDL1_SelectedIndexChanged_DataBound(object sender, EventArgs e)
        {
            //Inserting an item in the 0 index of the DDL named "-Select-"
            //which will navigate the user to select an item
            DDL1.Items.Insert(0, new ListItem("-Select-"));
        }


        protected void validateCheckBoxes_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (!CheckBox_ProjectResults.Checked)
                args.IsValid = false;
            else
                args.IsValid = true;


        }


        public void cValidator_Validate(object sender, ServerValidateEventArgs e)
        {
            if (CheckBox_ProjectResults.Checked)            
            e.IsValid = false; // or do any additional validation checks here!    
        else            
            e.IsValid = true;  


        }

also I am trying to get two textboxes to show up after the user checks a checkbox, but I cannot get that to work either. they are not visible currently but when I check it they do not go visible....the C# for the two textboxes I have in the pageload...





Answers (2)