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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:19:51+00:00 2026-05-15T17:19:51+00:00

This is how I update a table using DataAdapter and DataSet in VB using

  • 0

This is how I update a table using DataAdapter and DataSet in VB using SQL Server:

sqlStmt = String.Format("INSERT INTO my_table (name, lastname) VALUES ('John', 'Doe')")
ds = New DataSet
da = New SqlDataAdapter(sqlStmt, My.Settings.ConnectionString)
da.Fill(ds)

I know that the Fill method does not make sense in case of an INSERT statement, but I am new to this technology and the above statement does the job and updates the table w/o problems. My question is this: If there was an error (say a duplicate key error) how would I know this in my application? Should I be putting the above code in a try/catch block?

Also, if there is a "proper" method for running INSERT statements using a DataAdapter/DataSet combination that does not use the Fill method, please indicate that as well.

  • 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-15T17:19:51+00:00Added an answer on May 15, 2026 at 5:19 pm

    For update statements, you should use the SqlCommand object.

    SqlCommand cmd = new SqlCommand( "INSERT INTO my_table (name, lastname) VALUES ('John', 'Doe')", My.Settings.ConnectionString);
    
    cmd.ExectureNonQuery();
    

    However it is recommended that you use parameterized SQL queries, if by any chance you are acquiring the data from the user to reduce the chance of SQL Injection attacks 🙂

    SqlCommand cmd = new SqlCommand( "INSERT INTO my_table (name, lastname) VALUES (@FirstName, @LastName)", My.Settings.ConnectionString);
    
    cmd.Parameters.Add("FirstName", SqlDbType.NVarChar);
    cmd.Parameters.Add("LastName", SqlDbType.NVarChar);
    
    cmd.Parameters[0].Value = txtFirstName.Text;
    cmd.Parameters[1].Value = txtLastName.Text;
    
    cmd.ExecuteNonQuery();
    

    Answer to your other question:

    Yes. If there was a primary key violation, a SQLException will be thrown. You can catch it using a try-catch block and show a message or do whatever appropriate.

    try
    {
      cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {      
      MessageBox.Show("Error! " + ex.Message);   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# Update Table using SqlCommand.Parameters I'm trying to update an SQL Server
In SQL Server , it is possible to insert rows into a table with
Does this mean I can't update another table from a trigger if I'm using
I try to update database table and this is my code: public void updatefiletable(String
I'm getting and error using this syntax: update table set field1 = (field1+' -
i'm creating a procedure to update/insert a table using merge statement(upsert).now i have a
I'm trying to update a tuple in a table using php and sql via
I am using INSERT Trigger on that table. Once trigger is executed (it update
Original Question mysql-server-6.0.10 I have this problem, I'm using the COMPRESS function to update
I have a stored procedure that updates a table using linq, eg: (this is

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.