Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my controller action:
public ActionResult Index()
{
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["System.Data.SqlClient"].ToString());
con.Open();

DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("SELECT DISTINCT ReportID FROM ebRLMaster", con);
ViewBag.ReportId = cmd;
sda.TableMappings.Add("Table", "ebRLMaster");
sda.Fill(ds);
ViewBag.ReportId = ds;
ViewBag.ReportId = "ebRLMaster.ReportID";
con.Close();
return View("RLMaster");
}
This is the dropdown in my view:
@Html.DropDownList("ReportId", new SelectList(ViewBag.ReportId, "ReportId", "ReportId"), "")

It throws the following error at Dropdown code:
DataBinding: System.Char does not contain a property with the name ReportId.

Please help!!!!!
Posted
Comments
Afzaal Ahmad Zeeshan 20-Aug-15 5:56am    
ViewBag is not a char object. There is something very confusing going on there, please try to debug your application.

1 solution

try with below code :

@Html.DropDownList("ReportId", new SelectList(Test(), "Value", "Text"), "Select Report")


Then try to set dataset to Selectlistitem from below code that is:

public static List<SelectListItem> Test()
{
//Decalre Selectlistitem
var objselectList = new List<SelectListItem>();

//over here set listof value = list of your dataset value
foreach (var objitem in listofvalue)
{
objselectList.Add(new SelectListItem
{
Text = objitem.ReportID,
Value = Convert.ToString(objitem.objitem),
});
}
return objselectList;
}
 
Share this answer
 
v4
Comments
Member 11579819 20-Aug-15 5:28am    
Still not working.. Error is
DataBinding: 'System.Char' does not contain a property with the name 'Value'.
Suket shah 20-Aug-15 5:40am    
hey check my updated answer.
Member 11579819 20-Aug-15 6:50am    
Didnt get you.. My values are supposed to come from database. Let me explain you :
Table name is "ebRLMaster" and there is a column out there named as "ReportId".
I am supposed to bring all the distinct values from that column to the drop down.
I made a debug and found that the values from database are there in the variable "ds".
I think the problem is binding those data's with the drop down.
Let me know if you can help me
Suket shah 20-Aug-15 7:00am    
Best way to do is make it one common function and directly call that function from view and that return type should be SelectListItem.
you can do it with my above code just remove one that is assign value to viewbag
Member 11579819 20-Aug-15 7:04am    
OK.. I'll try doing that

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