help with BindingSources

BobLewiston

In Runtime
Messages
182
I need some help with BindingSources (in C#).

This is what I'm working on:

From the C# tutorial at Programmer's Heaven (http://www.programmersheaven.com/2/Les_CSharp_13_p9):

private void btnLoadData_Click(object sender, System.EventArgs e)
{
string connectionString = "server=P-III; database=programmersheaven; uid=sa; pwd=;";
SqlConnection conn = new SqlConnection (connectionString);
string cmdString = "SELECT * FROM article";
SqlDataAdapter dataAdapter = new SqlDataAdapter (cmdString, conn);
DataSet ds = new DataSet ();
dataAdapter.Fill (ds, "article");
dgDetails.SetDataBinding (ds, "article");
}

When I try to compile this, I get the following error: "The name 'dgDetails' does not exist in the current context".

I have just been told that dgDetails is a BindingSource. I'm a newbie, so I'm not familiar with BindingSources.

Could you tell me what I need to include in my code to get the compiler to recognize the dgDetails BindingSource and compile my app?
 
I've never heard of dgDetails. However, all you need to do to bind to a datagrid control is to use it's DataSource property.
Also, using the dataAdapter is an extra step.

Try this instead:

Code:
string connectionString = "server=P-III; database=programmersheaven; uid=sa; pwd=;";
    SqlConnection conn = new SqlConnection (connectionString);
    string cmdString = "SELECT * FROM article";
    SqlCommand cmd = new SqlCommand(cmdString,conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr,LoadOption.Upsert);
conn.Close();
MyDataGridControl.DataSource = dt;
 
The above will definitely work.

Just to clarify so there is no confusion, I believe dgDetails is a Data Grid control that was created and defined earlier in code.
 
Back
Top Bottom