Solved

Problem with sqlparamater in stored procedure

Posted on 2008-10-24
9
408 Views
Last Modified: 2012-05-05
I have an issue with a SQLParameter in some code I am suing but I can't find the problem.
What I am doing is reading in an xml file, inserting the info into a db table and then moving the images that are referenced in the xml file to enw locations. The error that is being reported is:

import.aspx/InsertPictures: The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects.

I have attached a copy of the XML and code below. I would be grateful if someone could point out the issue for me and explain where I have gone wrong. Thanks

SP

--

CREATE PROCEDURE AddPicture

@PictureName nvarchar(150), @WorkOrderNo int, @IssueNo int, @RoundName nvarchar(75), @OperativeNo int, @InChargeDate smalldatetime, @LocationNo int, @PanelNo int, @WorkDate smalldatetime, @WorkTime smalldatetime, @DesignName nvarchar(4000), @GPSLatitude nvarchar(50), @GPSLongitude nvarchar(50), @MOMSFeedback nvarchar(4000), @Country nvarchar(50)

AS

declare @sql nvarchar(2500)

set @sql = 'INSERT INTO [' + @Country + '] (PictureName, WorkOrderNo, IssueNo, RoundName, OperativeNo, InChargeDate, LocationNo, PanelNo, WorkDate, WorkTime, DesignName, GPSLatitiude, GPSLongitude, MOMSFeedback)

VALUES (@PictureName, @WorkOrderNo, @IssueNo, @RoundName, @OperativeNo, @InChargeDate, @LocationNo, @PanelNo, @WorkDate, @WorkTime, @DesignName, @GPSLatitude, @GPSLongitude, @MOMSFeedback) ' 

exec sp_executesql @sql, N'@PictureName nvarchar(150), @WorkOrderNo int, @IssueNo int, @RoundName nvarchar(75), @OperativeNo int, @InChargeDate smalldatetime, @LocationNo int, @PanelNo int, @WorkDate smalldatetime, @WorkTime smalldatetime, @DesignName nvarchar(4000), @GPSLatitude nvarchar(50), @GPSLongitude nvarchar(50), @MOMSFeedback nvarchar(4000)', @PictureName, @WorkOrderNo, @IssueNo, @RoundName, @OperativeNo, @InChargeDate, @LocationNo, @PanelNo, @WorkDate, @WorkTime, @DesignName, @GPSLatitude, @GPSLongitude, @MOMSFeedback

GO
 
 
 

INSERTPICTURES

--------------

Public Sub insertPictures(ByVal PictureName As String, ByVal WorkOrderNo As Integer, ByVal IssueNo As Integer, ByVal RoundName As String, ByVal OperativeNo As Integer, ByVal InChargeDate As String, ByVal LocationNo As Integer, ByVal PanelNo As Integer, ByVal WorkDate As String, ByVal WorkTime As String, ByVal DesignName As String, ByVal GPSLatitude As String, ByVal GPSLongitude As String, ByVal Feedback As String, ByVal ApplicationLog As String)

        doGlobal.mySQLCmd = New SqlClient.SqlCommand("AddPicture", doGlobal.mySQLConn)

        doGlobal.mySQLCmd.CommandType = CommandType.StoredProcedure
 

        Dim param1 As New SqlClient.SqlParameter("@PictureName", SqlDbType.NVarChar, 150)

        param1.Direction = ParameterDirection.Input

        param1.Value = Trim(PictureName)

        doGlobal.mySQLCmd.Parameters.Add(param1)
 

        Dim param2 As New SqlClient.SqlParameter("@WorkOrderNo", SqlDbType.Int)

        param2.Direction = ParameterDirection.Input

        param2.Value = Trim(WorkOrderNo)

        doGlobal.mySQLCmd.Parameters.Add(WorkOrderNo)
 

        Dim param3 As New SqlClient.SqlParameter("@IssueNo", SqlDbType.Int)

        param3.Direction = ParameterDirection.Input

        param3.Value = Trim(IssueNo)

        doGlobal.mySQLCmd.Parameters.Add(param3)
 

        Dim param4 As New SqlClient.SqlParameter("@RoundName", SqlDbType.NVarChar, 75)

        param4.Direction = ParameterDirection.Input

        param4.Value = Trim(RoundName)

        doGlobal.mySQLCmd.Parameters.Add(param4)
 

        Dim param5 As New SqlClient.SqlParameter("@OperativeNo", SqlDbType.Int)

        param5.Direction = ParameterDirection.Input

        param5.Value = Trim(OperativeNo)

        doGlobal.mySQLCmd.Parameters.Add(param5)
 

        Dim param6 As New SqlClient.SqlParameter("@InChargeDate", SqlDbType.Date)

        param6.Direction = ParameterDirection.Input

        param6.Value = Trim(InChargeDate)

        doGlobal.mySQLCmd.Parameters.Add(param6)
 

        Dim param7 As New SqlClient.SqlParameter("@LocationNo", SqlDbType.Int)

        param7.Direction = ParameterDirection.Input

        param7.Value = Trim(LocationNo)

        doGlobal.mySQLCmd.Parameters.Add(param7)
 

        Dim param8 As New SqlClient.SqlParameter("@PanelNo", SqlDbType.Int)

        param8.Direction = ParameterDirection.Input

        param8.Value = Trim(PanelNo)

        doGlobal.mySQLCmd.Parameters.Add(param8)
 

        Dim param9 As New SqlClient.SqlParameter("@WorkDate", SqlDbType.SmallDateTime)

        param9.Direction = ParameterDirection.Input

        param9.Value = Trim(WorkDate)

        doGlobal.mySQLCmd.Parameters.Add(param9)
 

        Dim param10 As New SqlClient.SqlParameter("@WorkTime", SqlDbType.SmallDateTime)

        param10.Direction = ParameterDirection.Input

        param10.Value = Trim(WorkTime)

        doGlobal.mySQLCmd.Parameters.Add(param10)
 

        Dim param11 As New SqlClient.SqlParameter("@DesignName", SqlDbType.NVarChar, 4000)

        param11.Direction = ParameterDirection.Input

        param11.Value = Trim(DesignName)

        doGlobal.mySQLCmd.Parameters.Add(param11)
 

        Dim param12 As New SqlClient.SqlParameter("@GPSLatitude", SqlDbType.NVarChar, 50)

        param12.Direction = ParameterDirection.Input

        param12.Value = Trim(GPSLatitude)

        doGlobal.mySQLCmd.Parameters.Add(param12)
 

        Dim param13 As New SqlClient.SqlParameter("@GPSLongitude", SqlDbType.NVarChar, 50)

        param13.Direction = ParameterDirection.Input

        param13.Value = Trim(GPSLongitude)

        doGlobal.mySQLCmd.Parameters.Add(param13)
 

        Dim param14 As New SqlClient.SqlParameter("@MOMSFeedback", SqlDbType.NVarChar, 4000)

        param14.Direction = ParameterDirection.Input

        param14.Value = Trim(Feedback)

        doGlobal.mySQLCmd.Parameters.Add(param14)
 

        Try

            doGlobal.mySQLConn.Open()

            doGlobal.mySQLCmd.ExecuteNonQuery()

            doGlobal.mySQLConn.Close()

        Catch ex As Exception

            doGlobal.mySQLConn.Close()

            doGlobal.doError("Database.vb/insertPicture: " & ex.Message.ToString, ApplicationLog)

        End Try

    End Sub

Open in new window

picturesXML.txt
0
Comment
Question by:clearchannel
9 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
Comment Utility
Hello clearchannel,

 23:          doGlobal.mySQLCmd.Parameters.Add(WorkOrderNo)

Should be

 23:          doGlobal.mySQLCmd.Parameters.Add(Param2)

Regards,

TimCottee
0
 
LVL 3

Expert Comment

by:Blackninja2007
Comment Utility
Is your problem that the GPSLatitude contains ' characters in the 2nd Picture block cant remember if this causes as issue with XML

0
 
LVL 10

Assisted Solution

by:jinn_hnnl
jinn_hnnl earned 300 total points
Comment Utility
Those parameters which has type Int, for example:

 Dim param2 As New SqlClient.SqlParameter("@WorkOrderNo", SqlDbType.Int)
        param2.Direction = ParameterDirection.Input
        param2.Value = Trim(WorkOrderNo) '<---- dont use Trim here
        doGlobal.mySQLCmd.Parameters.Add(param2 )


// just like this:
        Dim param3 As New SqlClient.SqlParameter("@IssueNo", SqlDbType.Int)
        param3.Direction = ParameterDirection.Input
        param3.Value = IssueNo
        doGlobal.mySQLCmd.Parameters.Add(param3)

Well I am not sure if this is an issue but you can give it a try.

JINN




0
 

Author Comment

by:clearchannel
Comment Utility
Totally overlooked that! I had them set as nvarchars originally and forgot to take off the formatting!!!
I'll give it a go and see what difference it makes.
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
Also make sure you take care of the param2 as Timmy mentioned.

We will see if you need further assistance

JINN
0
 

Author Comment

by:clearchannel
Comment Utility
Seems it was a culmination of things, although there's still an issue with the field WorkTime...

WorkTime sis et as smalldatetime and the time is coming to me in the format 10:36:02
When it is sinserted into the db it adds todays date at the begiining. How do I stop the SQL db form doing that and leaving just the time???
0
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
How is this field define in your database? So basically you just want the time in that field ?
Is this generic or sometimes this field can contains date?, cause I dont see the point of storing time only. Do you always want to store hours min sec only???

JINN
0
 
LVL 10

Assisted Solution

by:jinn_hnnl
jinn_hnnl earned 300 total points
Comment Utility
SQL contains only 2 type DateTime and SmallDateTime both of them will have date, there is no way you can eliminate the date part.

What we do in our case is we set our field as nvarchar and store the time like: hhmmss so it will be: 211534 (which is 21:15 34sec) we use this number to sort and display the way we want it (in another word: we dont use DateTime type data for this kind of scenarios)

Another solution is keep the SmallDateTime in the database field, let SQL add the date, we ignore the date when we display something.

Hope this helps
0
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
Comment Utility
hi, check for the time column in database, is it datatime data type or is it string, try to make it to time (Transact-SQL) data type
http://msdn.microsoft.com/en-us/library/ms186724.aspx
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

762 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

9 Experts available now in Live!

Get 1:1 Help Now