【问题标题】:Using Session variables in InsertCommand attribute of asp:LinkButton在 asp:LinkBut​​ton 的 InsertCommand 属性中使用 Session 变量
【发布时间】:2014-02-26 04:32:18
【问题描述】:

我想使用在代码隐藏中创建的 Session 变量,在实际标签中,主要在 InsertCommand="" 属性中:

这是我的代码隐藏(VB):

   Protected Sub RegSubmitButton_Click(sender As Object, e As EventArgs) Handles RegSubmitButton.Click
    For Me.i = 0 To UNPWListBox.Items.Count
        If (RegUNTextBox.Text = UNPWListBox.SelectedItem.Text) Then
            MsgBox("Username is unavailable. Please choose another username.")
            With RegUNTextBox
                .Text = ""
                .Focus()
            End With
        Else
            username = RegUNTextBox.Text
            password = RegPWTextBox.Text
            UNPWListBox.Items.Insert(UNPWListBox.SelectedIndex + 1, RegUNTextBox.Text)
            'UNPWListBox.Items.Add(username)
            UNPWListBox.SelectedItem.Text.Equals(username)
            UNPWListBox.SelectedItem.Value.Equals(password)
            Session(username) = username
            Exit For
        End If
    Next
    Dim newConnection = CreateObject("ADODB.Connection")

    Dim createTable = "CREATE TABLE [" + Session(username) + "] (ID int, Artist varchar(50), Title varchar(50), Label varchar(25), PressNumber varchar(15), YearReleased varchar(15), NMValue double, Notes string);"
    newConnection.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\allengatorpie\Documents\VBIIT1_DB.accdb")
    newConnection.Execute(createTable)
    Response.Redirect("AddVinyl.aspx")

下面是 ASP.NET 源码:

     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data
 Source=C:\Users\allengatorpie\Documents\VBIIT1_DB.accdb"
 ProviderName="System.Data.OleDb" SelectCommand="SELECT * FROM
 [allengatorpie]" InsertCommand="INSERT INTO [Session(username)] ([Artist], [Title], [Label],         [PressNumber], [YearReleased],
 [NMValue], [Notes]) VALUES (?, ?, ?, ?, ?, ?, ?)">

Session(username) 变量在后面的代码中完美运行,但我需要在 ASP 控件中使用它,以便 SQL 知道将数据插入到哪个表中。

提前致谢。

【问题讨论】:

  • 下面的代码对我有用。我只是决定将其全部放在后端,使用应用程序级别的变量。我将把它改成 Session,这样当用户注销时,它会清理变量。感谢您的任何和所有输入。 Protected Sub InsertButton_Click(sender As Object, e As EventArgs) SqlDataSource1.InsertCommand = "INSERT INTO [" + Application.Item("username") + "] ([Artist], [Title], [Label], [PressNumber], [YearReleased], [NMValue], [Notes]) VALUES (?, ?, ?, ?, ?, ?, ?)" End Sub

标签: asp.net sql vb.net ms-access


【解决方案1】:

可以使用插入参数More details

例子

<asp:sqldatasource
    id="SqlDataSource1"
    runat="server"
    connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
    ......>
      <insertparameters>
        <asp:formparameter name="CoName" formfield="CompanyNameBox" />
        <asp:formparameter name="Phone"  formfield="PhoneBox" />
      </insertparameters>
  </asp:sqldatasource>

【讨论】:

  • 我正在寻找的(说白了)是用我在注册或登录时创建的 Session(username) 变量替换 SQL InsertCommand 中的表名。
猜你喜欢
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多