How to bind data from Database to control fields?
When I select a value in DropDownList, I need to show its corresponding values to the textfields (Of course, there are textfields) from the database.
Example
Database : Table
eID eName eSalary
1 Tincy 50000
2 Vincy 40000
I have a DropDownList specifying the primary key(Here, eID is the prima开发者_如何学运维ry key). So, when I choose a value in DropDownList I need the whole details of eID-1 to be displayed in TextBoxes.
First of all, dropDownList1_SelectedIndexChanged event, handle selected item.
int requestedId = Convert.ToInt32(dropDownList1.selectedItem.Tostring());
and get your results with selectedId
var result = (from s in dataContext.YOURTABLE where s.Id.Equals(requestedId) select s).ToList();
if(result.Count > 0)
{return result.First()}
after First(). you can call any values whatever you want, and bind them to textFields.
To bind data with control follow the below steps:
1.Create stored proceudre or query to fetch data from your database table like
select * from Table where eID = 1
2.query data using DataReader object.
3.Assign value of datareader object to textbox control like
textbox1.Text = dr["eName "].ToString();
精彩评论