Writting to a database

Discussion in 'ASP' started by CaJack, May 8, 2007.

  1. CaJack

    CaJack New Member

    Joined:
    Mar 21, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I have a database with a table called CDTest and the fields CDName, CDPrice, DateAdded and UName. I’m having trouble with my code, I cant quite figure out whats wrong and I was hoping someone could help. Its supposed to take CDIDString and find a match in the database and allow the user to update CDPrice and UName. Here’s the code…

    Code:
    Dim cnn As Data.SqlClient.SqlConnection
    Dim cmd As Data.SqlClient.SqlCommand
    Dim param As Data.SqlClient.SqlParameter
    Dim strSQL As String
    strSQL = "Insert Into CDTestTBL(CDPrice, UName) Values(@CDPrice, @UName) WHERE CDID = '" & CDIDString & "'"
    Dim conn As String
    conn = "Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\aspnetdb.mdf"";Integrated Security=True;User Instance=True"
    
    cnn = New Data.SqlClient.SqlConnection(conn)
    cmd = New Data.SqlClient.SqlCommand(strSQL, cnn)
    
    param = New Data.SqlClient.SqlParameter("@CDPrice",Data.SqlDbType.VarChar)
    param.Value = CDPriceString
    cmd.Parameters.Add(param)
                
    param = New Data.SqlClient.SqlParameter("@UName",Data.SqlDbType.VarChar)
    param.Value = UNameString
    cmd.Parameters.Add(param)
    
    cnn.Open()
    cmd.ExecuteNonQuery()
    cnn.Close()
    
    An error appears when I run the code saying “Incorrect syntax near the keyword 'WHERE'.” and points to…
    Code:
     cmd.ExecuteNonQuery() 
    Could anyone lend a hand?
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Where have you defined CDIDString?? An empty CDIDString has caused this..
     
  3. CaJack

    CaJack New Member

    Joined:
    Mar 21, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    I fixed it now. You right I hadnt passed anything to CDIDString. I've also changed the SQL statement from insert to update.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice