4
Answers

Error while editing in dropdownlist..Object reference not set to an instance of an object.

amaan ahmed

amaan ahmed

8y
6.4k
1
<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>











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)
{

}
}
Answers (4)