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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:48:31+00:00 2026-06-14T04:48:31+00:00

I am new to SQL (started today itself), how to add data of dataset

  • 0

I am new to SQL (started today itself), how to add data of dataset to the database table when the table in database has the first column as automatic generating numbers i.e., (IDENTITY (1,1))

I have DataSet something like this

  name    age    location

   x      30       yyy
   y      20       ppp

The table in Database is something like this

  Id(auto)   name    age    location

  --------  no data  --------------

First of all I am filling the Dataset from the database table.
Since the first column is autogenerated I cant even add new values to DataSet.
If I say

 dsMainDoctors.Tables[0].Rows.Add( /* first column is autogenerated so I am leaving it*/
                secondColumn,
                thirdColumn,
                fourthColumn,
                FifthColumn);

Error –> Input string was not in a correct format.Couldn’t store in Doctor_ID Column. Expected type is Int32.

The code I am trying is: (When I given the first column value manually in the DataSet)

        SqlConnection conn = new SqlConnection(strConn);
        SqlCommand cmd = new SqlCommand(strQuery, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        conn.Open();
        da.Update(ds.Tables[0]);  //ERROR :- Update requires a valid InsertCommand when passed DataRow collection with new rows.
        conn.Close();
        conn.Dispose();
        cmd.Dispose();

 strQuery --> "delete from [dbo].[" + table_name + "]";

I could have searched this before asking but I dont know what keywords I should use
Please answer in comments if it is very easy question.

  • 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-14T04:48:33+00:00Added an answer on June 14, 2026 at 4:48 am

    Okay, the following code will build that INSERT command properly for you. Also, please note, that if you’re concerned about disposing objects, use the using statement instead because it will call Dispose when exiting the statement.

    SqlConnection conn = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand(strQuery, conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    SqlCommandBuilder builder = new SqlCommandBuilder(da);
    
    using (cmd)
    {
        using (conn)
        {
            conn.Open();
    
            da.InsertCommand = builder.GetInsertCommand();
            da.Update(ds.Tables[0]);  //ERROR :- Update requires a valid InsertCommand when passed DataRow collection with new rows.
    
            conn.Close();
        }
    }
    

    EDIT

    If you’re just wanting to delete the data in the database to rebuild it then that’s quite different than what you’re original question was implying. However, below is how simple that would be.

    var strQuery = "delete from [dbo].[" + table_name + "]";
    SqlConnection conn = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand(strQuery, conn);
    var rowsAffected = cmd.ExecuteNonQuery();
    
    // do something with rowsAffected if you want
    
    // now you can build `insert` statements if you want
    // to put more data into it, which you could use the
    // previous example for that
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started using the new project type SQL Server 2008 Database. I can
I just started using Core Data with sql-lite so I'm a bit new. I
Question: The new SQL Server 2008 database returns me values formatted English (date/float). Is
I need to create an entirely new Sql Server 2008 database and want to
I would like to be able to add a new SQL LOGIN and name
I am a seasoned .NET / SQL Server developer. I just started a new
I have recently started a new job and noticed that all the SQL tables
I have just started a new project using a Linq to Sql model and
Today I have been experimenting with SQL binary objects. I started by storing an
I need to migrate data in a large flat table located in SQL Server

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.