lights
lights

Reputation: 1

Get current UserID in ASP.NET and insert into Access database

Apologies for being an extreme beginner. I'm trying to get an insert query to work but include the current UserId as one of the fields.

From looking online the current code I have on the aspx.cs file:

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;

And on the AccessDataSource insert query on the aspx page:

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

I have three text boxes for user entry, Date, Time and People.

The table I'm trying to insert into looks like this:

Booking_ID, BookingDate, BookingTime, NoOfPeople, UserId, Facility_Id,

When I try to input an entry I get this error:

OLEDBEXCEPTION was unhandled by user code Data mismatch type in criteria expression

I'd appreciate any help thanks.

Update:

Here's the code I have for the aspx page:

 <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>

Upvotes: 0

Views: 844

Answers (1)

shree.pat18
shree.pat18

Reputation: 21757

When you use the TextBox.Text property, the returned value is always a String. Hence, when you try to insert the value for user ID directly as TextBox1.Text, you are trying to insert a String value into a Numeric column. You could try to convert it to an int first, using TryParse method as follows:

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

Then assign userid to the @UserID parameter.

Upvotes: 0

Related Questions