Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to save a dataset to a database. I got a dataset from another class, Now changes will be made on the form by a user on a datagridview, then the changed Dataset needs to be saved in the database.

I am using the below code; Its not generating any errors, but the data is not being saved in the database.

public class myForm    
{    
    DataSet myDataSet = new DataSet();
    public void PouplateGridView()
    {
        try
        {
            SqlService sql = new SqlService(connectionString); // Valid Connection String, No Errors


            myDataSet = sql.ExecuteSqlDataSet("SELECT * FROM Qualification"); // Returns a DataSet
            myDataGridView.DataSource = myDataSet.Tables[0];
            myDataGridView.AutoGenerateColumns = true;
            myDataGridView.AutoResizeColumns();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.InnerException + Environment.NewLine + ex.Message, "Error");
            this.Close();
        }

    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        //myDataSet.AcceptChanges();EDIT:Don't know why, but this line wasn't letting the chane in db happen.
        SqlCommand sc = new SqlCommand("SELECT * FROM Qualification", sql.Connection); //ADDED after Replies
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommandBuilder scb = new SqlCommandBuilder(da); //ADDED after replies
        da.Update(myDataSet.Tables[0]);
    }
}
public class mySqlService
{
public DataSet ExecuteSqlDataSet(string sql)
        {
            SqlCommand cmd = new SqlCommand();
            this.Connect();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            cmd.CommandTimeout = this.CommandTimeout;
            cmd.Connection = _connection;
            if (_transaction != null) cmd.Transaction = _transaction;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;

            da.SelectCommand = cmd;

            da.Fill(ds);
            da.Dispose();
            cmd.Dispose();

            if (this.AutoCloseConnection) this.Disconnect();

            return ds;
        }
}

What am I doing wrong here? There are ways on the web to save the dataset, if the datset is created, edited and saved in the same class etc., BUT I would like to have the select dataset method in the mySqlService class. How should I, now can save the dataset to the database?

EDIT: I have commented the three lines that were required to make the code work. The code works now.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
206 views
Welcome To Ask or Share your Answers For Others

1 Answer

In order to run Update method of SqlDataAdapter you must have to configure InsertCommand, DeleteCommand and UpdateCommand properties along with SelectCommand of SqlDataAdapter or construct the SqlCommandBuilder object which configure these commands implicitly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...