获取ASP中的当前UserID.并插入到Access数据库中

本文关键字:插入 Access 数据库 ASP 获取 UserID | 更新日期: 2023-09-27 17:50:46

很抱歉作为一个极端的初学者。我试图得到一个插入查询工作,但包括当前UserId作为字段之一。

从网上查看我在aspx.cs文件上的当前代码:

AccessDataSource2.InsertCommand = "INSERT INTO [aspnet_ActivityBooking] ([BookingDate],[BookingTime], [NoOfPeople], [UserId], [Facility_Id]) VALUES (@Date, @Time, @People, @UserId, 1);";
AccessDataSource2.Insert();
AccessDataSource2.DataBind();
txtDate.Text = Profile.Date;
txtTime.Text = Profile.Time;
txtPeople.Text = Profile.People;
TextBox1.Text = System.Web.HttpContext.Current.User.Identity.Name;

对于aspx页面上的AccessDataSource插入查询:

INSERT INTO [aspnet_ActivityBooking] ( [BookingDate], [BookingTime], [NoOfPeople], [UserId], [Facility_Id]) VALUES (@Date, @Time, @People, @UserId, 1)

我有三个文本框用于用户输入:日期、时间和人员。

我要插入的表看起来像这样:

Booking_ID,BookingDate,BookingTime,NoOfPeople,用户标识,Facility_Id,

当我尝试输入一个条目时,我得到这个错误:

oledbexcexception未被用户代码处理条件表达式中的数据不匹配类型

我很感激你的帮助。

更新:

以下是我为aspx页面编写的代码:
 <br />
  <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
<br />
<table style="width: 100%" id="table">
    <tr>
        <td style="width: 134px; height: 22px;">Booking Date&nbsp;</td>
        <td style="height: 22px">
            <asp:TextBox ID="txtDate" runat="server" ontextchanged="txtDate_TextChanged"></asp:TextBox>&nbsp;(dd/mm/yyyy)
        </td>
    </tr>
    <tr>
        <td style="width: 134px">Booking Time</td>
        <td>
            <asp:TextBox ID="txtTime" runat="server"></asp:TextBox>&nbsp;(eg. 22:24 or 10:24pm)
        </td>
    </tr>
    <tr>
        <td style="width: 134px">Number of People</td>
        <td>
            <asp:TextBox ID="txtPeople" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td style="width: 134px">
            &nbsp;</td>
        <td>


            <asp:Button ID="btnClear" runat="server" Text="Clear" />
        </td>
    </tr>
     </table>

  <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
      DataFile="~/App_Data/ASPNetDB.mdb" 
      InsertCommand="INSERT INTO [aspnet_ActivityBooking] ( [BookingDate], [BookingTime], [NoOfPeople], [UserId], [Facility_Id]) VALUES (@Date, @Time, @People, @UserId, 1)" 
      SelectCommand="SELECT aspnet_ActivityBooking.Booking_ID, aspnet_ActivityBooking.BookingDate, aspnet_ActivityBooking.BookingTime, aspnet_ActivityBooking.NoOfPeople, aspnet_ActivityBooking.UserId, aspnet_ActivityBooking.Facility_Id FROM ((aspnet_ActivityBooking INNER JOIN asnet_Facility ON aspnet_ActivityBooking.Facility_Id = asnet_Facility.Facility_ID) INNER JOIN aspnet_Users ON aspnet_ActivityBooking.UserId = aspnet_Users.UserId)">
      <InsertParameters>
          <asp:ControlParameter ControlID="txtDate" Name="Date" PropertyName="Text" />
          <asp:ControlParameter ControlID="txtTime" Name="Time" PropertyName="Text" />
          <asp:ControlParameter ControlID="txtPeople" Name="People" PropertyName="Text" />
          <asp:ProfileParameter Name="UserName" PropertyName="UserId" />
          <asp:ControlParameter ControlID="TextBox1" Name="UserId" PropertyName="Text" />

      </InsertParameters>
  </asp:AccessDataSource>

获取ASP中的当前UserID.并插入到Access数据库中

当您使用TextBox.Text属性时,返回值始终是String。因此,当您尝试将用户ID的值直接插入TextBox1.Text时,您正在尝试将字符串值插入到数字列中。您可以尝试先将其转换为int,使用TryParse方法,如下所示:

int userid = 0;
bool isUserIDInt = int.TryParse(TextBox1.Text, out userid);

userid赋值给@UserID参数