Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. C# - Help with preferred method to add a new row

C# - Help with preferred method to add a new row

Scheduled Pinned Locked Moved Database
csharpdatabasehelptutorialquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    work_to_live
    wrote on last edited by
    #1

    Just starting to play around with database access in dotNET. I've run through a number of tutorials, and reviewed a number of articles on this web site. All was well and good until I tried to do something on my own... Uh Oh... I created a simple relational database with the following tables... Table - PhoneNumber PhoneNumber_ID PhoneNumber PhoneNumberTypeID PersonID Table - PhoneNumberType PhoneNumberType_ID PhoneNumberType Table - Person Person_ID FirstName LastName I threw some data in the PhoneNumberType and Person tables, and then wanted to see if I could create a form to add entries to the PhoneNumber table. I added two comboboxes to the form, and bound them to datasets filled with the contents of PhoneNumberType and Person tables. The valuemember of the comboboxes is the ***_ID column of the selected row in the combobox. That's working fine. I added a textbox to the form for the phone number. So, I have all the elements necessary to add a new row to the PhoneNumber table, but I'm not sure of the most efficient method of performing the insert. After poking around on the internet for a while, it looks like there might be two options avaliable to me (there are probably more)... 1) Create a DataAdapter for the PhoneNumber table, then create and fill a dataset with all the phone numbers in the PhoneNumber table. Add the new row and then update the database with the new dataset (need to figure out how to do that). This seems like overkill for this simple function. 2) Create an SQLCommand and execute it with the new row values. I vaguely remember seeing an article that discussed using ?'s in the SQL command, and then filling in the parameters afterwards, but I can't seem to find it again. Is either of these the preferred approach, or is there another approach I might consider. Thanks... whew that was a mouthful.

    W 1 Reply Last reply
    0
    • W work_to_live

      Just starting to play around with database access in dotNET. I've run through a number of tutorials, and reviewed a number of articles on this web site. All was well and good until I tried to do something on my own... Uh Oh... I created a simple relational database with the following tables... Table - PhoneNumber PhoneNumber_ID PhoneNumber PhoneNumberTypeID PersonID Table - PhoneNumberType PhoneNumberType_ID PhoneNumberType Table - Person Person_ID FirstName LastName I threw some data in the PhoneNumberType and Person tables, and then wanted to see if I could create a form to add entries to the PhoneNumber table. I added two comboboxes to the form, and bound them to datasets filled with the contents of PhoneNumberType and Person tables. The valuemember of the comboboxes is the ***_ID column of the selected row in the combobox. That's working fine. I added a textbox to the form for the phone number. So, I have all the elements necessary to add a new row to the PhoneNumber table, but I'm not sure of the most efficient method of performing the insert. After poking around on the internet for a while, it looks like there might be two options avaliable to me (there are probably more)... 1) Create a DataAdapter for the PhoneNumber table, then create and fill a dataset with all the phone numbers in the PhoneNumber table. Add the new row and then update the database with the new dataset (need to figure out how to do that). This seems like overkill for this simple function. 2) Create an SQLCommand and execute it with the new row values. I vaguely remember seeing an article that discussed using ?'s in the SQL command, and then filling in the parameters afterwards, but I can't seem to find it again. Is either of these the preferred approach, or is there another approach I might consider. Thanks... whew that was a mouthful.

      W Offline
      W Offline
      work_to_live
      wrote on last edited by
      #2

      Here's my brute force method... // Load the current phone numbers sqlDataAdapterPhoneNumber.Fill(dataSetPhoneNumber1); // create a new row System.Data.DataRow row = dataSetPhoneNumber1.Tables["PhoneNumber"].NewRow(); // Fill it row["PhoneNumberTypeID"] = comboBoxPhoneTypeID.SelectedValue; row["EntityID"] = comboBoxName.SelectedValue; row["PhoneNumber"] = textBoxPhoneNumber.Text; // Add it to the dataset dataSetPhoneNumber1.Tables["PhoneNumber"].Rows.Add(row); // Update the database (might experiment with GetChanges) sqlDataAdapterPhoneNumber.Update(dataSetPhoneNumber1);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups