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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New needs help with dropdown list

Status
Not open for further replies.

sirnose1

Programmer
Nov 1, 2005
133
US
I have a dropdown list named ddllst. I want to save the ID in tblLogentry. How do do this? I know there is a problem with my code:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration


Partial Class logentry
Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Dim connectionstring = WebConfigurationManager.ConnectionStrings("LMConnectionString").ConnectionString
Dim AddSQL As String


AddSQL = "INSERT INTO tblLogBook (techInit,timeIn,date_submit,time_submit,entry,areaID)VALUES("
AddSQL &= "'" & Me.txtInit.Text & "',"
AddSQL &= "'" & Now.Date.ToString & "', "
AddSQL &= "'" & Me.txtCurrDate.Text & "',"
AddSQL &= "'" & Me.txtCurrTime.Text & "',"
AddSQL &= "'" & Me.txtEntry.Text & "',"
AddSQL &= "'" & Me.ddlLst.UniqueID & "')"


Dim con As New SqlConnection(connectionstring)
Dim cmd As New SqlCommand(AddSQL, con)

Dim added As Integer = 0

Try
con.Open()
added = cmd.ExecuteNonQuery()

Me.lblResults.Text = added.ToString() & "Records Inserted"

Catch ex As Exception

Me.lblResults.Text = "Error inserting record"

End Try




End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not (Page.IsPostBack) Then

Dim militaryTimeFormat As String = "HH:mm"
Me.txtCurrDate.Text = String.Format("{0:d}", Now())
Me.txtCurrTime.Text = DateTime.Now.ToString(militaryTimeFormat)

Me.ddlLst.DataTextField = "area"
Me.ddlLst.DataValueField = "areaID"

End If

End Sub


End Class
 
Hi,

your insert code maybe need to change.

Code:
AddSQL &= "'" & Me.ddlLst.UniqueID & "')"

should probably be

Code:
AddSQL &= "'" & Me.ddlLst.selectedItem.Value & "')"

this is untested and i havnt done any .Net for a bit so give it a try.

Rob

---------------------------------------
 
Also, have you heard of SQL Injection? If not, I suggest you read up on it or someone could quite easily destroy your database if you use the code above.


____________________________________________________________

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