在asp:LinkBut​​ton的InsertCommand属性中使用Session变量

时间:2014-02-26 04:32:19

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

我想在代码隐藏中使用在实际标记中创建的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(用户名)变量在后面的代码中完美地工作,但我需要在ASP控件中使用它,以便SQL知道将数据插入哪个表。

提前致谢。

1 个答案:

答案 0 :(得分: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>