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 7404255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:13:49+00:00 2026-05-29T05:13:49+00:00

Okay guys, After many readings and finding solutions down here I just need to

  • 0

Okay guys,

After many readings and finding solutions down here I just need to ask this.
I already searched for it but it didn’t help me as good as it should to sort this out.
Also, excuse me for my grammatical mistakes if any…

What’s the problem?
I load up a WPF form with a combobox in. This combobox gets all the names from all the tables in my database. I choose a table name and press a button to fill up a DataGrid (WPF) with the chosen table. This all works perfectly. But, then when I changed a cell or added/deleted a row or column I have to update this to the database.
This is where I’m stuck. I got it working trough a not so optimal way. So that’s why I ask if there is a better solution.

//fill the datagrid with data from chosen table in combobox!            
selectedTable = cboxTables.SelectedItem.ToString();
dataset = db.getDataFromTable(selectedTable);
datatable = dataset.Tables[selectedTable];
datagridAdmin.ItemsSource = datatable.DefaultView;

And when the selection in the DataGrid has changed, I set the ‘Submit’ button as active wich calls this code:

db.updateTable(selectedTable, datatable);

Note that ‘db’ is a instance off my databaseclass. The method is as following:

public bool updateTable(String tableName, DataTable datatable)
{
    using (SqlBulkCopy bulkcopy = new SqlBulkCopy(thisConnection.ConnectionString, SqlBulkCopyOptions.CheckConstraints))
    {
        //Set destination table name
        //to table previously created.
        bulkcopy.DestinationTableName = tableName;

        try
        {
            thisCommand = new SqlCommand("DELETE FROM " + tableName, thisConnection);
            thisCommand.ExecuteNonQuery();
            bulkcopy.WriteToServer(datatable);
        }
        catch (Exception ex)
        {
            logger.WriteLine(applicationName, ex.Message);
        }
    }
}

But the problem is, the first column is a auto increment ID wich keeps raising every time I submit the changed DataGrid.
Isn’t there a better way to do this?

Thanks!

PS: I’m coding in Visual Studio 2010 with C# in WPF.

  • 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-05-29T05:13:50+00:00Added an answer on May 29, 2026 at 5:13 am

    In short for the method you have given you would be better off using the Truncate table method before calling your bulk copy, this will reset the Identity column back to 0:

    thisCommand = new SqlCommand("TRUNCATE TABLE " + tableName, thisConnection);
    

    However either method is going to cause problems with foreign keys in your database. I am not sure how you are retrieving your data in your database class, but I would look at using the SqlCommandBuilder to update your database rather than deleting and re-inserting all the data with each update.

    EDIT

    To further explain my suggestion of the SqlCommandBuilder.

    A ViewModel such as the following should allow use of the SqlCommandBuilder (Note: this is hugely cut down and in real terms this requires better validation, exception and event handling, but the rough idea is there):

    public class YourViewModel
    {
        private SqlDataAdapter adapter;
        private DataTable table;
        private SqlCommandBuilder commandBuilder;
        public DataTable Table { get { return table; } set { table = value; } }
        public void LoadTable(string connectionString, string tableName, int[] primaryKeyColumns)
        {
            adapter = new SqlDataAdapter(string.Format("SELECT * FROM {0}", tableName), connectionString);
            table = new DataTable();
            adapter.Fill(table);
            List<DataColumn> primaryKeys = new List<DataColumn>();
            for (int i = 0; i < primaryKeyColumns.Length; i++) 
            {
                 primaryKeys.Add(table.Columns[primaryKeyColumns[i]]);
            }
            table.PrimaryKey = primaryKeys.ToArray();
            commandBuilder = new SqlCommandBuilder(adapter);
            commandBuilder.GetUpdateCommand();
        }
        public int Update()
        {
            if (table == null || table.Rows.Count == 0 || adapter == null) 
            {
                 return 0;
            }
            if (table.PrimaryKey.Length == 0)
            {
                throw new Exception("Primary Keys must be defined before an update of this nature can be performed");
            }
            else
            {
                return adapter.Update(table);
            }
        }
    }
    

    Bind the Table property to the grid view, then call the Update method when required. You can even bind the update to the table’s rowchanged events etc to automatically update the db. The Code Project has a fairly good article on WPF, Data grids and database integration.

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

Sidebar

Related Questions

Okay, before you guys go nuts -- this is just a small site, temporary
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay. I know this looks like the typical Why didn't he just Google it
The eclipse and checkstyle guys of you will surely now this problem: After organizing
Okay guys, I'm sure this has a very simple solution but my searches are
Okay guys, maybe you can help me out with this one. I'm about ready
Hye guys. Okay. I have done this coding. But it seems have error. Can
Okay, here goes. Stack Overflow virgin here but hopefully you guys will be able
hey guys, I got some difficulty here. It is purely coding algorithm problem. Okay,
Hey guys. I have some difficulty in creating a filename. okay, here is what

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.