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

  • Home
  • SEARCH
  • 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 974531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:27:04+00:00 2026-05-16T03:27:04+00:00

I am dealing with the following problem: I use a MSSQL Stored Procedure for

  • 0

I am dealing with the following problem:
I use a MSSQL Stored Procedure for displaying my data in a DataGridView. The Update and Insert Commands work, but there is one problem:

On inserting a new Row, the auto-numbered primary key isn’t send back to my DataAdaptar. So the insertion is successfull in the database, but the PK is left blank in the DataGridView.
I allready tried some codes like:

    private void _rowUpdated(object sender, SqlRowUpdatedEventArgs e)
    {
        if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert)
        {
            cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT @@IDENTITY FROM " + e.Row.Table.TableName;

            DataRow r = dt.Rows[dt.Rows.Count - 1];
            r.ItemArray[0] = cmd.ExecuteScalar();
            //r.SetModified(); --> err?
            r.AcceptChanges();
        }
    }

on the DataAdapter, but nothing seems to work. All the SQL commands work fine.

When I refresh the data in the DataGridView, everyting is perfect. But the problem with this is, that the sort order and column width are adjusted. And that isn’t what I want.

Can someone help me with this problem?

Looking forward for the solutions!

Thanks!

  • 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-16T03:27:05+00:00Added an answer on May 16, 2026 at 3:27 am

    Finally found the answer and wanted to share it:

    dt.RowChanged += new DataRowChangeEventHandler(_update_fields);
    
    
        private void _update_fields(object sender, DataRowChangeEventArgs e)
        {
            try
            {
                if (e.Action == DataRowAction.Add)
                {
                    conn.Open();
                    cmd = conn.CreateCommand();
                    cmd.CommandText = "SELECT IDENT_CURRENT('" + e.Row.Table.TableName + "')";
                    dt.Rows[dt.Rows.Count - 1][0] = int.Parse(cmd.ExecuteScalar().ToString()) + 1;
                    dt.AcceptChanges();
                    conn.Close();
                }
                adapt.Update(dt);
            }
            catch (SqlException ex)
            {
                Debug.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
    

    Hope it will save you some time! 🙂

    Gr
    VeeWee

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

Sidebar

Related Questions

we're dealing with a very slow update statement in an Oracle project. Here's a
Dealing with an employee that went over my head
When dealing with small projects, what do you feel is the break even point
I frequently have problems dealing with DataRows returned from SqlDataAdapters . When I try
I'm dealing with a MySQL table that defines the JobName column as UNIQUE. If
I am dealing with MySQL tables that are essentially results of raytracing simulations on
The table I'm dealing with it potentially larger than available memory (let's say 10GB)
I'm dealing with an issue with my current employer that has seriously made me
I am dealing with a large codebase that has a lot of classes and
I'm dealing with game dialogue files (conversation between player and non-playable-characters) where dialogue choices

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.