I just keep getting the same error. can not insert record. Problem near keyword "where".

Imports System.Security
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Data


Partial Class admin
    Inherits System.Web.UI.Page
    Public connectPath As String = ConfigurationSettings.AppSettings("Maya")


    Protected Sub fillDropList(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            DropList1.DataSource = Membership.GetAllUsers()
            DropList1.DataBind()
        End If
    End Sub

    Protected Sub DropList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropList1.SelectedIndexChanged
        Dim theUser2 As Integer = names.SelectedDataKey.Value
        Dim con As New SqlConnection
        con.ConnectionString = connectPath

        'the customerID of the selected gridview row


        Dim setRep As New SqlCommand
        setRep.Connection = con
        setRep.CommandText = "INSERT INTO dbo.customers(rep)VALUES(@dropList1)WHERE customerID = @ID"
        setRep.Parameters.AddWithValue("@dropList1", DropList1.SelectedItem.ToString())
        setRep.Parameters.AddWithValue("@ID", gridView1.SelectedDataKey.Value)

        Dim added As Integer = 0
        Try
            con.Open()

            added = setRep.ExecuteNonQuery()

            results.Text = added.ToString() & "record has been added!"



        Catch Err As Exception
            results.Text = "Error inserting record. "
            results.Text &= Err.Message
        Finally
            con.Close()
        End Try


    End Sub


End Class

Recommended Answers

All 4 Replies

This is probably so simple but i just cant seen to figure it out. Im trying to insert the dropList1.selected.value into the column named "rep" in the dbo.customers table where customerID= the customer id of my gridview.

evidently there is a problem near key word "where" in my sql command text. Even when i try hard coding values such as

INSERT INTO dbo.customers(rep) VALUES(some text) WHERE customerID = 48

I still get an error. what am i doing wrong?????
thanks in advance
Rick Pace

Dim setRep As New SqlCommand
        setRep.Connection = con
        setRep.CommandText = "INSERT INTO dbo.customers(rep)VALUES(@dropList1)WHERE customerID =@ID"
        setRep.Parameters.AddWithValue("@dropList1", DropList1.SelectedValue.ToString())
        setRep.Parameters.AddWithValue("@ID", gridView1.SelectedDataKey.Value)
commented: please dont post the same question in 3 different posts -1

Which error you get?? What is your CustomerID field? Does it autogenerated? If not then check that you may miss must have field values. Please post your error.

That is invalid SQL and normally I would post the correct SQL but I have no clue what you're trying to do.

Valid SQL is simply:

Insert Into dbo.customers (col1, col2) Values (@val1, @val2)

Are you trying to UPDATE the table instead of INSERT maybe?

Update dbo.Customers
Set 
col1 = @val1,
col2 = @val2
where id = @id

OHHHHH Yes please read basic concept on writing SQL. In Insert statement why where clause??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.