Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT:</p> <p>Inserting the <code>UserID</code> into the <code>Tickets</code> table when adding a record first requires that you have access to the <code>UserID</code> value. You then need to pass this value in your <code>INSERT</code> statement. </p> <p>Looks like we first need to retrieve the UserId. Since you are using <code>FormsAuthentication</code> we can retrieve the UserName from the <code>User.Identity.Name</code> object and use that as the value in our first query to retrieve the UserId.</p> <pre><code> Dim cmdS As String = "Select [UserID] from Users Where Deleted='N' AND UserName=@UserName" Dim cmdGetUserId As New SqlCommand(cmdS, sConnection) Dim cmd As New Data.SqlClient.SqlParameter("@UserName", Data.SqlDbType.VarChar) cmdGetUserId.Parameters.Add("@UserName", SqlDbType.VarChar) cmdGetUserId.Parameters.Item("@UserName").Value = User.Identity.Name Dim obj As Object = cmdGetUserId.ExecuteScalar Dim myUserId As Integer = Integer.Parse(obj) </code></pre> <p>Now that we have the UserId value for our current user we can modify our <code>INSERT</code> statement and parameters.</p> <pre><code> cmdInsertTicket.Connection = conticket cmdInsertTicket.CommandText = "INSERT INTO Ticket " _ &amp; "(UserID, DateCreated, FullName, Email, TicketType, Subject, Message, Deleted)" _ &amp; "Values(@UserID, @DateCreated, @FullName, @Email, @TicketType, @Subject, @Message, @Deleted)" Dim appUserId As New Data.SqlClient.SqlParameter("@UserID", Data.SqlDbType.Int) cmdInsertTicket.Parameters.Add(appUserId) cmdInsertTicket.Parameters.Item("@UserID").Value = myUserId Dim appDateCreated As New Data.SqlClient.SqlParameter("@DateCreated", Data.SqlDbType.DateTime) cmdInsertTicket.Parameters.Add(appDateCreated) cmdInsertTicket.Parameters.Item("@DateCreated").Value = Now() ... Dim appDeleted As New Data.SqlClient.SqlParameter("@Deleted", Data.SqlDbType.Char) cmdInsertTicket.Parameters.Add(appDeleted) cmdInsertTicket.Parameters.Item("@Deleted").Value = "N" </code></pre> <p>You can access authentication information through the <code>User.Identity</code> object once the user has been authenticated. Might also want to think about implementing a custom <code>IIdentity</code> class to store the UserID if you will need access to it often. Here's a good MSDN article about Custom Authentication: <a href="http://msdn.microsoft.com/en-us/library/ms172766(v=vs.80).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms172766(v=vs.80).aspx</a></p> <p>UPDATE:</p> <p>In regards to the comment below, you are retrieving the <strong>UserID</strong> because the <code>SqlCommand</code> is being executed with the <code>ExecuteScalar</code> method which returns the value of the first column of the first row. I would recommend taking a closer look at the <code>SqlCommand</code> object: <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx</a> and this ADO.NET primer on MSDN: <a href="http://msdn.microsoft.com/en-us/library/e80y5yhx(v=vs.80).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/e80y5yhx(v=vs.80).aspx</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload