Results 1 to 5 of 5

Thread: commandtext in cmd.executenonquery

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2003
    Posts
    56

    commandtext in cmd.executenonquery

    Please help me debug.Here is my problem




    Public Function CreateInsertCommand() As SqlCommand
    Dim strsql As String
    strsql = "INSERT INTO MCS_CURRENT ( COMPANYCODE , EMPLOYEENO , COSTCENTER , LEADID , WORKSCHEDULEDATE , JOBNO , OPERATION , LINEHOURS , DATEIN, TIMEIN , DATEOUT , TIMEOUT ,ETCIN , ETCOUT , EQUIPMENTNO , UPDATETIME ) VALUES ( ? ,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) "
    Dim CMD As New SqlCommand(strsql, MyConnection)
    Dim PC As SqlParameterCollection = CMD.Parameters
    PC.Add("COMPANYCODE_NEW", SqlDbType.Int)
    PC.Add("EMPLOYEENO_NEW", SqlDbType.BigInt)
    PC.Add("COSTCENTER_NEW", SqlDbType.BigInt)
    .....
    ......
    PC.Add("UPDATETIME_NEW", SqlDbType.DateTime)
    Return CMD
    End function

    Public Function submitInsert(ByVal row As DataRow, ByVal cmd As SqlCommand) As Integer
    Dim PC As SqlParameterCollection = cmd.Parameters
    PC("COMPANYCODE_NEW").Value = row("COMPANYCODE")
    Console.WriteLine(row("COMPANYCODE"))
    PC("EMPLOYEENO_NEW").Value = row("EMPLOYEENO")
    ....
    .....
    PC("EQUIPMENTNO_NEW").Value = row("EQUIPMENTNO")
    PC("UPDATETIME_NEW").Value = row("UPDATETIME")
    Console.WriteLine(cmd.CommandText)
    Return cmd.ExecuteNonQuery()
    End Function

    When I run this I get an error at the line Return cmd.ExecuteNonQuery() .
    It could be a syntax error but when I console.writeline cmd.Commandtext it gives me the text like
    INSERT INTO MCS_CURRENT ( COMPANYCODE , EMPLOYEENO , COSTCENTER , LEADID , WORKSCHEDULEDATE , JOBNO , OPERATION , LINEHOURS , DATEIN, TIMEIN , DATEOUT , TIMEOUT ,ETCIN , ETCOUT , EQUIPMENTNO , UPDATETIME ) VALUES ( ? ,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)

    and not with the actual values of ? s so that I can run it on the sql query analyser and debug.
    How do I get the commndtext with all the passed parameters values.I do see all the parametercollection getting populated in my submitInsert function above. But it doesnt show up in the command text.
    Please help
    thx

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: commandtext in cmd.executenonquery

    I have always found this type of debugging to be the most painful thing to do. As you know, the problem is somewhere in the SQL string, but once the string gets to a certain size, it is REALLY tough to parse out as a single string.

    While I don't have an answer to your specific question about seeing the string with all the parameters spelled out (though I'd like to see an answer to that), I do have a couple of suggestions.

    1) Step through the code that puts the info into the Query. If you highlight the part to the right of the equals sign and press Shift+F9 (or is it control....), you will see what is being passed in.

    2) This would be tough for your code, but you might try removing pieces of the query to figure out which piece is causing the error.
    My usual boring signature: Nothing

  3. #3
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: commandtext in cmd.executenonquery

    PC.Add("COMPANYCODE_NEW", SqlDbType.Int)
    Does this make a difference?
    VB Code:
    1. PC.Add("[B]@[/B]COMPANYCODE_NEW", SqlDbType.Int)

  4. #4
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: commandtext in cmd.executenonquery

    After a little reading found that @ is required in the SQL string when using SQL Server. Try something like:
    VB Code:
    1. strsql = "INSERT INTO MCS_CURRENT ( COMPANYCODE ,
    2. EMPLOYEENO , COSTCENTER , LEADID , WORKSCHEDULEDATE , JOBNO ,
    3. OPERATION , LINEHOURS , DATEIN, TIMEIN , DATEOUT , TIMEOUT ,ETCIN ,
    4. ETCOUT , EQUIPMENTNO , UPDATETIME ) VALUES (
    5. @COMPANYCODE_NEW ,@Param2,etc) "
    6. Dim CMD As New SqlCommand(strsql, MyConnection)
    7. Dim PC As SqlParameterCollection = CMD.Parameters
    8. PC.Add("@COMPANYCODE_NEW", SqlDbType.Int)
    9. ...

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2003
    Posts
    56

    Re: commandtext in cmd.executenonquery

    Thank you Campster . That fixed it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width