Monday, March 26, 2012

Please help

I am completely perplexed as to why the code below is not functioning. Let me clarify that, the first portion of the code below is working just fine, meaning the "For Next" loop, but the code that is supposed to execute the Stored Procedure isn't even firing. I know that the code isn't making the request to the database because I asked our SQL Admin to turn Trace on and watch for the request. I know that my connection string to the Database is fine because I use this code elsewhere without any problems.

Any assistance would be very much appreciated. Thanks.


Public Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'Build list of message recipients
Dim dlItem As DataListItem
Dim Recipients As String
For Each dlItem In dtlAssocList.Items
Dim chbSelAssoc As CheckBox = CType(dlItem.FindControl("chbSelAssoc"), CheckBox)
If chbSelAssoc.Checked = True Then
Dim lblAssocCode As Label = CType(dlItem.FindControl("lblAssocCode"), Label)
Recipients += lblAssocCode.Text + ","
End If
Next dlItem

'Check to see if the SelectAll checkbox is checked.
Dim SelectAll As String
If chbSelectAll.Checked = True Then
SelectAll = "1"
Else
SelectAll = "0"
End If

Dim cnnConnection As System.Data.SqlClient.SqlConnection
Dim connString2 As String = ConfigurationSettings.AppSettings("connString2")
Dim cmdCommand As System.Data.SqlClient.SqlDataAdapter

cnnConnection = New System.Data.SqlClient.SqlConnection(connString2)
cmdCommand = New System.Data.SqlClient.SqlDataAdapter("wpsp_MM_I_Message", cnnConnection)

cmdCommand.SelectCommand.CommandType = CommandType.StoredProcedure

cmdCommand.SelectCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@dotnet.itags.org.msg_title", SqlDbType.VarChar, 50))
cmdCommand.SelectCommand.Parameters("@dotnet.itags.org.msg_title").Value = txbMsgTitle.Text
cmdCommand.SelectCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@dotnet.itags.org.msg_text", SqlDbType.VarChar, 500))
cmdCommand.SelectCommand.Parameters("@dotnet.itags.org.msg_text").Value = txbMessageText.Text
cmdCommand.SelectCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@dotnet.itags.org.sendAll", SqlDbType.Bit, 1))
cmdCommand.SelectCommand.Parameters("@dotnet.itags.org.sendAll").Value = SelectAll
cmdCommand.SelectCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@dotnet.itags.org.AssocList", SqlDbType.VarChar, 4000))
cmdCommand.SelectCommand.Parameters("@dotnet.itags.org.AssocList").Value = Recipients

Response.Redirect("ManagersMessage.aspx")
End Sub

Hmmm, ... where do you execute your command? It seems that you forgot the execution of the command.
You are not aactually doing anything here. You are setting parameters, and then doing nothing. You never open the connection or fill a dataset or anything.

You should debug the application (if using VS.NET) or otherwise, you can use tracing or even Response.Write() to see where you are going.

What are you indending to do?
The 'wpsp_MM_I_Message' Stored Procedure takes the variables that I pass to it and then executes another Stored Procedure (behind the scenes).

What code do I need to include to get the 'wpsp_MM_I_Message' Stored Procedure to execute?
Presuming this returns no records:


Dim cnnConnection As System.Data.SqlClient.SqlConnection
Dim connString2 As String = ConfigurationSettings.AppSettings("connString2")
Dim cmdCommand As System.Data.SqlClient.SqlCommand

cnnConnection = New System.Data.SqlClient.SqlConnection(connString2)
cmdCommand = New System.Data.SqlClient.SqlDataAdapter("wpsp_MM_I_Message", cnnConnection)

cmdCommand.CommandType = CommandType.StoredProcedure

cmdCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@.msg_title", SqlDbType.VarChar, 50))

cmdCommand.Parameters("@.msg_title").Value = txbMsgTitle.Text

cmdCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@.msg_text", SqlDbType.VarChar, 500))
cmdCommand.Parameters("@.msg_text").Value = txbMessageText.Text

cmdCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@.sendAll", SqlDbType.Bit, 1))
cmdCommand.Parameters("@.sendAll").Value = SelectAll
cmdCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@.AssocList", SqlDbType.VarChar, 4000))
cmdCommand.Parameters("@.AssocList").Value = Recipients

cmdCommand.ExecuteNonQuery()

Note cmdCommand is changed into a SqlCommand object rather than a SqldataAdapter

if it returns records, you could either create a SQLDataReader or a Dataset
That's what I needed. Thank you very much.

0 comments:

Post a Comment