Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Data to SQL

Status
Not open for further replies.

trickytr

Programmer
Joined
May 21, 2005
Messages
6
Location
TR
Hi everyone,

I am getting a data from the user from a textbox and I want to add it to the SQL server but I don't want to use a stored procedure instead codes are on the *.aspx page.
My code is:

INSERT INTO Authors (Id, LName) values(UserName,@txtUserPass )

and error message is:

System.Data.SqlClient.SqlException: Must declare the variable '@txtUserName'


when I use this code with adding a string like below there is no problem

INSERT INTO Authors (Id, LName) values('User','pass' )

but I want to use variables.Is there anyway to do this directly without using stored procedures?

Thanks a lot
 
You can either write the variable to the string i.e.
Code:
Dim strSQL as String = "INSERT INTO Authors (Id, LName) values('" & txtID & "','" & txtLName & ")"
or you can use paramaters (by using Parameters.Add on you data command object.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
By the way, the recommended option is to use parameters (for a variery of reasons).

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top