I have a class named memberdetails and memberdb which are part of my class library. I am using asp.net C# with MSSQL as the back end
My memberdetails class
public int MemID
{
get { return memID; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public MemberDetails(int memID, string firstName)
{
this.memID = memID;
this.firstName = firstName;
}
memberdb class
public List<MemberDetails> MemberResult(int memid)
{
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = con.CreateCommand())
{
List<MemberDetails> memberdetails = new List<MemberDetails>();
MemberDetails member;
try
{
con.Open();
cmd.CommandText = "usp_Member_Result";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@memgen", memid);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
member = new MemberDetails((int)reader["memid"], (string)reader["firstname"]);
memberdetails.Add(member);
}
reader.Close();
return memberdetails;
}
}
catch (SqlException err)
{
throw new ApplicationException("Data error.", err);
}
finally
{
if (con != null)
con.Close();
}
}
}
After I compile those classes into a DLL and then add it as a reference to my project I am trying to bind my DropDownlist to retrieve those values. In the codebehind page I am getting a value from the gridview putting it into a variable "memgen" sending that to my method and then returning the results. When I debug my application I see the values are returned however they are not binded to the DropDownList.
CodeBehind Page
member.MemberResult(memgen);
// MemberDetails details = new MemberDetails();
ddlFamilyMembers.DataTextField = "Text";
ddlFamilyMembers.DataValueField = "Value";
// ddlFamilyMembers.DataSource = memberdetails; << "This does not exist in the current context"
ddlFamilyMembers.DataBind();
Your code-behind needs to change to be something like this:
List<MemberDetails> details = member.MemberResult(memgen);
ddlFamilyMembers.DataTextField = "FirstName";
ddlFamilyMembers.DataValueField = "MemID";
ddlFamilyMembers.DataSource = details;
ddlFamilyMembers.DataBind();
The specific changes:
MemberResult
DataTextField
and DataValueField
DataSource
property I think the problem is that you forgot to add the datasource of the dropdownlist.
You can do as shown below :
var ddlvals = member.MemberResult(memgen);
ddlFamilyMembers.DataTextField = "FirstName";
ddlFamilyMembers.DataValueField = "MemID";
ddlFamilyMembers.DataSource = ddlvals;
ddlFamilyMembers.DataBind();
Without binding that won't work. I hope this helps your issue.
List<MemberDetails> details = member.MemberResult(memgen);
ddlFamilyMembers.DataTextField = "FirstName";
ddlFamilyMembers.DataValueField = "MemID";
ddlFamilyMembers.DataSource = details;
ddlFamilyMembers.DataBind();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.