C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,095 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,002 people online right now. Registration is fast and FREE... Join Now!




Save a DataGridView

 

Save a DataGridView

Braindead

30 Jun, 2009 - 07:34 AM
Post #1

New D.I.C Head
*

Joined: 21 Jun, 2009
Posts: 10


My Contributions
Hi @ all lookaround.gif

Well, I have created a database, created a DataSet, filling the
DataSet and binding it to a DataGridView and ..... it works fine icon_up.gif .

But now I would like to save the information from this DataGridView
back into the database file.

Here is my code:

CODE

SqlCeConnection conn = new SqlCeConnection("Data Source = c:\\Test.sdf");

SqlCeDataAdapter DA = new SqlCeDataAdapter("SELECT * FROM Test", conn);
      
DA.Update(DataSet, "Test");
      


Error appears:

Update requires a valid UpdateCommand when passed DataRow collection with modified rows

Any help? rolleyes.gif

This post has been edited by Braindead: 30 Jun, 2009 - 08:39 AM

User is online!Profile CardPM
+Quote Post


reCoded

RE: Save A DataGridView

30 Jun, 2009 - 10:25 AM
Post #2

D.I.C Regular
***

Joined: 25 Feb, 2008
Posts: 270



Thanked: 5 times
My Contributions
Why are you useing SqlCeConnection and not SqlConnection?

This might be what you are looking for.
http://quickstart.developerfusion.co.uk/qu...DataFromDB.aspx
User is offlineProfile CardPM
+Quote Post

Braindead

RE: Save A DataGridView

30 Jun, 2009 - 10:54 AM
Post #3

New D.I.C Head
*

Joined: 21 Jun, 2009
Posts: 10


My Contributions
Because my little tool needs only to run on a desktop system.
So the user don't need to install a sql-server or similar.
It's just for the data connection to the database file.


Now this is my code, but the IDE shows another error

CommandText property has not been initiliazed

CODE
SqlCeConnection conn = null;
conn = new SqlCeConnection(ConnString);
SqlCeCommand cmd = conn.CreateCommand();
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
SqlCeCommandBuilder CB = new SqlCeCommandBuilder(da);
da.Update(Dataset, "Test");


This post has been edited by Braindead: 30 Jun, 2009 - 11:10 AM
User is online!Profile CardPM
+Quote Post

b.ihde

RE: Save A DataGridView

30 Jun, 2009 - 11:13 AM
Post #4

D.I.C for life
Group Icon

Joined: 29 Sep, 2008
Posts: 979



Thanked: 30 times
Dream Kudos: 75
My Contributions
Hi!

You could use a bindingsource... It s pretty easy then..

CODE


private void SaveButton1_Click(object sender, Eventags e)
{
this.Validate();
                this.bindingSource1.EndEdit();
                this.yourTableAdapter.Update(this.yourDataSet.YourTable);
}


Hope that helps!

regards

Ben
User is offlineProfile CardPM
+Quote Post

modi123_1

RE: Save A DataGridView

30 Jun, 2009 - 11:19 AM
Post #5

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,835



Thanked: 80 times
Dream Kudos: 150
My Contributions
QUOTE(Braindead @ 30 Jun, 2009 - 12:54 PM) *

Because my little tool needs only to run on a desktop system.
So the user don't need to install a sql-server or similar.
It's just for the data connection to the database file.


Now this is my code, but the IDE shows another error

CommandText property has not been initiliazed

CODE
SqlCeConnection conn = null;
conn = new SqlCeConnection(ConnString);
SqlCeCommand cmd = conn.CreateCommand();
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
SqlCeCommandBuilder CB = new SqlCeCommandBuilder(da);
da.Update(Dataset, "Test");




Sorry bud, but SQLCeConnection is for Windows CE OS and mobile. SQLConnetion is part of the framework so there will not be a needed install. So you are using this for a mobile device?

you need to set your DataAdapter's UpdateCommand to the your command builder.

For example (I am pulling this from the SQLDataAdpater example on MSDN)
CODE

    // Create the UpdateCommand.
    command = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);

    // Add the parameters for the UpdateCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
    SqlParameter parameter = command.Parameters.Add(
        "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.UpdateCommand = command;


You are missing the last line.

This post has been edited by modi123_1: 30 Jun, 2009 - 11:20 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 11:42AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month