Solved

Problem with sqlparamater in stored procedure

Posted on 2008-10-24
9
413 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
ID: 22795195
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
ID: 22795218
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
ID: 22795234
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:clearchannel
ID: 22795240
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
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22795334
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
ID: 22796545
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
ID: 22796635
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
ID: 22796723
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
ID: 22796762
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

807 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