[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
Re: Taking input from textbox and pushing to sql database...
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.
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();
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.
Re: [RESOLVED] Taking input from textbox and pushing to sql database...
Thx alot for the help guys!
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?
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 :D )