简体   繁体   中英

Oracle Connection from C# not reading data

So the following code will not enter the loop to populate my list box. My list box shows "Select All" and test outputs "entering loop". What would cause the try not to fail but the loop not to execute either?

conn.Open();
OracleCommand executeQuery = new OracleCommand(sql, conn);
executeQuery.CommandType = CommandType.Text;

OracleDataReader dr = executeQuery.ExecuteReader();

lstInstructors.Items.Clear();
lstInstructors.Items.Add(new ListItem("Select All", "%"));

string test = "entering loop";

while (dr.Read())
{
    test = "start reading items";
    lstInstructors.Items.Add(new ListItem(dr.GetValue(0).ToString()));
    test += dr.GetValue(0).ToString();
}

Where is the value for "sql" set?

A different way to accomplish populating populating a list box would be to fill a data table then bind it to the list box.

OracleDataAdapter da = new OracleDataAdapter();
DataTable dt = new DataTable();
var sql = "some sql string";
OracleCommand executeQuery = new OracleCommand(sql, conn);
using (da = new OracleDataAdapter(executeQuery))
{
    da.Fill(dt);
}
lstInstructors.DataSource = dt;
lstInstructors.DataTextField = "Field Name from sql";
lstInstructors.DataValueField = "Field Value from sql";
lstInstructors.DataBind();
lstInstructors.Items.Insert(0, new ListItem("Choose", "Choose"));
lstInstructors.SelectedValue = "Choose";

be sure to include using System.Data

I've adapted this from the SqlDataAdapter so there may be some syntax differences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM