0

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
3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by mail2saion
-1

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)
Votes + Comments
please dont post the same question in 3 different posts
0

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.

0

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

Edited by sknake: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.