似乎无法将 guid (userID) 插入表中!

发布于 2024-10-17 00:16:50 字数 2306 浏览 4 评论 0原文

我正在使用 Visual Studio 2010 为我的一个 uni 模块创建一个 Web 项目。 我正在使用 asp.net 会员资格,并且所有 sql-server 表都已正确设置(我很确定)。

我有一个自己创建的表单,它使用 sqlDataSource.insert() 插入到数据库中。最初我遇到了一些问题,根本无法将任何数据插入表中,但我已经解决了这个问题。

我现在的问题是我需要将当前登录用户的 userID 插入表的一列中。但每次我点击提交按钮时,我都会被告知不能在 userID 列中插入空值。

我可以使用以下代码片段检索用户ID:

    Dim userID As String
    Dim memUser As MembershipUser
    memUser = Membership.GetUser()
    userID = memUser.ProviderUserKey.ToString()
    e.Command.Parameters(0).Value = userID

这是页面背后的代码:

Public Class WebForm1
Inherits System.Web.UI.Page

'Dim userID As Guid = CType(memUser.ProviderUserKey, Guid)



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Dim UserID As Guid = CType(Membership.GetUser.ProviderUserKey, Guid)
    'memUser = Membership.GetUser()
    Dim userID As Guid


    Dim memUser As MembershipUser
    memUser = Membership.GetUser()
    userID = memUser.ProviderUserKey

    If IsPostBack() Then
        Session.Add("UserID", userID)
    Else
        userID = Session("UserID")
    End If

    TextBox1.Text = userID.ToString


End Sub
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
    e.Command.Parameters(0).Value = userID

End Sub
Protected Sub buttonInsertCitation_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonInsertCitation.Click


    SqlDataSource1.Insert()
End Sub
End Class

正如您所看到的,它有点混乱,我一直在疯狂地尝试不同的变体,因为我需要赶上其他大学的步伐模块。

这是来自 .aspx 页面的 SQLDataSource,控制参数对于我插入的其他数据运行良好。也许 是错误的?

<InsertParameters>
        <asp:parameter name="userID" />
        <asp:ControlParameter ControlID="textNotes" Name="notes" PropertyName="Text" />
        <asp:ControlParameter ControlID="textEssayTitle" Name="essayTitle" PropertyName="Text" />
        <asp:ControlParameter ControlID="radioIsPrivate" Name="isPrivate" PropertyName="Text" />
        <asp:ControlParameter ControlID="dropPaperID" Name="paperID" PropertyName="Text" />
</InsertParameters>

我对 vb 比较陌生,我来自 html & CSS 背景。

I'm creating a web project for one of my uni modules using Visual studio 2010.
I'm using asp.net membership and have all of my sql-server tables set up correctly (I'm pretty sure).

I have a form that I have created myself that inserts into the database using sqlDataSource.insert(). I had some problems initially with not being able to insert any data into the table at all, but I have cleared that up.

My problem now is that I need to insert the currently logged in user's userID into one of the columns of the table. But every time I hit the submit button, I am told I cannot insert null values in the userID column.

I can retrieve the userID using this snippet:

    Dim userID As String
    Dim memUser As MembershipUser
    memUser = Membership.GetUser()
    userID = memUser.ProviderUserKey.ToString()
    e.Command.Parameters(0).Value = userID

This is the code behind for the page:

Public Class WebForm1
Inherits System.Web.UI.Page

'Dim userID As Guid = CType(memUser.ProviderUserKey, Guid)



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Dim UserID As Guid = CType(Membership.GetUser.ProviderUserKey, Guid)
    'memUser = Membership.GetUser()
    Dim userID As Guid


    Dim memUser As MembershipUser
    memUser = Membership.GetUser()
    userID = memUser.ProviderUserKey

    If IsPostBack() Then
        Session.Add("UserID", userID)
    Else
        userID = Session("UserID")
    End If

    TextBox1.Text = userID.ToString


End Sub
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
    e.Command.Parameters(0).Value = userID

End Sub
Protected Sub buttonInsertCitation_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonInsertCitation.Click


    SqlDataSource1.Insert()
End Sub
End Class

As you can see it is a bit of a mess, I've been frantically trying different variations, as I need to catch up a little with other uni modules.

Here is my SQLDataSource from the .aspx page, The controlparameters work fine for the other data I am inserting. maybe the <asp:parameter name="userID" /> is wrong?

<InsertParameters>
        <asp:parameter name="userID" />
        <asp:ControlParameter ControlID="textNotes" Name="notes" PropertyName="Text" />
        <asp:ControlParameter ControlID="textEssayTitle" Name="essayTitle" PropertyName="Text" />
        <asp:ControlParameter ControlID="radioIsPrivate" Name="isPrivate" PropertyName="Text" />
        <asp:ControlParameter ControlID="dropPaperID" Name="paperID" PropertyName="Text" />
</InsertParameters>

I'm relatively new to vb, and I come from a html & css background.

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

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

发布评论

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

评论(1

司马昭之心 2024-10-24 00:16:50

使用 ASP.NET 成员资格提供程序,您无需担心执行任何 SQL。

With the ASP.NET membership providers, you don't need to worry about performing any sql.

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