Syntax Error using objCommand.Parameters.Add......attempting to write data to sql database

I am trying to write a record to  an SQL database.  I thought I have correctly delcared my variables but the system is giving a syntax error on the admDate line.  It is telling me to define the variable, but I have already defined it.  The admDate is actually Dim as a String variable....Would this create my problem?????

Any help is greatly appreciated.


Dim admDate As String

Try
                'Write ELA Objective Info record
                Dim objConnection As SqlConnection = New SqlConnection("server=(local);database=FBISD-TAKS;user id=sa;password=pa55word")
                Dim objCommand As SqlCommand = New SqlCommand
                objCommand.Connection = objConnection

                Dim r4 As Int32
                For r4 = 1 To 51
                    objCommand.CommandText = "Insert Into itemAnalysisDetail " & "(admDate, studentId, teacherId, objectiveNumber, itemCorrectResp, studentResp) " & _
                                "VALUES(@admDate,@studentId,@teacherId,@readingElaobj(r4),@readingElaItemCorrectRespArray(r4),@readingElaStudentRespArray(r4))"
                    objCommand.Parameters.Add("@admDate", SqlDbType.DateTime, 10)    <<<------- Problem Line..I Think!
                    objCommand.Parameters.Add("@studentId", SqlDbType.Int, 4)
                    objCommand.Parameters.Add("@teacherId", SqlDbType.Int, 4)
                    objCommand.Parameters.Add("@readingElaobj", SqlDbType.VarChar, 2)
                    objCommand.Parameters.Add("@readingElaItemCorrectRespArray", SqlDbType.Int, 4)
                    objCommand.Parameters.Add("@readingElaStudentRespArray", SqlDbType.Int, 4)
                    objConnection.Open()
                    objCommand.ExecuteNonQuery()
                    objConnection.Close()
                Next
            Catch ex As Exception
                MsgBox(ex.Message)

            End Try

Thanks
Kenny

kwh3856Asked:
Who is Participating?
 
rattmmanConnect With a Mentor Commented:
Hey Kenny,

  Gregory has a point on those lines.  The values you want are the parameter names that you are adding, so you would want to assighn the values when you create the parameters.

  So Instead of This:

     "VALUES(@admDate,@studentId,@teacherId,@readingElaobj(r4),@readingElaItemCorrectRespArray(r4),@readingElaStudentRespArray(r4))"

   You'll want

      "VALUES @admDate,@studentId,@teacherId,@readingElaobj,@readingElaItemCorrectRespArray,@readingElaStudentRespArray)"

  Then when you create your parameters you'll need to go

objCommand.Parameters.Add("@admDate", SqlDbType.DateTime, 10).value = [VariableName]

  if it is in an array you'll need

objCommand.Parameters.Add("@admDate", SqlDbType.DateTime, 10).value = [variableName].(index)


0
 
rattmmanCommented:
Hi Kenny,

Are you trying to hit a stored procedure? or just trying to run that insert as is?

RC
0
 
natlozCommented:
Date should be Dim as DATETIME
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
kwh3856Author Commented:
I am tring to insert as is.  

Thanks
kenny
0
 
natlozCommented:
Dim admDate As DateTime
0
 
gregoryyoungCommented:
the problem is ...

"VALUES(@admDate,@studentId,@teacherId,@readingElaobj(r4),@readingElaItemCorrectRespArray(r4),@readingElaStudentRespArray(r4))"

the (r4)s are you trying to access an array there ?
0
 
gregoryyoungCommented:
you are also never setting values to your parameters here.
0
 
kwh3856Author Commented:
Yes...I am trying to use an array.  Is that the right syntax????


Thanks
Kenny
0
 
kwh3856Author Commented:
I thought I was setting the values to the memory variables I had set previously in the program.  Does it not work this way??

Thanks
Kenny
0
 
kwh3856Author Commented:
Thank you both for turning on so many lights.  I have been trying to understand the syntax and how this works for about 3 to 4 days now.  All the tutorials and Wrox books I have read never really made this clear.  Let me go make so major changes to my code.   Again   THANKS FOR YOUR HELP


Thanks
Kenny
0
 
rattmmanCommented:
OK Kenny,

If you need any help you could email me at ********************

RC

editted: gregoryyoung removed email address.
0
 
kwh3856Author Commented:
Once again....THANKS!!!!!!!!!!!!!!
0
 
gregoryyoungCommented:
hmm ... didnt I point out what the issue was ?
0
 
rattmmanCommented:
I agree we should split that point Gregory...

RC
0
 
kwh3856Author Commented:
Yes.....I wanted to split the points but did not see the button. I will post the question again so you can submit your answer.  Again...thank you very much.

Kenny
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.