Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Dataset to database

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.

like image 710
Saad Farooq Avatar asked Feb 20 '23 18:02

Saad Farooq


1 Answers

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.

like image 97
KV Prajapati Avatar answered Feb 22 '23 08:02

KV Prajapati