|
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:  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:  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:  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:  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. |
|
|
|
|
 Follow us on Twitter: https://twitter.com/dotnetspider
|
|