VB.NET 在详细信息视图中引用标签文本

发布于 2024-12-08 12:00:17 字数 1449 浏览 0 评论 0原文

我试图从我的 CodeBehind 中的 DetailsView 中的标签引用文本,然后通过存储过程传递它。

我的DetailsView是DetailsView1,两个字段是lblDialID & lblCallbackID。我已将它们转换为 TemplateFields。数据填充正确,但是当我尝试获取数据并传递它时,我没有得到任何结果。

存储过程没有执行它的操作,并且对变量的 Response.Write 不会产生任何结果,所以假设我只是在抓空气:)

这里有什么指导吗?

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()

Conn.Close()
Conn.Dispose()
MyCommand.Dispose()
End Sub

这是存储过程

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 

<> 这是否也是因为我传递了一个字符串,但 SP 将其视为 INT?但这应该不会造成问题吧?

谢谢你!

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()

Conn.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2024-12-15 12:00:17

看起来在您的 AddWithValue 中,您需要访问标签的 Text 属性,而不是将 Label 对象传递到过程中,应该没问题。

所以:

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

如果你没有获得控件,它就会抛出 NullReferenceException。

编辑:

对于 SP,您需要隔离问题发生的位置。尝试从 SQL Management Studio 运行 SPproc,提供 .NET 将传入的参数并确认 proc 正在运行。如果它确实有效,我会调试您的代码以确保这些值包含在参数中。 Integer 类型应该不重要,因为 .NET 稍后会查找该类型。如果你想安全一点,你可以在将它们发送到 Proc 之前将它们解析为整数。

编辑2:

您可以尝试以这种方式使用隐藏字段:(

在您的详细信息视图中的某个位置)

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

并且在您的点击事件中,

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

您可能必须查看源代码才能确切地看到传回的名称是什么,而不是使用拉姆达表达式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文