issue with the insert sql command using vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    issue with the insert sql command using vb.net

    Hi,

    Am trying to connect and insert a record into my database using vb.net this is my code:
    Code:
    cs = New SqlConnection("Data Source=AYACHMOHAMED;Initial Catalog=Employe;Integrated Security=True")
    
            cs.Open()
            sqlcom = New SqlCommand("INSERT INTO EMPLOYE VALUES 3, 'AYACH', 'PDG'", cs)
            Try
                ra = sqlcom.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox("wrong " & ra)
            End Try
            cs.Close()
    the problem is ra = 0 mean no insertion made I can't figure it out!

    thank's in advance
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Your insert syntax is incorrect.

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      OK how about this one:
      Code:
      sqlcom = New SqlCommand("INSERT INTO EMPLOYE (field1, field2...) VALUES (value1, value2, ...)", cs)
      guess what doesn't work either

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Assuming that your table is actually named EMPLOYE and not EMPLOYEE. And assuming your connection string cs is correctly formatted. And assuming your data source AYACHMOHAMED is properly set up.

        If that really is the entire SQL string you tried to use, then that is also incorrect.

        Code:
        sqlcom = New SqlCommand("INSERT INTO EMPLOYE (ActualFieldNameOfIntegerField, ActualFieldNameOfStringField, ActualFieldNameOfOtherStringField) VALUES (3, 'AYACH', 'PDG')", cs)

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #5
          concerning the EMPLOYE is the french version of EMPLOYEE for the rest all of it is ok and guess what I know that there is something wrong with it and this is why I posted the question are you realy trying to help here or just piling up your postes...?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            I'm trying to help but if you really used
            Code:
            INSERT INTO EMPLOYE (field1, field2...) VALUES (value1, value2, ...)
            as your SQL string, of course it's going to fail. You didn't specify the field names or the values to use. You basically copied and pasted the syntax from a SQL help file.

            So either, A) that is not the actual SQL string you used in your code, in which case we can't help you without seeing the actual string you used or B) that is the actual string you used.

            If B, then you either didn't read the rest of the help file or you didn't understand it. So, assuming you did read it, but the text was confusing, I gave you the correct SQL string to use from the minimal information that was provided.

            Which was
            Code:
            sqlcom = New SqlCommand("INSERT INTO EMPLOYE (ActualFieldNameOfIntegerField, ActualFieldNameOfStringField, ActualFieldNameOfOtherStringField) VALUES (3, 'AYACH', 'PDG')", cs)
            All you had to do was replace some of what I had with your field names. However, judging by your most recent post, you did not try my suggestion.

            Comment

            • momotaro
              Contributor
              • Sep 2006
              • 357

              #7
              you didn't even remember my first post you are such a lost cause...! it's realy pitty there was a time when we realy havn't to argue about things like these and when people were realy truly engaged on this site...
              thx anyway

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Your first post had no field names and your first post had incorrect SQL syntax. You looked up the correct syntax, but you didn't replace the values in the syntax with the values that were relevant to your tables. So you ended up with incorrect syntax again. So I gave you the correct syntax in which all you had to to was plug in your field names. But you didn't do that. At no point did you give me all the relevant information that is needed for syntactically correct SQL. Nor did you give the impression that you even tried.

                Comment

                Working...