Ron Aricha

Ron Aricha

  • 1.7k
  • 52
  • 2.2k

Bind sql data source to dropdownlist

Dec 10 2018 4:11 AM
private void fillExamList()
{
string selectExamination = "SELECT ExamCode, ExamType + ', ' + ExamDate AS ExamList FROM Examination WHERE PatientID = " + txtSearchID.Text + "";
SqlDataAdapter da = new SqlDataAdapter(selectExamination, conn);
DataSet ds = new DataSet();
try
{
conn.Open();
da.Fill(ds);
foreach (DataRow dr in ds.Tables["Table"].Rows)
{
if (dr["ExamType"].ToString() == "0")
{
dr["ExamType"] = "Yes";
}
}
ddlExamList.DataValueField = "ExamCode";
ddlExamList.DataTextField = "ExamList";
ddlExamList.DataSource = ds;
ddlExamList.DataBind();
ddlExamList.Items.Insert(0, new ListItem("Select...", "0"));
}
catch (Exception ex)
{
ErrorHandling(ex.ToString());
}
finally
{
conn.Close();
}
}
 
When I run this code I get an error that says:
 
System.ArgumentException: Column 'ExamType' does not belong to table Table.
at System.Data.DataRow.GetDataColumn(String columnName)
at System.Data.DataRow.get_Item(String columnName)
at BindData.Default.fillExamList() in C:\Users\Ron\Documents\Visual Studio 2017\Projects\BindData\BindData\Default.aspx.cs:line xxx
 
How can I fix it?

Answers (4)