dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
Pritom Nandy
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » LINQ »

How to bind data to dropdownlist in c#


Posted Date: 08 Feb 2012      Posted By:: kiran.d.n     Member Level: Bronze    Member Rank: 2673     Points: 2   Responses: 4



i am using Linq store procedure. i am fetching the the result as id and name to ISingleResult<objresult> obj. how i can bind the odj to dropdownlist as value as id and text as name.

Thanks,

Kiran.D.N
Web Developer
Kreativepool
Mysore
::WeB DevELopeRs::




Responses

#656078    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 08/Feb/2012   Rating: 2 out of 52 out of 5     Points: 3

Please Check this sample code to bind the Drop Down list.


SqlConnection con = new SqlConnection(ConnStr);
string strQuery = "select EmpName,ID from emp where Id>100";

SqlDataAdapter ada = new SqlDataAdapter(strQuery, con);
DataSet lds = new DataSet();
ada.Fill(lds);

ddlname.DataSource = lds;
ddlname.DataTextField = "Empname"
ddlname.DataValueField = "Id"
ddlname.DataBind();
ddlName.Items.Insert(0, "-Select-");



Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#656082    Author: Crazy Bouyz      Member Level: Bronze      Member Rank: 2264     Date: 08/Feb/2012   Rating: 2 out of 52 out of 5     Points: 2

Convert your ISingleResult object to ToList() and bind the dropdown with ID as value and Name as Text


 
#656094    Author: kiran.d.n      Member Level: Bronze      Member Rank: 2673     Date: 08/Feb/2012   Rating: 2 out of 52 out of 5     Points: 1

thank you Anil Kumar Pandey
its Working Fine..

Thanks,

Kiran.D.N
Web Developer
Kreativepool
Mysore
::WeB DevELopeRs::






 
#656937    Author: SonyMadhu      Member Level: Gold      Member Rank: 45     Date: 13/Feb/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi kiran,


To bind data to dropdown list, u can refer the below

string strCon = "Server=192.1.2.1;Database=DBName1;Uid=sa;Pwd=sb";
SqlConnection con = new SqlConnection(strCon);
string strSQL = "select name from table1";
SqlDataAdapter sqlda = new SqlDataAdapter(strSQL, con);
DataTable dt = new DataTable();
sqlda.Fill(dt);
try
{
con.Open();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
con.Close();
}

catch (Exception ex)
{

}


Regards,
Madhu
Be so hapy wen others look at you,they become hapy too



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : How to perform LINQ query on a SQL server database?
Previous : What Is LINQ in ASP.NET
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.