Sir,
I am getting an Null Reference Exception in dataset even though the dataset is not null and count > 0 in ASP.NET.
Please do a solution for this issue.
Here the code :-
The error line is - ddlProduct.DataSource=ds;
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ravi KumarRank #2,085 · 690 reputationPosted Apr 9, 2015, 12:13 AM EDT
check the fallowing one
ddlproduct.Datasource=ds.tables[0].defaultview;
saravanagopi s23 reputationPosted Apr 9, 2015, 3:06 AM EDT
Akhil GargRank #772 · 3K reputationPosted Apr 9, 2015, 3:35 AM EDT
Bineesh ViswanathRank #1,681 · 907 reputationPosted Apr 9, 2015, 8:44 PM EDT
Akhil GargRank #772 · 3K reputationPosted Apr 9, 2015, 9:17 PM EDT
But you can initialize before setting it DropDownList ddlproduct=new DropDOwnList(); ddlproduct= .....
Khargesh RajputRank #616 · 3.9K reputationPosted Apr 9, 2015, 9:27 PM EDT
and check ds.tables[0].rows.count has value or not
remove ds!= null from if
only check count
and check ddlproduct control found or not
then check ds rows count
Bineesh ViswanathRank #1,681 · 907 reputationPosted Apr 9, 2015, 11:10 PM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 2, 2015, 5:27 AM EDT
Your FindControl method causes this error. You are attempting to access your DropDownList that is located inside a GridView in which your GridView is not bounded with data yet. You need to move your code for populating your DropDownList at RowDataBound event to avoid null reference error.
Here's a quick example:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
DropDownList ddl1 = (DropDownList)e.Row.FindControl("DropDownList1");
//populate your dropdownlist here
}
}