Showing posts with label inserting. Show all posts
Showing posts with label inserting. Show all posts

Monday, March 26, 2012

Trouble inserting items from a dropdownlist into a database

Hi I'm relatively new to asp and I'm having a bit of trouble on one of my projects.

Basically I can't work out how to insert an item, that's been selected from a dropdownlist, into a table. I tried:

string queryString; queryString = "Insert INTO Details(Gender) VALUES(";

queryString += "'" + list1.SelectedItem.Value + "')";

However this didn't seem to work and I was wondering what bit of code I had to change to make it insert the selected item from the dropdownlist (list1)

Thanks!

Try list1.SelectedValue.


You should also use parameters instead of concatenating to the end of your SQL string. This leaves your application open for SQL injection attacks.

sivilian

Tuesday, March 13, 2012

Troubles with INSERT INTO Statement...

hey all, im currently trying to do an insert statement, using textbox's and a variable. the only thing is its inserting txtTextBox.Text into the db instead of the value of that textbox...and i get this error when trying to insert my variable...

Exception Details: System.Data.SqlClient.SqlException: The name 'RndmNum' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.

Source Error:

Line 42: dbQuery = "INSERT INTO table (name, fault_type, fault_location, fault_description, fault_number) VALUES ('txtName.Text', 'txtType.Text', 'txtLocation.Text', 'txtDesc.Text', RndmNum)"
Line 43: dbCommand = New SqlCommand(dbQuery,dbConnection)
Line 44: dbCommand.ExecuteNonQuery()
Line 45: dbConnection.Close
Line 46:

this is my insert statement:

dbQuery = "INSERT INTO table (name, fault_type, fault_location, fault_description, fault_number) VALUES ('txtName.Text', 'txtType.Text', 'txtLocation.Text', 'txtDesc.Text', RndmNum)"

thanks everyone,
JustinYou are sending txtName.Text as a string. Your dbQuery needs to look like this:

dbQuery = "INSERT INTO table (name, fault_type, fault_location, fault_description, fault_number) VALUES ('" & txtName.Text & "', '" & txtType.Text & "', '" & txtLocation.Text & "', '" & txtDesc.Text & "', " & RndmNum & ")"