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;
}
}
"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;
}
}