User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 456,528 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,803 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 6523 | Replies: 2
Reply
Join Date: Oct 2007
Posts: 3
Reputation: punkazz is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
punkazz's Avatar
punkazz punkazz is offline Offline
Newbie Poster

NEWBIE - Insert data from ASP.Net application into mutiple SQL tables

  #1  
Oct 1st, 2007
Hi everyone! I am very new to programming and SQL. I have created (designed) an application for a school project; that now needs to be coded to be functional. I have been able to pull data from SQL without a problem; however, I am stuck on inserting data into sql.

Brief synopsis of app (since i have no code for insert) a user selects a "request" type; based on that different fields appear on the page and the user has to fill in SEVERAL different fields which need to feed into SEVERAL different tables within my SQL Database. On top of this, many of the tables PK needs to auto increment when a new record is added.

Can someone please point me in the write direction? Where do I start? I am writing this in C# and need help with the code on that side along with the insert commands.

Thank you!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: NEWBIE - Insert data from ASP.Net application into mutiple SQL tables

  #2  
Oct 1st, 2007
Here's 2 sample ways to do it (I used two tables, each of which have a PK and either an int or an nvarchar column):
1) Have several INSERT commands that, when the form is submitted, each pick out their own data from the form and submit them. The code would look something like this:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (Page.IsPostBack) // form submitted
  4. {
  5. SqlConnection sc = null;
  6. SqlCommand command = null;
  7. try
  8. {
  9. sc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\inetpub\wwwroot\foo\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
  10. sc.Open();
  11. string query1 = String.Format(@"INSERT INTO Numbers(number) VALUES({0})",
  12. numberInput.Text);
  13. command = new SqlCommand(query1, sc);
  14. command.ExecuteNonQuery();
  15. string query2 = String.Format(@"INSERT INTO Strings(string) VALUES('{0}')",
  16. stringInput.Text);
  17. command = new SqlCommand(query2, sc);
  18. command.ExecuteNonQuery();
  19. }
  20. finally // clean up
  21. {
  22. if(command != null)
  23. command.Dispose();
  24. if(sc != null)
  25. sc.Close();
  26. }
  27. }
  28. }

2) Have a stored procedure in the database that does all the inserts for you. The code would look kind of like this:
C#:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (Page.IsPostBack) // form submitted
  4. {
  5. SqlConnection sc = null;
  6. SqlCommand command = null;
  7. try
  8. {
  9. sc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\inetpub\wwwroot\foo\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
  10. sc.Open();
  11. command = new SqlCommand("InsertData", sc);
  12. command.CommandType = CommandType.StoredProcedure;
  13. command.Parameters.AddWithValue("@number", numberInput.Text);
  14. command.Parameters.AddWithValue("@string", stringInput.Text);
  15. command.ExecuteNonQuery();
  16. }
  17. finally // clean up
  18. {
  19. if(command != null)
  20. command.Dispose();
  21. if(sc != null)
  22. sc.Close();
  23. }
  24. }
  25. }
  26.  
SQL:
  1. CREATE PROCEDURE dbo.InsertData
  2. (
  3. @number INT,
  4. @string NVARCHAR(50)
  5. )
  6. AS
  7. INSERT INTO Numbers(number)
  8. VALUES(@number);
  9.  
  10. INSERT INTO Strings(string)
  11. VALUES(@string);
  12.  
  13. RETURN

Note that I didn't validate my data in either of these. Also, the connection string should be in web.config, but I was too lazy to add it. Also note that using a SProc (stored procedure) requires EXECUTE permissions for your database, which, while you probably have it, isn't always the case. The sproc only requires one query to the database though, so it's better peforming; it's also "more secure" but validation is always a good idea (better yet, lack thereof is always a bad idea).

Oh, and for the auto increment property, MSSQL (aka T-SQL) uses the IDENTITY(start, increment) property in the column definition. If you're using Visual Studio, you can also open the table definition, and for the column properties for your PK set the identity property as well.

Another note about my code (especially if you're new to ASP.NET): the condition at the beginning checks to make sure the page is being reached from a form submission. It doesn't necessarily validate that it's from the same page (you can postback to a different URL), and I didn't code it to reset the form.

Hope this gets you started
Reply With Quote  
Join Date: Oct 2007
Posts: 3
Reputation: punkazz is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
punkazz's Avatar
punkazz punkazz is offline Offline
Newbie Poster

Re: NEWBIE - Insert data from ASP.Net application into mutiple SQL tables

  #3  
Oct 4th, 2007
THANK YOU! that was great help! I did it just a bit different but couldn't have done it without your response...now i get to go work on pulling the data back
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 4:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC