Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling SQL Stored procedure from codebehind problems

Posted on 2012-04-07
5
Medium Priority
?
394 Views
Last Modified: 2012-05-12
hello, I am having a bit problems with this one and I dont know why, it should work.

The stored procedure works fine when I execute, but when I use codebehind to call it, thats where it does not work.

codebehind (vb .net)
Function ssRadioStatistik(ByVal ChannelName As String, ByVal AlbumTitle As String, ByVal TrackTitle As String, ByVal DisplayArtist As String, ByVal StartTime As String)
        Try
            If DisplayArtist.ToLower.Contains("wrd1") = True And DisplayArtist.ToLower.Contains("wrd2") = True Then

'tested with a msgbox to see if it comes to here and it does...

                Dim myConnectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

                Response.Write(myConnectionString)

                Dim myConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(myConnectionString)
                myConnection.Open()
                Try
                    Dim command As SqlClient.SqlCommand = New SqlClient.SqlCommand("ErDetsStoredProcedure", myConnection)
                    command.CommandType = CommandType.StoredProcedure
                    command.Parameters.AddWithValue("@ChannelName", ChannelName)
                    command.Parameters.AddWithValue("@AlbumTitle", AlbumTitle)
                    command.Parameters.AddWithValue("@TrackTitle", TrackTitle)
                    command.Parameters.AddWithValue("@DisplayArtist", DisplayArtist)
                    command.Parameters.AddWithValue("@StartTime", StartTime)
                Finally
                    myConnection.Close()
                End Try

            End If
        Catch ex As Exception
        End Try
    End Function

Open in new window


Stored procedure code(works fine when executed)
ALTER PROCEDURE [Joachim_ErDets].[ErDetBieberStoredProcedure]
	@ChannelName nvarchar(50),
    @AlbumTitle nvarchar(50),
	@TrackTitle nvarchar(50),
	@DisplayArtist nvarchar(50),
	@StartTime datetime
AS

IF NOT EXISTS (SELECT * FROM ErDetsTable WHERE StartTime = @StartTime AND ChannelName = @ChannelName)
BEGIN
    INSERT INTO ErDetsTable (ChannelName, AlbumTitle, TrackTitle, DisplayArtist, StartTime, DatetimeStamp)
    VALUES (@ChannelName, @AlbumTitle, @TrackTitle, @DisplayArtist, @StartTime, GETDATE())
END

Open in new window


it gives no error when I try the use the codebehind to execute it, but it does not write anything in table. (tested the stored procedure by executing it, works great)

Thanks in advance, I have no clue why this does not work.
0
Comment
Question by:JoachimPetersen
  • 2
  • 2
5 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 1000 total points
ID: 37818599
You need to pass StartTime as date time parameter.
Replace line 20:
command.Parameters.AddWithValue("@StartTime", StartTime)
with:
'Add the parameter.
Dim parameter As SqlParameter= command.Parameters.Add("@dt",  System.Data.SqlDbType.DateTime);
'Set the value.
parameter.Value = DateTime.Now;
' Execute the proc
command.ExecuteNonQuery();

Check:
http://stackoverflow.com/questions/425870/using-datetime-in-a-sqlparameter-for-stored-procedure-format-error
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37819699
>thats where it does not work.

Error?
0
 
LVL 14

Expert Comment

by:dejaanbu
ID: 37822414
you are missing

 command.ExecuteNonQuery() 

Open in new window


again if u face issues, add

.....
 Catch ex As Exception
response.write ex.toString()  'For Testing only
        End Try

Open in new window

0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 37825745
I already mentioned command.ExecuteNonQuery() as part of my previous post.
0
 
LVL 14

Expert Comment

by:dejaanbu
ID: 37826334
@mas_oz2003,

Sorry about that..... truly, i didn't read your comment properly..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

877 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