Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:49:45+00:00 2026-05-25T18:49:45+00:00

I’ve got a function that stores temporary information generated for every user authenticated in

  • 0

I’ve got a function that stores temporary information generated for every user authenticated in the system. This ‘session ID’ is a string stored in a Sessions table, along the original ID of the user which authenticated and was given said session identifier.

The function to remove/deauthenticate/invalidate an existing session first checks if the user exists through another method implemented as follows:

        int userId = 0;
        SqlCeCommand cmd = new SqlCeCommand();
        SqlCeParameterCollection sqlParams = cmd.Parameters;
        sqlParams.AddWithValue("@User", userName);
        cmd.Connection = this.conn;
        cmd.CommandText = "SELECT Id FROM Users WHERE (Username = @User)";

        userId = (int) cmd.ExecuteScalar()
        cmd.Dispose();

Afterwards it tries to find an existing session for that user, which is to be removed (via a different method again):

        SqlCeCommand cmd = new SqlCeCommand();
        SqlCeParameterCollection sqlParams = cmd.Parameters;
        sqlParams.AddWithValue("@SID", mysession);
        sqlParams.AddWithValue("@UID", myuserid);

        cmd.Connection = this.Connection;
        cmd.CommandText = "SELECT Id FROM UserSessions WHERE (SessionID = @SID) AND (User_Id = @UID)";
        int foo = cmd.ExecuteNonQuery();

…which fails. No exception is raised unfortunately. So I added an insecure equivalent using a non parametrized query string:

        cmd.CommandText = String.Format("SELECT Id FROM UserSessions WHERE (SessionID = '{0}') AND (User_Id = {1})", mysession, myuserid);
        cmd.Prepare();
        int bar = cmd.ExecuteNonQuery();

Added a breakpoint, paused, copy pasted the query into the Visual Studio Query tool and voila, it indeed worked. But after continuing, that query in the code failed as well. I’m unable to find the culprit of this annoying issue since no exception is raised and everything seems correct. The data exists, the parameters are provided in proper types (string and int) and I’m out of things to check. The connection is open and so forth.

Any clues from anyone around? Thanks!

Update: Mea culpa, missed the fact that the function used ExecuteScalar until I modified it for testing. It does use ExecuteScalar and returns null, just in case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:49:45+00:00Added an answer on May 25, 2026 at 6:49 pm

    You’re using ExecuteNonQuery:

    int foo = cmd.ExecuteNonQuery();
    

    … but you’re clearly trying to execute a query (a SELECT)! Use ExecuteScalar again, as you did in the first code, or ExecuteReader and look through the results appropriately. If you stick with ExecuteScalar, you should first check whether the result is null to indicate no results.

    ExecuteNonQuery returns the number of rows affected by an UPDATE/INSERT/DELETE command – which is what it’s intended for. I suspect it’s returning -1 for you, as documented:

    For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

    (Emphasis mine.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
i got an object with contents of html markup in it, for example: string
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.