【发布时间】: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 </td>
<td style="height: 22px">
<asp:TextBox ID="txtDate" runat="server" ontextchanged="txtDate_TextChanged"></asp:TextBox> (dd/mm/yyyy)
</td>
</tr>
<tr>
<td style="width: 134px">Booking Time</td>
<td>
<asp:TextBox ID="txtTime" runat="server"></asp:TextBox> (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">
</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/…