C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Re:Binding column value to combo box in windows


Posted Date: 29 Aug 2008      Total Responses: 3

Posted By: satish       Member Level: Bronze     Points: 1



Hello all
How to bind the single column value to the combobox(windows form)from database in c#.

Thanks & regards
satish





Responses

Author: ANIL PANDEY    29 Aug 2008Member Level: DiamondRating:     Points: 6

hi,

u can refer the following code..


SqlConnection con = new SqlConnection(strConStr);
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
string strQuery = string.Empty;
int lintUID = Convert.ToInt32(Session["UID"].ToString());

strQuery = "Select Colname from tbLeaveDetails where UID='" + lintUID + "' order by DateFrom ";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
SqlDataAdapter ada = new SqlDataAdapter(strQuery,con);
ada.Fill(ds);

cmbLeaves.DataSource = ds;
cmbLeaves.DataBind();

//Closing the conection
con.Close();
cmd.Dispose();
con.Dispose();

Thanks
Anil

Thanks & Regards
Anil Kumar Pandey



Author: D.Jeya kumar(JK)    29 Aug 2008Member Level: DiamondRating:     Points: 6

Hi,


Try the below code.it will bind the single column for the Both the text and as well as value field.




string strConStr = "someconneection";
SqlConnection conn = new SqlConnection(strConStr);
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();

strQuery = "Select Columnname from Tblname where Userid=" + Userid + "";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
SqlDataAdapter ada = new SqlDataAdapter(strQuery, conn);
ada.Fill(ds);

cmbLeaves.DataSource = ds;
cmbLeaves.DataBind();

cons.Close();
cmd.Dispose();
con.Dispose();

DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataBind();

DropDownList1.DataTextField = "Columnname";
DropDownList1.DataValueField = "Columnname";


Regards
JK



Author: Ritesh N. Jain    29 Aug 2008Member Level: GoldRating:     Points: 4

Little modification in above code,Assign DataTextField property before calling DataBind Method,and in case you are not going to use DataValueField there is no need to assign that property as it will uncessarily postback data to client.


DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "Columnname";
DropDownList1.DataBind();



Post Reply
You must Sign In to post a response.
Next : how to change messagebox color in windows application
Previous : binding the columnvalue to the combobox in Windowsform
Return to Discussion Forum
Post New Message
Category: Windows

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use