Using a value from database column for the selected index on a drop-down list in c#

0

I am working .NET app where I have a drop-down list of statuses and I want to set the selected index to a field in my database table called VisitorStatusID which is an integer in the database. I am using this code for that:

DataTable dt = new DataTable();

SqlConnection connection = new SqlConnection(“ ");

connection.Open();

SqlCommand sqlCmd = new SqlCommand("SELECT * FROM [HRO_REPORTS].[dbo].NCAMS", connection);

SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

sqlDa.Fill(dt);

if (dt.Rows.Count > 0)
{

    ddVisitStatus.SelectedIndex = (int)dt.Rows[gvNCAMS.SelectedIndex]["VisitorStatusID"];

}

connection.Close();

The problem that I am having is that the drop-down list is displaying the same selection regardless of the user selected and it is not the right status for ny of the users that I selected. What am I doing wrong?

Thank you.

Link: Using a value from database column for the selected index on a drop-down list in c#
Source: Stack Sql

Share.

About Author

Leave A Reply