|
-
Jun 9th, 2006, 12:20 PM
#1
Thread Starter
Junior Member
[RESOLVED] Taking input from textbox and pushing to sql database...
Ok im doing this project for work (Im an intern). I am working with a C# webform and one of things i need to do is take information that is input by the user into these textboxes and push that into my sql database. i have been scouring the web and these books i have but i cant find very detailed information. if anyone could point me to a tutorial or maybe give me some sample code, that would be GREAT, thx guyz.
Using:
Visual Studio 2003
Last edited by HardD99; Jun 9th, 2006 at 03:26 PM.
-
Jun 12th, 2006, 01:39 PM
#2
Thread Starter
Junior Member
Re: Taking input from textbox and pushing to sql database...
-
Jun 12th, 2006, 01:56 PM
#3
Hyperactive Member
Re: Taking input from textbox and pushing to sql database...
Why don't you take the data inputted from the textboxes and use SQL to push that information to the database:
e.g.
string SQL = "Insert into table values( ' " +textbox1.text+ " ' )";
Then take the SQL string and execute it to your database.
If the user entered "hi" in textbox1, the SQL will read as:
"Insert into table values( ' hi ' )"
Then that value hi will be inserted in the column.
-
Jun 12th, 2006, 04:07 PM
#4
Thread Starter
Junior Member
Re: Taking input from textbox and pushing to sql database...
thx jennifer, for some reason i was overlooking the obvious, here is what i have now but its not actually pushing to the database, ill post the snippet of where a button is clicked and its supposed to insert to db, let me know if im not giving enuf code
Code:
SqlCommand myInsertCommand = myConnection.CreateCommand();
myInsertCommand.CommandText = "INSERT INTO tblReason ( " + " strReason" + ") VALUES (" + "txtReason.Text" + ")";
myInsertCommand.Parameters.Add("txtReason.Text", SqlDbType.VarChar, 255, "strReason");
BindData();
-
Jun 12th, 2006, 04:32 PM
#5
Re: Taking input from textbox and pushing to sql database...
For one, you are not even executing the myInsertCommand command.
For two, since a user input is involved, you should create a stored procedure in your database first, after which you then call the stored procedure by passing the user's text to it.
Code:
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "MyInsertCommand";
cmd.Parameters.Add("@varname",TextBox1.Text);
cmd.ExecuteNonQuery();
For three, you've put in the name of the textbox in double quotes.
-
Jun 13th, 2006, 10:23 AM
#6
Thread Starter
Junior Member
Re: [RESOLVED] Taking input from textbox and pushing to sql database...
Thx alot for the help guys!
-
Jun 13th, 2006, 01:41 PM
#7
Hyperactive Member
Re: [RESOLVED] Taking input from textbox and pushing to sql database...
Here's a tip. It always work for me. Print out the SQL string without executing it. Like in a textbox and copy that printout and then go to the database, open the query builder and paste the SQL in there. If it does not execute, then something is wrong with the SQL statement itself. I always like fixing SQL queries in the query facility of the database and then copying it back to my code.
mendhak that stored procedure you're talking about, what exactly is that. Are you talking about pl sql?
-
Jun 16th, 2006, 02:23 PM
#8
Re: [RESOLVED] Taking input from textbox and pushing to sql database...
The stored proc I mentioned?
I made it up, Babe.
(I've always wanted to say that )
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|