Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<table cellpadding="8" cellspacing="3"  >

<tr>
    <td>
        Category Name:
    </td>
    <td>
        <asp:DropDownList ID="DropDownList1" runat="server"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged"
            AutoPostBack="true" Width="146px" ontextchanged="DropDownList1_TextChanged">
        </asp:DropDownList>

    </td>
</tr>
<tr>
<td>
    SubCategory Name:
</td>
<td>
    <asp:DropDownList ID="DropDownList2" runat="server"  Width="146px"
        AutoPostBack="true" onprerender="DropDownList2_PreRender"
        onselectedindexchanged="DropDownList2_SelectedIndexChanged"
        ontextchanged="DropDownList2_TextChanged">
    </asp:DropDownList>
</td>
</tr>
<tr>
<td>
    Level Name: /td>
<td>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>








my c# code is:


C#
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            filldropdown();
           
            fillgrid();

            if (Request.QueryString["id"] != null)
            {
                Button1.Visible = false;
                Button2.Visible = true;
                edits();
            }
            else
            {
                Button2.Visible = false;
                Button1.Visible = true;
            }

           
        }


    }

    public void fillgrid()
    {
        onlineexam oe = new onlineexam();
        //DataTable dt = oe.getcasestudy();
        DataTable dt = oe.getlevel();
        if (dt.Rows.Count == 0)
        {

        }
        else
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }
    public void edits()
    {
        onlineexam oe = new onlineexam();
        int id = Convert.ToInt32(Request.QueryString["id"].ToString());
     
        DataTable dt = oe.getlevelbyid(id);
       
           string fn= dt.Rows[0]["CategoryId"].ToString();
        string sn = dt.Rows[0]["CategoryName"].ToString(); 
        DropDownList1.SelectedItem.Text = fn;
/*error here* Object reference not set to an instance of an object*/ DropDownList2.SelectedItem.Text= sn;

       // for (int z = 0; z < DropDownList1.Items.Count; z++)
       // {
       // if (DropDownList1.Items[z].Text == fn)
       // {
       //     DropDownList1.Items[z].Selected = true;
       // }
       //}
        //for (int x = 0; x < DropDownList2.Items.Count; x++)
        //{
        //    if (DropDownList2.Items[x].Text == sn)
        //    {
        //        DropDownList2.Items[x].Selected = true;
        //    }
        //}
            TextBox1.Text = dt.Rows[0]["LevelName"].ToString();
    }


    public void filldropdown()
    {
        onlineexam oe = new onlineexam();
        DataTable dt = oe.getcategoryid();
        if (dt.Rows.Count == 0)
        {
        }
        else
        {
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = dt.Columns["CategoryName"].ToString();
            DropDownList1.DataValueField = dt.Columns["Id"].ToString();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "Select");
        }
    }
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        string CategoryId = DropDownList1.SelectedItem.Text.ToString();
        string CategoryName = DropDownList2.SelectedItem.Text.ToString();
      //  string CategoryName = DropDownList3.SelectedItem.Text.ToString();
        string LevelName = TextBox1.Text;
        onlineexam oe = new onlineexam();
        oe.addlevel(CategoryId, CategoryName, LevelName);
        MessageBox msg = new MessageBox();
        msg.Show("Level Added Successfully");
        fillgrid();
        DropDownList2.Items.Clear();
        TextBox1.Text = "";
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

        MessageBox msg = new MessageBox();
        onlineexam oe = new onlineexam();
        // oe.deletecasestudy(id);
        oe.deletelevel(id);


        msg.Show("Deleting Successfully");
        fillgrid();
        DropDownList1.SelectedItem.Text = "Select";
        DropDownList2.Items.Clear();
        TextBox1.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        string CategoryId = DropDownList1.SelectedItem.Text.ToString();
        string CategoryName = DropDownList2.SelectedItem.Text.ToString(); ;
        //string CategoryName = DropDownList3.SelectedItem.Text.ToString();
        string LevelName = TextBox1.Text;
        onlineexam oe = new onlineexam();
        oe.updatelevel(id, CategoryId, CategoryName, LevelName);
        MessageBox msg = new MessageBox();
        msg.Show("Level Added Successfully");
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    public void filldropdown2(int CategoryId)
    {
        onlineexam oe = new onlineexam();
        DataTable dt = oe.getsubcategoryid(CategoryId);
        if (dt.Rows.Count == 0)
        {

            //DropDownList2.Items.Insert(0, "Select");
            DropDownList2.Items.Insert(0, "NULL");
        }
        else
        {
            DropDownList2.DataSource = dt;
           // DropDownList2.DataTextField = dt.Columns["CategoryName"].ToString();
            //string d = dt.Columns["CategoryName"].ToString();
            //DropDownList2.DataTextField = d;
            DropDownList2.DataValueField = dt.Columns["CategoryName"].ToString();
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, "Select");
        }
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_TextChanged(object sender, EventArgs e)
    {
        DropDownList2.Items.Clear();
        TextBox1.Text = "";
        int CategoryId = Convert.ToInt32(DropDownList1.SelectedItem.Value);
        filldropdown2(CategoryId);
    }
    protected void DropDownList2_TextChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList2_PreRender(object sender, EventArgs e)
    {
        
    }
}
Posted
Updated 7-Aug-12 21:31pm
v2
Comments
AshishChaudha 9-Aug-12 7:03am    
Is this issue solved without any solution...I can't find anything in accepted Solution..
Sergey Alexandrovich Kryukov 26-Mar-13 23:35pm    
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership. And the fact you even self-accepted on formally is just outrageous, a sure way for a ban. And you also pretended that you are answering someone else's question. I hope you won't do it after this warning.

Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

Hi,
Instead of :
C#
DropDownList1.SelectedItem.Text = fn;
/*error here* Object reference not set to an instance of an object*/ DropDownList2.SelectedItem.Text= sn;

Use:
C#
DropDownList1.Text = fn;
/*error here* Object reference not set to an instance of an object*/ DropDownList2.Text= sn;

Because you are entering custom text so selected item property will be null at that time until and unless you select any item from the list.


--Amit
 
Share this answer
 
Comments
Member 7767311 9-Aug-12 1:29am    
its not working
_Amy 9-Aug-12 1:31am    
What error you are getting after editing..??
Member 7767311 9-Aug-12 1:36am    
no but its not selecting anything
C#
DropDownList1.SelectedValue= fn;

/*error here* Object reference not set to an instance of an object*/

No need of this
C#
 string fn= dt.Rows[0]["CategoryId"].ToString();
 string sn = dt.Rows[0]["CategoryName"].ToString(); 
 DropDownList1.SelectedItem.Text = fn;

filldropdown2(fn);
DropDownList2.SelectedItem.Text= sn;


The selectedText of dropdown will be automatically get selected, because you are binding DropdownList before going to edit().


Thanks
Ashish
 
Share this answer
 
v2
Comments
Member 7767311 9-Aug-12 1:30am    
i want to select in edit automatically two dropdownvalues wen i click edit in gridview
Member 7767311 9-Aug-12 1:30am    
u r solution is not working
AshishChaudha 9-Aug-12 2:06am    
Dear, While editing and filling values in cascaded Dropdownlist. first dropdownlist is automatically fill on load, but when you edit you have to first fill second dropdownlist respect to the first dropdownlist items, then select value of dropdownlist what you are retrieving from database.
AshishChaudha 9-Aug-12 2:09am    
try my updated solution..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900