finding and selecting the correct value with a databound dropdownlist

M

Matt

I need to select the correct item in a DDL from the value in the
database, if the record exists. Basically, the DDL is databound and
if this record already exists in tblFulfill, then I need the DDL to
show that value as selected. Everything is working except binding the
DDL. Here is the code for the DDL and for the IF statement.
*********************************************************
// Create the Employee List
SqlCommand cmdEmpAssign = new SqlCommand
("Select Fname + ' ' + Lname AS EmployeeName, EmpID FROM tblEmployee
ORDER BY Fname, Lname", sqlConnection1);
SqlDataReader drEmpAssign;
sqlConnection1.Open();
drEmpAssign = cmdEmpAssign.ExecuteReader();
ddlAssign.DataSource = drEmpAssign;
ddlAssign.DataTextField = "EmployeeName";
ddlAssign.DataValueField = "EmpID";
ddlAssign.DataBind();
drEmpAssign.Close();
sqlConnection1.Close();
// Add a blank entry at the top of the Employee List
ddlAssign.Items.Insert(0,"");

// Check to see if the RequestID is already in tblFulfill
bool fill = false;
SqlCommand cmdTest = new SqlCommand
("Declare @fill bit IF ('"+ rid +"'= (SELECT RequestID FROM
tblFulfill WHERE RequestID = '"+ rid +"')) SET @fill = 1 Else SET
@fill = 0 Select fill = @fill", sqlConnection1);
SqlDataReader drTest;
sqlConnection1.Open();
drTest = cmdTest.ExecuteReader();
while (drTest.Read())
{
fill = (drTest.GetBoolean(0));
}
drTest.Close();
//If the RequestID is already in tblFulfill then bind the controls
//to the values in the database
if (fill == true)
{
// Filter the Data View and bind the controls
dvFulfill.RowFilter = "RequestID = '" + urid + "' ";
ddlAssign.SelectedIndex =
ddlAssign.Items.IndexOf(ddlAssign.Items.FindByValue("FulfilledBy"));
datAssigned.DataBind();
chkFulfilled.DataBind();
datFulfilled.DataBind();
txtNotes.DataBind();
chkHold.DataBind();
txtHold.DataBind();
}
sqlConnection1.Close();
*****************************************************************

Thanks for the help and let me know if you need any more information.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top