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

时间:2014-03-17 11:14:44

标签: c# asp.net

道歉是一个极端的初学者。我试图让插入查询起作用,但将当前的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, 预定日期, BookingTime, NoOfPeople, 用户身份, 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>

1 个答案:

答案 0 :(得分:0)

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

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

然后将userid分配给@UserID参数。