【问题标题】:Get current UserID in ASP.NET and insert into Access database在 ASP.NET 中获取当前 UserID 并插入 Access 数据库
【发布时间】:2014-03-17 11:14:44
【问题描述】:

抱歉,我是一个极端的初学者。我正在尝试让插入查询工作,但将当前 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, 预定日期, 预订时间, 没有人, 用户身份, Facility_Id,

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

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

感谢您的帮助。

更新:

这是我的 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>

【问题讨论】:

  • 您尝试插入的列的数据类型是什么? .Text 将始终返回 String
  • 嗨。 Booking_ID 是“自动编号”。 BookingDate、BookingTime 和 NoOfPeople 是“文本”。 UserId 和 Facility_Id 是 Number。
  • 我知道这有点无关紧要,但是为什么要将 BookingDate 存储为文本而不是日期/时间,将 BookingTime 存储为文本而不是日期/时间,将 NoOfPeople 存储为文本而不是数字?您将无法对您的数据库进行任何有意义的搜索或操作。如果您需要有关 MS Access 支持的列类型的更多信息,请查看此处:office.microsoft.com/en-gb/access-help/…

标签: c# asp.net


【解决方案1】:

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

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

然后将userid 赋值给@UserID 参数。

【讨论】:

  • 我要删除这一行吗:TextBox1.Text = System.Web.HttpContext.Current.User.Identity.Name;解析代码会去哪里?我尝试将其放在其他行下,但仍然在 AccessDataSource2.Insert(); 上收到 OLEDBEXCEPTION 错误;行。
  • 从您的插入查询中,您似乎正在使用参数化查询。您能否发布您为参数赋值的代码?这就是这个解析代码应该存在的地方。
  • 这个代码?
  • 您能否将此数据添加到您的问题而不是评论中?
  • 好的,我已将其添加到问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
相关资源
最近更新 更多