Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 17th, 2005, 09:14 AM
Authorized User
 
Join Date: Aug 2005
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default INSERT Data to SQL Database

How do I insert data from a input textbox or textarea into MS SQL database table
 
Old September 17th, 2005, 09:26 PM
Authorized User
 
Join Date: Jul 2005
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

string insertSQL, yourConnectionString;
int numberRecAdded;

insertSQL = " insert into ..... "; // your insert sql string
yourConnectionString = ".... "; // your connection string

SqlConnection dbConn = new SqlConnection(yourConnectionString);
SqlCommand cmd = new SqlCommand(insertSQL, dbConn);

try{
  dbConn.Open();
  int numberRecAdded = cmd.ExecuteNonQuery();
}

catch(Exception err){

}

 
Old September 18th, 2005, 10:46 AM
Authorized User
 
Join Date: Aug 2005
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't see where the code takes value from a text box ex. <input type="text" ID="Fname" name="Fname" value="" runat="server">
I have on my page several input fileds ex. register pages. I have on page fields user id, PWD and e-mail. All the filed values should insert into sql database.

 
Old September 18th, 2005, 12:55 PM
Authorized User
 
Join Date: Jul 2005
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will put onclick attribute inside the definition of input box. In your case,
<input type="text" ID="Fname" name="Fname" value="" runat="server" onclick="Fname_Click">

Then, put whatever insert logic inside the the function Fname_Click such as

protected void Fname_Click(object sender, EventArgs e){
 ... your insert SQL code ...
}

 
Old September 20th, 2005, 03:50 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Assuming your form has a submit button on it, and assuming it's an ASP.NET server control, not a basic html submit button, and assuming your using Visual Studio.NET, just double click on the button in the Form Designer, which will take you into code view, and create a btnSubmit_Click subroutine for you where btnSubmit is the ID of your button.

Then, inside the procedure write some code like this.

Code:
dim cnxn as new SQLConnection
dim cmd as new SQLCommand
cnxn.ConnectionString = "<your connection string goes here>"
cnxn.open
cmd.Connection = cnxn
Now we get to where there are two different methodologies for doing the same thing. The simpler, faster way is to say something like...

Code:
cmd.CommandText = "INSERT INTO <tableName> (<field1>,<field2>,<etc>) VALUES ('" & Me.Fname.Text & "', '" & Me.Lname.Text & "', '" & Me.<whateverControlName>.Text & "')"
cmd.ExecuteNonQuery
cmd.dispose
cnxn.dispose
In the above example, I treated all three fields as text type fields, and so the single quote was needed. It's not needed for numbers, except dates. Also, the Me. part is optional, I just use it because intellisense brings up a list of my controls and I can do less typing.

The problem with the above method is that any single quotes/apostrophes in the user's input will cause an error, since single quotes are used by SQL as a delimeter, so if I enter my address as "Aaron's house", for example, your app will crash. In reality, this is easy to check for, just do a replace for each textbox, and replace one single quote with two, e.g.
Code:
fname.text = fname.text.Replace("'", "''")
A bigger problem with the above method is that it leaves you exposed to a type of hack called a SQL Injection Attack, whereby a hacker types in "OR 1=1" into one of your textboxes and gets access to restricted information.

A safer, preferred method is to use SQL parameters, which eliminates both problems. So the first part would look the same, but then you'd add:

With cmd.Parameters
   .Add (New SqlParameter ("@FirstName", Fname.Text))
   .Add (New SqlParameter ("@LastName", Lname.Text))
   .Add (New SqlParameter ("@MiddleInitial", MI.Text)) 'for example
End With

Then your INSERT statement changes to:

[code]cmd.CommandText = "INSERT INTO <tableName> (<field1>,<field2>,<etc>) VALUES (@FirstName, @LastName, @MiddleInitial)"[code]

As you can see, the code is also a whole lot cleaner.

Hope that helps!

Aaron






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert text file in sql server database sharvari_mothe Java Databases 3 June 5th, 2007 07:25 AM
INSERT Data to SQL Database Lofa ASP.NET 1.0 and 1.1 Basics 1 February 22nd, 2006 03:09 AM
Check before insert data into sql Alcapone C# 2 October 26th, 2004 07:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.