Solved

Calling SQL Stored procedure from codebehind problems

Posted on 2012-04-07
5
387 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 250 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

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)

Question has a verified solution.

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

Suggested Solutions

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

679 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