Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8824905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:44:52+00:00 2026-06-14T06:44:52+00:00

Hi i am working on creating sqlconnectivity class and i come across a problem

  • 0

Hi i am working on creating sqlconnectivity class and i come across a problem when i tried to update the whole dataset after editing it in datagridview. Can Please anyone sugguest why i cant seem to abel to update my dataset. Here is my Class code which i used to fetch datset and update dataset.

public class clssql
{

    #region private sqlvariabels

    private static string _sqlconnectionstring;
    private SqlDataAdapter _sqldataadapter;
    private SqlCommand _sqlcommand;
    private SqlConnection _sqlconnection;
    private SqlCommandBuilder _sqlcommandbuilder;
    private SqlTransaction _sqlTransaction;
    private DataSet _ds;
    private bool _Transctionbinded=false;
    private string _errorstring;
    #endregion
    /// <summary>
    /// Constructor for clssql
    /// </summary>
    public clssql()
    {
        _sqlconnectionstring = GenerateSQLConnectionString(false);
        _sqlconnection = new SQLConnection(_sqlconnectionstring);
    }
    /// <summary>
    /// BindsTransaction to the Object
    /// </summary>
    public void BindTransaction()
    {
        _Transctionbinded = true;
        _sqlconnection.Open();
        _sqlTransaction = _sqlconnection.BeginTransaction();
        _sqlcommand.Transaction = _sqlTransaction;
    }


    /// <summary>
    /// Releases the transaction
    /// </summary>
    public void ReleaseTransaction()
    {
        if (_errorstring=="")
        {
            _Transctionbinded = false;
            _sqlTransaction.Commit();
            _sqlconnection.Close();

        }
            else
        {

        }
        _sqlTransaction = null;
    }


    /// <summary>
    /// Generate sqlconnection string.
    /// </summary>
    /// <param name="hardcoded">set to true if you want hardcoded string and not from app.config generated</param>
    /// <returns></returns>
    public static string GenerateSQLConnectionString(bool hardcoded = false)
        {
            StringBuilder connstring = new StringBuilder();

            if (hardcoded)
            {
                return @"Data Source=MAYA-PC\SQLExpress;Initial Catalog=BankDB;Persist Security Info=True;User ID=sa;Password=sql123";
            }
                else
            {
                try
                {
                    connstring.Append("Data Source=" + ConfigurationManager.AppSettings["DataSource"] + ";");
                    connstring.Append("Initial Catalog=" + ConfigurationManager.AppSettings["InitialCatalog"] + ";");
                    connstring.Append("Persist Security Info=True;");
                    connstring.Append("User ID=" + ConfigurationManager.AppSettings["UserID"] + ";");
                    connstring.Append("Password=" + ConfigurationManager.AppSettings["Password"] + ";");
                    return connstring.ToString();
                }
                catch (Exception)
                {
                    throw;
                }

            }

        }

  /// <summary>
  /// Executes an SQL Command Text.
  /// </summary>
  /// <param name="Query">Query to execute</param>
  /// <returns>Returns the Number of Rows Affected</returns>
    public int ExecuteNonQuery(string Query)
    {
        try
        {
            int rec;
            if (_Transctionbinded)
            {
                //Do Nothing
            }
                else
            {
                _sqlconnection.Open();
            }
            //_sqlconnection = new SqlConnection(_sqlconnectionstring);
            _sqlcommand.CommandText = Query;
            _sqlcommand.CommandType = CommandType.Text;
            rec = _sqlcommand.ExecuteNonQuery();

            if (_Transctionbinded)
            {
                //Do Nothing
            }
            else
            {
                _sqlconnection.Close();
            }
            return rec;
        }
        catch (Exception ex)
        {
            if (_Transctionbinded)
            {
                _errorstring = ex.Message;
            }
            else
            {
                _sqlconnection.Close();
            }
            throw;
        }
    }

    /// <summary>
    /// Directly Executes an SQL Command
    /// </summary>
    /// <param name="_sqlcommand"></param>
    /// <returns></returns>
    public int ExecuteCommand(SqlCommand _sqlcommand)
    {
        try
        {
            int rec;
            if (_Transctionbinded)
            {
                //Do Nothing
            }
                else
            {
                _sqlconnection.Open();
            }

            _sqlcommand.Connection = _sqlconnection;
            rec =_sqlcommand.ExecuteNonQuery();

            if (_Transctionbinded)
            {
                //Do Nothing
            }
            else
            {
                _sqlconnection.Close();
            }
            return rec;
        }
        catch (Exception ex)
        {
            if (_Transctionbinded)
            {
                _errorstring = ex.Message;
            }
            else
            {
                _sqlconnection.Close();
            }
            throw;
        }

      }

    /// <summary>
    /// Get Dataset From Query
    /// </summary>
    /// <param name="SelectQuery"></param>
    /// <returns></returns>
    public DataSet GetDataset(string SelectQuery)
    {
        try
        {

            if (_Transctionbinded)
            {
                //Do Nothing
            }
            else
            {
                _sqlconnection = new SqlConnection(_sqlconnectionstring);
                _sqlconnection.Open();
            }

            _sqlcommand = new SqlCommand(SelectQuery, _sqlconnection);
            _sqldataadapter = new SqlDataAdapter(_sqlcommand);
            _sqlcommandbuilder = new SqlCommandBuilder(_sqldataadapter);
            _ds = new DataSet();
            _sqldataadapter.Fill(_ds);

            if (_Transctionbinded)
            {
                //Do Nothing
            }
            else
            {
                _sqlconnection.Close();
            }

            return _ds;
        }
        catch (Exception)
        {

            throw;
        }

    }


    /// <summary>
    /// Updates Dataset
    /// </summary>
    /// <param name="ds"></param>
    /// <returns></returns>
    public int UpdateDataset(DataSet ds)
        {

            int rec;
            try
            {
                if (_Transctionbinded)
                {
                //Do Nothing
                }
            else
                {
                _sqlconnection.Open();
                }

                if (ds.HasChanges()) { 
                    ds.AcceptChanges(); 
                }
                _sqldataadapter.UpdateCommand = _sqlcommandbuilder.GetUpdateCommand(true);
                _sqldataadapter.InsertCommand = _sqlcommandbuilder.GetInsertCommand(true);
                _sqldataadapter.DeleteCommand = _sqlcommandbuilder.GetDeleteCommand(true);
                rec = _sqldataadapter.Update(ds);

                if (_Transctionbinded)
                {
                    //Do Nothing
                }
                else
                {
                    _sqlconnection.Close();
                }

                return rec;
            }
            catch (Exception ex)
            {
                if (_Transctionbinded)
                {
                    _errorstring = ex.Message;
                }
                else
                {
                    _sqlconnection.Close();
                }
                throw;
            }

        }

    }

Well i have putted my whole class here but The getdataset and update dataset are the only one that matters no need for transctions right now just for future refrences.

In the same Project when i tried this code the after editing datagridview the dataset was updated correctly. i tried and tried but dont understand why it works on the below code and doesnt work on above code..

 private void LoadClick(object sender, EventArgs e)
    {
        string connectionString = @"Data Source=MAYA-PC\SQLExpress;Initial Catalog=CrystalTutorial;Persist Security Info=True;User ID=sa;Password=sql123";
        string sql = "Select * From Customer_Orders";
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        sCommand = new SqlCommand(sql, connection);
        sAdapter = new SqlDataAdapter(sCommand);
        sBuilder = new SqlCommandBuilder(sAdapter);
        sDs = new DataSet();
        sAdapter.Fill(sDs, "Stores");
        sTable = sDs.Tables["Stores"];
        connection.Close();
        dataGridView1.DataSource = sDs.Tables["Stores"];
        dataGridView1.ReadOnly = true;
        btnsave.Enabled = false;
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    }

    private void new_btn_Click(object sender, EventArgs e)
    {
        dataGridView1.ReadOnly = false;
        btnsave.Enabled = true;
        btnnew.Enabled = false;
        btndelete.Enabled = false;
    }

    private void delete_btn_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
            sAdapter.Update(sTable);
        }
    }

    private void save_btn_Click(object sender, EventArgs e)
    {
        sAdapter.Update(sTable);
        dataGridView1.ReadOnly = true;
        btnsave.Enabled = false;
        btnnew.Enabled = true;
        btndelete.Enabled = true;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:44:53+00:00Added an answer on June 14, 2026 at 6:44 am

    You should not call the AcceptChanges method before the Update. You will find the explanation to this in the Order of using AcceptChanges and TableAdapter.Update thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on creating a private messaging system, (PHP/MySQL) in which users can
I'm working on creating a Hibernate query engine that can create dynamic queries. Thus
I am working on creating a class to handle very large numbers for use
I'm working on creating a class file called MyClass - it works fine when
I'm working on creating PDF files in PHP with the R&OS PDF class (
I am working on creating a webpage with animated images randomly positioned. My problem
I'm working creating rails application and wondered where I can find good tutorials on
I'm currently working on creating my own blog section.. After brainstorming a little while
I'm working on creating a COM callable wrapper for the System.Net.NetworkInformation.Ping class for use
I am working on creating an MVC framework, which passes any url parameters after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.