Question
Need in C# - C SHARP Problem 1 Create the log application The first problem is to create a log application that you will later
Need in C# - C SHARP
Problem 1 Create the log application
The first problem is to create a log application that you will later use with the GridView to show information about your visitors and the log file. You have already created the log table and the three stored procedures in a previous assignment.
Step 1. Insert the data in the log table when the session starts
Modify the Global.asax code file
In the first part you will do what you have done in the previous assignments. Again there are many ways to do this but I will give you one way to help guide you. Feel free to customize the code.
Modify the global code file and locate the session starts event handler.
Create a variable for the connection string to your database connection string.
Create the connection object and pass the connection string.
Create the command object and pass the connection and the name of the stored procedure, InsertLogSessionStart.
Set the command type to stored procedure.
Step 2. Create the parameters and assign them values.
Use what you learned in previous weeks to retrieve the session ID, session variables, HTTP server variables, the date and time, cookies and other data, that are collected in the Log table. (Hint: Look in the book and lessons for these topics! This is a great review for the final exam!)
I provided the first two for you. You need one for each column in the table, except logid because that is your identity field that is autonumbered. Notice we don't know the customer ID so we passed null as the value. Look back in your notes for how to retrieve the server variables and other objects.
cmd.Parameters.AddWithValue("@customerid", null); cmd.Parameters.AddWithValue("@authuser", Request.ServerVariables["AUTH_USER"]);
You may stumble on a few that you can do some additional processing. I'll help you with those. The session ID is tricky because it needs to be a string data type. One way to implicitly convert it to a string is to add the "" at the end of the line.
string sessionIDModified = HttpContext.Current.Session.SessionID + ""; cmd.Parameters.AddWithValue("@sessionid", sessionIDModified);
If you think a field might be null, such as the referrer, you can pass either null or the value. Don't forget in the original stored procedure we also passed null as the default value just as a backup. So you don't need to do this on every column!
if (Request.ServerVariables[" "] == null) { cmd.Parameters.AddWithValue("@referrer", null); } else { cmd.Parameters.AddWithValue("@referrer", Request.ServerVariables["HTTP_REFERER"]); }
The last problem is with the Response.Cookies collection, because more than one value is possible. Remember this code? It's the same one you used earlier. We just changed the line breaks to (;) so that the entire value is stored as a string.
if (Request.Cookies == null) { cmd.Parameters.AddWithValue("@cookie", null); } else { HttpCookieCollection MyCookieColl = Request.Cookies; HttpCookie MyCookie; string mycookielist = ""; String[] AllCookieKeys = MyCookieColl.AllKeys; for (int i = 0; i < AllCookieKeys.Length; i++) { MyCookie = MyCookieColl[AllCookieKeys[i]]; mycookielist += "Cookie: " + MyCookie.Name + ";"; mycookielist += "Secure: " + MyCookie.Secure + ";"; String[] MyCookieValues = MyCookie.Values.AllKeys; //Loop through cookie Value collection and print all values. for (int j = 0; j < MyCookieValues.Length; j++) { mycookielist += "Value: " + j + ": " + Server.HtmlEncode(MyCookieValues[j]) + ";"; } } cmd.Parameters.AddWithValue("@cookie", mycookielist); }
The other parameters are easy, if you follow the example in the authuser example at the top of the page and refer to your class notes and book. After that, you just have a few lines. Use the Try-Catch structure and open the connection, execute the command with ExecuteNonQuery (because it's an insert command), catch the exception and in the finally section just close the connection. It's the same code You have written in the other homework, so it should be straight forward.
Step 3. Update the data in the log table when the session ends
In the second part you will update just two fields in the Log table.
Modify the global code file and locate the session ends event handler.
Create a variable for the connection string to your database connection string.
Create the connection object and pass the connection string.
Create the command object and pass the connection and the name of the stored procedure, UpdateLogSessionEnd.
Set the command type to stored procedure.
Step 4. Create the parameters and assign them values.
Create the four input parameters
Retrieve the value for logid parameter from the Session["LogID"] variables.
Retrieve the customerid parameter from the AUTH_USER server variable.
Retrieve the session ID from the Session objects' SessionID property.
Retrieve the datevisitended using the DateTime and retrieving the data and time for today. Time does matter here to make sure to include the time stamp too.
Use the Try-Catch structure and open the connection, execute the command with ExecuteNonQuery (because it's an update command), catch the exception and in the finally section just close the connection. You may assign the a variable to the returned value from the ExecuteNonQuery command if you want but it's not essential here because the user has logged off. Did you see the pattern?
Step 5. Retrieve the log values to test the program.
We can look at the log table in Visual Studio and also write a program to test to see if we can retrieve the logid. You will need the logid as a session variable for the UpdateLogSessionEnd stored procedure.
At the bottom of the footer, inside the footer tags in the site.master page, add a label called lblSession.
Modify the code file of the site master page, and locate the page load event handler. Inside the function, but at the bottom of the function, add the code just as you did with the other activities.
Create a variable for the connection string to your database connection string.
Create the connection object and pass the connection string.
Create the command object and pass the connection and the name of the stored procedure, GetLogID.
Set the command type to stored procedure.
You'll need to pass the session ID to retrieve the logid.
So, create a variable and set it to HttpContext.Current.Session.SessionID + ""; which will implicitly convert the session to a string.
Create a command object for the session ID, and use the session ID variable you created above.
Inside the Try of the Try-Catch exception handling, open the connection.
Assign the reader to the command objects' ExecuteReader method (as you did before, with other SELECT queries).
Use a while loop to call the read method of the reader object.
Assign the reader login field, to the Session variable named LogID.
Assign the lblSession text property to a custom message displaying the values of the session ID, session LogID variable and the reader logid value.
After the while loop, close the reader.
Don't forget you can display an error message using the error object in the label in the Catch block.
In the finally block, close the connection.
Save and build the application and preview the About page in a browser using the local web server built inside Visual Studio.
Step 6. Commenting out Step 5
Once you can 'see' the values, comment out the code. The purpose was to test if the data was being written in the log file, not to display it on the page! There are quirks. If we display the data, you will see that the 'session start' triggers off again, on different pages, if you reopen the browser, especially when working with a computer that uses parallel instructions or localhost servers.
What your challenge is, to look in your log file, do you see records being written? If you do, congrats. Then you can comment out the code in Step 5, save and rebuild the solution.
Comment out BOTH the label control in the footer and the code in the code file that you added in the site.master page
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started