Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Calling SQL Stored procedure from codebehind problems

Posted on 2012-04-07
5
386 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 35

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 35

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 58
Nested Case statement 4 37
SQL Server / Update DB? 22 36
Code works but breaks when I add one section 4 20
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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