【发布时间】: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