Problem with binding data retrieve from oracle DB to textbox

G

Guest

Did anyone know how to bind the data retrieve from Oracle Database to textbox control. I try with following code but it display this error message:

"An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

Additional information: Cannot bind to property or column dName on DataSource." This error indicate this code"txtDept.DataBindings.Add("Text", ds, "dName");"


I call these function when form load.


private void fillDataSet()
{
try
{
staff = new Staff();
da = staff.GetStaffDetail();
ds = new DataSet();
da.Fill(ds,"Staff");
dt = new DataTable("Staff");
dv = new DataView(dt);
dgStaff.DataSource = ds.Tables["Staff"].DefaultView;
cm = (CurrencyManager)(BindingContext[dv]);
ds = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void bindFields()
{
txtDept.DataBindings.Clear();
txtDeptId.DataBindings.Clear();
txtStaffId.DataBindings.Clear();
txtStaffName.DataBindings.Clear();
txtTel.DataBindings.Clear();

txtDept.DataBindings.Add("Text", ds, "dName");
txtDeptId.DataBindings.Add("Text", ds, "dId");
txtStaffId.DataBindings.Add("Text", ds, "sId");
txtStaffName.DataBindings.Add("Text", ds, "sName");
txtTel.DataBindings.Add("Text", ds, "sContact");
}



This is a Staff class which responsible to retrieve data from oracle database.

public class Staff
{
private string connStr = "Provider=MSDAORA;Data Source=HONG;User ID=scott;Password=tiger";
private System.Data.OleDb.OleDbConnection conn;
private System.Data.OleDb.OleDbDataAdapter da;
private System.Data.OleDb.OleDbCommand comm;
private System.Data.OleDb.OleDbDataReader dr;
private string strSQL;
private int maxId;


public OleDbDataAdapter GetStaffDetail()
{
strSQL = "SELECT s.sId, s.sName, s.dId, s.sContact, d.dName " +
"FROM Staff s, Department d WHERE s.dId = d.dId";
conn = new OleDbConnection(connStr);
conn.Open();
da = new OleDbDataAdapter(strSQL, conn);
return da;
}
}
 
G

Guest

First thing is that in fillDataSet you assing ds=null; so when calling txtDept.DataBindings.Add("Text", ds, "dName");" ds is actually null and nothing to bind to.
the second thing is that txtDept.DataBindings.Add("Text", ds, "dName"); wants to bind "dName" property or columng of object ds. But your ds dataset does not have a propery called dName and it is not a table so it does not have a column dName. use
txtDept.DataBindings.Add("Text", ds, "Staff.dName"); (as i understand your table's name is Staff)
or
txtDept.DataBindings.Add("Text", ds.Tables["Staff"], ".dName");
 

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

Top