Solved

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

Posted on 2004-09-07
15
492 Views
Last Modified: 2008-03-06
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

0
Comment
Question by:kwh3856
  • 6
  • 4
  • 3
  • +1
15 Comments
 
LVL 2

Expert Comment

by:rattmman
ID: 12000138
Hi Kenny,

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

RC
0
 
LVL 7

Expert Comment

by:natloz
ID: 12000170
Date should be Dim as DATETIME
0
 

Author Comment

by:kwh3856
ID: 12000176
I am tring to insert as is.  

Thanks
kenny
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 7

Expert Comment

by:natloz
ID: 12000179
Dim admDate As DateTime
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12000225
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12000232
you are also never setting values to your parameters here.
0
 

Author Comment

by:kwh3856
ID: 12000265
Yes...I am trying to use an array.  Is that the right syntax????


Thanks
Kenny
0
 
LVL 2

Accepted Solution

by:
rattmman earned 500 total points
ID: 12000273
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
 

Author Comment

by:kwh3856
ID: 12000274
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
 

Author Comment

by:kwh3856
ID: 12000303
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
 
LVL 2

Expert Comment

by:rattmman
ID: 12000311
OK Kenny,

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

RC

editted: gregoryyoung removed email address.
0
 

Author Comment

by:kwh3856
ID: 12000320
Once again....THANKS!!!!!!!!!!!!!!
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12000342
hmm ... didnt I point out what the issue was ?
0
 
LVL 2

Expert Comment

by:rattmman
ID: 12000353
I agree we should split that point Gregory...

RC
0
 

Author Comment

by:kwh3856
ID: 12000357
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now