Set DropDownList selected value on ASP.Net GridView Edit Button Click using C# and VB.Net

Last Reply 21 days ago By itsme

Posted 22 days ago

I want dropdownlist to show database value entered by user at the time of filling the form on edit button click but I checked by debugging it my control is ignoring dopdownlist line thats why it is not filling with database value

I had written like this but control is skipping these line and giving error: Object reference not set to an instance of an object. at Jobopedia.SP_edit_new.btnEdit_Click(Object sender, EventArgs e)

DropDownList1.SelectedValue = (grow.FindControl("lblBname") as DropDownList).SelectedValue;
DropDownList2.SelectedValue = (grow.FindControl("lblcity") as DropDownList).SelectedValue;

 

        protected void btnEdit_Click(object sender, EventArgs e)
        {            
            try
            {
                Button btn = sender as Button;
                GridViewRow grow = btn.NamingContainer as GridViewRow;
                Id.Value = (grow.FindControl("Id") as Label).Text;
                Image img = (grow.FindControl("img") as Image);
                txt_email.Text = (grow.FindControl("lblemail") as Label).Text;
                txt_link.Text = (grow.FindControl("lblweb") as Label).Text;
                txt_img.Text = (grow.FindControl("lblimg") as Label).Text;
                txt_name.Text = (grow.FindControl("lblname") as Label).Text;
                txt_pay.Text = (grow.FindControl("lblpay") as Label).Text;
                txt_toll.Text = (grow.FindControl("lbltoll") as Label).Text;
                txt_Address.Text = (grow.FindControl("lblAddress") as Label).Text;
                Image1.ImageUrl = img.ImageUrl;
                btnUpdate.Visible = true;
                DropDownList1.SelectedValue = (grow.FindControl("lblBname") as DropDownList).SelectedValue;
                DropDownList2.SelectedValue = (grow.FindControl("lblcity") as DropDownList).SelectedValue;
                ddlTimeFrom.SelectedValue = (grow.FindControl("lblopen") as DropDownList).Text;
                ddlTimeTo.SelectedValue = (grow.FindControl("lblopen") as DropDownList).Text;
            }
            catch
            {

            }
        }

 


Posted 21 days ago Modified on 21 days ago

Hey

I found the answer and it is working

Thanks

        protected void btnEdit_Click(object sender, EventArgs e)
        {            
            try
            {
                Button btn = sender as Button;
                GridViewRow grow = btn.NamingContainer as GridViewRow;
                Id.Value = (grow.FindControl("Id") as Label).Text;
                Image img = (grow.FindControl("img") as Image);
                txt_email.Text = (grow.FindControl("lblemail") as Label).Text;
                txt_link.Text = (grow.FindControl("lblweb") as Label).Text;
                txt_img.Text = (grow.FindControl("lblimg") as Label).Text;
                txt_name.Text = (grow.FindControl("lblname") as Label).Text;
                txt_pay.Text = (grow.FindControl("lblpay") as Label).Text;
                txt_toll.Text = (grow.FindControl("lbltoll") as Label).Text;
                txt_Address.Text = (grow.FindControl("lblAddress") as Label).Text;
                Image1.ImageUrl = img.ImageUrl;
                btnUpdate.Visible = true;
                string value = (grow.FindControl("lblBname") as Label).Text;
                DropDownList1.ClearSelection();
                DropDownList1.Items.FindByText(value).Selected = true;
                string value2 = (grow.FindControl("lblcity") as Label).Text;
                DropDownList2.ClearSelection();
                DropDownList2.Items.FindByValue(value2).Selected = true;
                
                string from = (grow.FindControl("lblopen") as Label).Text;
                string[] parts = from.Split(',');
                //string to = (grow.FindControl("lblopen") as Label).Text;
                ddlTimeFrom.ClearSelection();ddlTimeTo.ClearSelection();
                ddlTimeFrom.Items.FindByValue(parts[0].Trim()).Selected = true;
                ddlTimeTo.Items.FindByValue(parts[1].Trim()).Selected = true;
            }
            catch(Exception ex)
            {
                Response.Write(ex);
            }
        }