开发者

VB.NET Reference a Label's Text in DetailsView

开发者 https://www.devze.com 2023-04-11 06:49 出处:网络
I\'m attempting to reference the text from a label in a DetailsView in my CodeBehind, then pass it through a Stored Procedure.

I'm attempting to reference the text from a label in a DetailsView in my CodeBehind, then pass it through a Stored Procedure.

My DetailsView is DetailsView1, and the two fields are lblDialID & lblCallbackID. I've converted them to TemplateFields. The data is populating properly, but when I'm attempting to grab the data and pass it, I'm not getting any results.

The Stored Procedure is not performing it's action, and a Response.Write on the variables does not produce any results so assumedly I'm just grabbing thin air :)

Any guidance here?

Protected Sub lnkCBtoPUB_Click(ByVal sender As Object, ByVal e As EventArgs)    
Dim Conn As New SqlConnection("Data Source=SERVER;Initial Catalog=DATABASE;User ID=USER;Password=PASSWORD")
Dim MyCommand As New SqlCommand
Dim sDialID As Label = CType(DetailsView1.FindControl("lblDialID"), Label)
Dim sCallbackID As Label = CType(DetailsView1.FindControl("lblCallbackID"), Label)

Conn.Open()
MyCommand.Connection = Conn
MyCommand.CommandType = CommandType.StoredProcedure
MyCommand.CommandText = "spAlterAgentCallback"
MyCommand.Parameters.AddWithValue("@DialID", sDialID.Text)
MyCommand.Parameters.AddWithValue("@CallbackID", sCallbackID.Text)

MyCommand.ExecuteNonQuery()

Co开发者_JAVA百科nn.Close()
Conn.Dispose()
MyCommand.Dispose()
End Sub

Here is the Stored Procedure

ALTER PROCEDURE [dbo].[spAlterAgentCallback]
@DialID AS INT,
@CallBackID AS INT
AS
BEGIN
UPDATE DIAL 
SET CBDATETIME = GETDATE(),
AgentID = '',
CRC = 'SCBR',
Notes = 'This Record Was Recently a Callback for Another Agent.'
WHERE DialID = @DialID

DELETE FROM CALLBACK WHERE CallBackID = @CallBackID
END 

<> Could this also be because I am passing a string, but the SP is taking it as an INT? This shouldn't pose a problem though?

Thank you!


Looks like in your AddWithValue, you need to access the Text Property of the Labels instead of passing the Label object into the proc and you should be okay.

So:

MyCommand.Parameters.AddWithValue("@DialID", sDialID.Text)
MyCommand.Parameters.AddWithValue("@CallbackID", sCallbackID.Text)

If you weren't getting the controls, it would have thrown a NullReferenceException.

EDIT:

As for the SP you need to isolate where the issue is occurring. Try running the SProc from SQL Management Studio giving the parameters that .NET would pass in and confirm the proc is running. If it does work, I would debug your code to make sure the values make it into the parameters. The Integer type shouldn't matter because .NET looks up the type later. If you wanted to be safe, you could parse them into integer's before sending them to the Proc.

EDIT2:

You could try using Hidden Fields in this way:

(Somewhere in your detailsview)

<asp:HiddenField ID="_CtlDialIDHidden" runat="server" Value='<%#Eval("DialID")%>' />
<asp:HiddenField ID="_CtlCallbackIDHidden" runat="server" Value='<%#Eval("CallbackID")%>' />

And in your click event

If Request.Form.AllKeys.Contains("_CtlDialIDHidden") Then
    MyCommand.Parameters.AddWithValue("@DialID",  Request.Form.Item(Request.Form.AllKeys.FirstOrDefault(Function(x As String) x.Contains("_CtlDialIDHidden"))))
End If
If Request.Form.AllKeys.Contains("_CtlCallbackIDHidden") Then
    MyCommand.Parameters.AddWithValue("@CallbackID",  Request.Form.Item(Request.Form.AllKeys.FirstOrDefault(Function(x As String) x.Contains("_CtlCallbackIDHidden"))))
End If

You may have to view source to see exactly what the name being passed back is instead of using the lambda expressions.

0

精彩评论

暂无评论...
验证码 换一张
取 消