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 8977225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:19:24+00:00 2026-06-15T19:19:24+00:00

So, presence of @whatever in SQL command string gives me a fatal error( Fatal

  • 0

So, presence of @whatever in SQL command string gives me a fatal error(Fatal error encountered during command execution.) but not when I execute the same query in MySQL Query Browser.

This is the simple example where I give a row number for every John in my table and show them in datagridview:

private void button1_Click(object sender, EventArgs e)
{
   _dataTable.Clear();

   try
   {
      _conn = new MySqlConnection(Cs);
      _conn.Open();

      string name = "john";
      //This is the string that gives me an error
      string ConnString = "SELECT name,lastname,(@Row := @Row + 1) AS RowNumber FROM person JOIN (SELECT @Row := 0) r WHERE name = '"+name+"'";

      //This one doesn't gives an error
      //string ConnString = "SELECT name,lastname FROM person WHERE name = '" + name + "'";

      _cmd = new MySqlCommand
      {
         Connection = _conn,
         CommandText = ConnString
      };

      _cmd.ExecuteNonQuery();

      _da = new MySqlDataAdapter(_cmd);
      _da.Fill(_dataTable);

      _cb = new MySqlCommandBuilder(_da);

      dataGridView1.DataSource = _dataTable;
      dataGridView1.DataMember = _dataTable.TableName;
      dataGridView1.AutoResizeColumns();

      _conn.Close();
  }   
  catch (Exception ex)
  {
     MessageBox.Show(ex.Message);
  }
  finally
  {
     if (_conn != null) _conn.Close();
  }
}

Why is this happening and how can I fix this? I’ve seen lot of questions about this but not a concrete answer for it.

  • 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-15T19:19:25+00:00Added an answer on June 15, 2026 at 7:19 pm

    You shouldn’t build SQL statements like that.. use parameters instead. See SQL Injection. e.g,

    var command = new SqlCommand("SELECT name,lastname,(@Row := @Row + 1) AS" 
                                 " RowNumber FROM person JOIN (SELECT @Row := 0)"
                                 " r WHERE name = @Name");
    SqlParameter param  = new SqlParameter();
    param.ParameterName = "@Name";
    param.Value         = "John Doe";
    command.Parameters.Add(param);
    

    At any rate, that’s not a connection string!

    To use user-defined parameters, you need to set an option. The error you are seeing is a problem with your query because C# thinks you are trying to use @Row as a parameter. See How can I use a MySql User Defined Variable in a .NET MySqlCommand. Add this to your connection string ;Allow User Variables=True. There is also a blog post about this issue.

    Also, what version of the MySQL data provider are you using? You may need to update.

    I found this in the 5.0 documentation:

    “Prior versions of the provider used the ‘@’ symbol to mark parameters in SQL. This is incompatible with MySQL user variables, so the provider now uses the ‘?’ symbol to locate parameters in SQL. To support older code, you can set ‘old syntax=yes’ on your connection string. If you do this, please be aware that an exception will not be throw if you fail to define a parameter that you intended to use in your SQL.”

    From this page: http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html

    So using the @ symbol for the @Name parameter may not work in MySQL and you would need to use ?Name for parameters and @Row for user variables.

    e.g,

    var command = new SqlCommand("SELECT name,lastname,(@Row := @Row + 1) AS" 
                                 " RowNumber FROM person JOIN (SELECT @Row := 0)"
                                 " r WHERE name = ?Name");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to detect the presence of URL in a string Is there
this may not be an earth-shattering deficiency of python, but i still wonder about
Does the presence of python GIL imply that in python multi threading the same
I am trying to filter a ListBox based on the presence of a string.
Paperclip has validator methods for validating the presence, size, and content-type of files. But
I'm using activeadmin and for whatever reason it's not like my Tag model. I
BACKGROUND: when local username is not the same as the git username, git will
I'm publishing a user presence in a Trusted Application Endpoint, but I also would
I'm looking for presence of an element in a list. In Python there is
I try to validate the presence of attributes that belong to another model, to

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.