What happens if you open the connection and pass it to the command rather than doing the Command.connection.open?
Here are some steps I have laying around the desktop that might be of use...
1. Open Visual Studio 7 and create a new Visual C# Windows Application.
2. Use the Toolbox to add two Buttons and a DataGrid to the default form.
3. Use the Properties window to change the Text property of Button1 to "Load" and Button2 to "Save".
4. Double-click Button1 to add an event handler.
5. Add the following code at the very top of the code window, following the other "using" statements:
using System.Data.OleDb;
6. Add the following member variable declarations to the Form1 class definition, after the button and grid declarations:
private OleDbConnection con = new OleDbConnection("Provider=sqloledb;Data Source=YourServer;User ID=sa;Password=YourPassword;Initial Catalog=Northwind");
private OleDbDataAdapter da;
private DataSet ds = new DataSet();
7. Add the following code to the Button1_Click event handler:
da = new OleDbDataAdapter("Select * From Customers", con);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(ds, "Cust");
dataGrid1.DataSource = ds.Tables[0];
8. Double-click Button2 to add an event handler:
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.Update(ds, "Cust");
8. Press F5 to compile and run the application.
9. The grid is initially empty. Click the "Load" button to populate the grid.
10. Make a change to several records and click the Load button again. Note what happens to the changes. Do not change the CustomerID field as you will get an exception for
violating referential Integrity on the back-end.
11. Make a change to several records and click the Save button. Close and re-run the application. Re-populate the grid. Are the changes kept?
Want to know more? Check out the MSDN Library at
http://msdn.microsoft.com or the Microsoft Knowledge Base at
http://support.microsoft.com
Scot Rose, MCSD
Microsoft Visual Basic Developer Support
Email :
(E-Mail Removed) <Remove word online. from address>
This posting is provided “AS IS”, with no warranties, and confers no rights.
--------------------
>Content-Class: urn:content-classes:message
>From: "Ken" <(E-Mail Removed)>
>Sender: "Ken" <(E-Mail Removed)>
>Subject: Edit datagrid and make changes to dataset
>Date: Wed, 3 Sep 2003 06:40:46 -0700
>Lines: 40
>Message-ID: <0ae701c37220$facf10a0$(E-Mail Removed)>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Thread-Index: AcNyIPrPbFfKyWLWQ/+V8vzKSVRGHw==
>Newsgroups: microsoft.public.dotnet.framework.windowsforms
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.windowsforms:51556
>NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
>X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
>
>Hello Everyone,
>
>I am now convinced that half a solution is still a lie!
>I have seen countless examples of making changes to the
>datagrid -- this I am able to that without a problem.
>However, I have seen few (less than none ) examples that
>show a "working" (actually works when you use the code)
>example of a System.Windows.Forms.DataGrid where you
>update the grid and the supporting dataset.
>Here is what I have:
>
>private void loadgrid()
>{
>myconn3.ConnectionString = connstr3;
>mysqlcom3.Connection = myconn3;
>mysqlcom3.Connection.Open();
>SqlDataAdapter myad3 = new SqlDataAdapter
>("SHOWPrinterList",myconn3);
>myad3.Fill(mydataset3,"PRINTERLIST");
>dataGrid1.DataSource = mydataset3;
>dataGrid1.DataMember = "PRINTERLIST";
>mycmdbuilder = new SqlCommandBuilder(myad3); ---Here is
>where I am getting green lined
>(incorrect syntax near "ShowPrinterList")
>dataGrid1.ColumnHeadersVisible = true;
>//dataGrid1.SetDataBinding(mydataset3,"PRINTERLIST");
>}
>
>I understand that you should be updating the underlying
>dataset, however , I have not been able to see an example
>that actually works! Nor have I been successful trying to
>do the update myself. I have both 1.0 and 1.1 frameworks
>to work with. Can anybody send me a simple example in C#
>of a Datagrid populated by one table using a stored
>procedure and that can allow an update of both the
>datagrid and the dataset. I would really appreciated your
>help on this
>
>Thank You in Advance for Your Help,
>Ken
>