Using a dropdownlist to insert data into SQL database

Closed
mikeoe2003 Posts 25 Registration date Thursday November 1, 2012 Status Member Last seen March 27, 2014 - Mar 27, 2014 at 02:38 PM
 Blocked Profile - Mar 27, 2014 at 05:26 PM
Hello friends,
Please I am having issues inserting selected values from my dropdownlist into a database. Please note that my dropdownlist is databound to a table in my database(meaning it gets dynamically populated with values from a column in the database). Below is my code

==============================================================
Imports System.Data
Imports System.Data.SqlClient
Public Class Registration_Form
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtDate.Text = DateTime.Now.ToShortDateString
End Sub

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Try

'Dim cmd As New SqlCommand("INSERT INTO Contacts (SurName,FirstName,Gender,Age,Email,Phone,Occupation,State_of_Residence,State_of_Origin,Date) VALUES ('" + txtSurname.Text + "','" + txtFirstname.Text + "','" + txtGender.Text + "','" + txtAge.Text + "','" + txtEmail.Text + "','" + txtPhone.Text + "','" + txtoccupation.Text + "','" + txtResidence.Text + "','" + txtOrigin.Text + "','" + txtDate.Text + "')", con)
Dim cmd As New SqlCommand("INSERT INTO Contacts (SurName,FirstName,Gender,Age,Email,Phone,Occupation,StResidDropDownList,StOrgDropDownList,Date) VALUES ('" + txtSurname.Text + "','" + txtFirstname.Text + "','" + txtGender.Text + "','" + txtAge.Text + "','" + txtEmail.Text + "','" + txtPhone.Text + "','" + txtoccupation.Text + "',' " + StResidDropDownList.SelectedValue + " ',' " + StOrgDropDownList.SelectedValue + " ','" + txtDate.Text + "')", con)

cmd.ExecuteNonQuery()

Catch ex As Exception
con.Close()
End Try
End Sub

End Class
==========================================================
i am sure my problem is with the dropdownlists (StResidDropDownList.SelectedValue and StOrgDropDownList.SelectedValue). Because when I take it off , I can insert values into the database.
Related:

1 response

Blocked Profile
Mar 27, 2014 at 05:24 PM
Ok, well more than likely your drop down list is reporting as the "INDEX" number of the drop down. So, if you are looking for a text input into the DB, you are more than likely getting an integer. The integer is the index of the selection of the list.

Make a debug statement to make certain that you are inserting what you expected.

If that is not it, then try your question again, because basically all you posted whas an SQL statement being programmatically produced from form elements.

SelectedValue property gives you the actual value of the item in selection whereas SelectedItem.Text gives you the display text.
It has been a while!

Let us know!

"If you can't soar with the eagles, then don't fly with the flock!" - Oliver Sykes; Bring Me The Horizon
0
Blocked Profile
Mar 27, 2014 at 05:26 PM
You are also placing an empty space in your string with those two statements.

'_" + StResidDropDownList.SelectedValue + "_','_" + StOrgDropDownList.SelectedValue + "_'

Every place I have put a"_" is where you have coded a space! FYI!
0