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

  • SEARCH
  • Home
  • 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 7974915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:30:49+00:00 2026-06-04T08:30:49+00:00

sqlQuery = SELECT [ID] from [users] WHERE CallerName=@CallerName; OleDbConnection conn = new OleDbConnection(connectionString); conn.Open();

  • 0
sqlQuery = "SELECT [ID] from [users] WHERE CallerName=@CallerName";

OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
cmd = new OleDbCommand(sqlQuery, conn);
cmd.CommandText = sqlQuery;
cmd.Parameters.Add("@CallerName", OleDbType.VarChar).Value = labelProblemDate.Text.Trim();
cmd.Parameters["@CallerName"].Value = name;
cmd.ExecuteNonQuery();          
conn.Close();

I was told that this is how to read data from a SELECT query using Parameters but it’s not working. I think I did something wrong.

I am using WinForms and Microsoft Access 2007

  • 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-06-04T08:30:50+00:00Added an answer on June 4, 2026 at 8:30 am

    It looks like you have your answer, but I wanted to point out a few things from your example code:

    sqlQuery = "SELECT [ID] from [users] WHERE CallerName=@CallerName";
    
    OleDbConnection conn = new OleDbConnection(connectionString);
    conn.Open();
    cmd = new OleDbCommand(sqlQuery, conn);
    cmd.CommandText = sqlQuery;
    cmd.Parameters.Add("@CallerName", OleDbType.VarChar).Value = labelProblemDate.Text.Trim();
    cmd.Parameters["@CallerName"].Value = name;
    cmd.ExecuteNonQuery();
    conn.Close();
    

    First, note that your SQL Query is using Microsoft SQL syntax and that Microsoft Access prefers a slightly different syntax. Instead of wrapping your column names in square brackets, use the tilde mark:

    sqlQuery = "SELECT `ID` from `users` WHERE `CallerName`=@CallerName";
    

    Next, in your SQL Query, be aware that Microsoft Access does not accept named parameters. Your SQL text above using @CallerName will execute with no problem, but all the OleDb object will see is this:

    sqlQuery = "SELECT `ID` from `users` WHERE `CallerName`=?";
    

    If, at some point later on, you decide to go with Stored Procedures instead of text SQL, remember to call Prepare() on your OleDbCommand after adding your parameters and before executing the command.

    If you have multiple parameters, ensure that you add these parameters to your OleDbCommand in the same order that you called them in your SQL text. OleDb does not care what you name them, but you can use them for yourself, to aid you; it is NOT used in the query. @CallerName will make no attempt to match up with anything in your SQL text.

    Next, I wanted to look at your usage of the OleDbParameter item. In the two lines below, you are adding one (1) parameter to your OleDbCommand with the value labelProblemDate.Text.Trim() and in the very next line you are re-assigning that same parameter’s value to a variable (that is unknown to us) called name. It does no good for you to declare the parameter with one value then re-assign it to something else.

    You could have used the modified snippet below and gotten the same results (remember to add the size field, as shown below and specified in your database):

    cmd.Parameters.Add("@CallerName", OleDbType.VarChar, 255).Value = labelProblemDate.Text.Trim();
    // cmd.Parameters["@CallerName"].Value = name;
    

    Similarly, your OleDbCommand is being created with your sqlQuery parameter, so specifying your command’s CommandText property is unnecessary:

    cmd = new OleDbCommand(sqlQuery, conn);
    //cmd.CommandText = sqlQuery;
    

    Finally, as others have said, if you want to query your data as your SQL statement suggest, you must read the data in as opposed to calling ExecuteNonQuery() (notice it is called Non Query).

    To sum it up, I have written it out here:

    sqlQuery = "SELECT `ID` from `users` WHERE `CallerName`=?";
    int result = 0;
    OleDbConnection conn = new OleDbConnection(connectionString);
    try {
      conn.Open();
      var cmd = new OleDbCommand(sqlQuery, conn);
      //cmd.CommandText = sqlQuery; This command was specified by your initializer
      cmd.Parameters.Add("?", OleDbType.VarChar, 255).Value = labelProblemDate.Text.Trim();
      //cmd.Parameters["@CallerName"].Value = name; Possible bug here
      using (OleDbDataReader reader = cmd.ExecuteReader())
      {
        if(reader.HasRows)
        {
          reader.Read();
          result = reader.GetInt32(0);
        }
      }
    } finally {
      conn.Close();
    }
    return result;
    

    Always put the Close in a finally block in case your program throws an error for any reason. This prevents your application from crashing and leaving the file open. A using clause, I have found, does not necessarily close a connection when it is done (like they are supposed to do).

    I hope this helps. I’m refreshing my knowledge of OleDb at the moment, and wanted to point out a few things.

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

Sidebar

Related Questions

i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have a bind in the SQL query SELECT * FROM users WHERE name
I want to insert in sql query something like that: Select * from Users
I have a sql query which is like that select * from users where
I have problem with this SQL query: (SELECT tb1.id,tb1.bdate,tb1.jumpCard,tb1.publicImage,tb1.lastlogin FROM users AS tb1, online
this SQL statement select firstname,lastname from users where lastname in('foo','bar') group by firstname it
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
When using this SQL query: Select * from table_name what happens if the name
If I run the following SQL query SELECT * FROM A LEFT JOIN B

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.