-1

I've attempting to insert data into my localhost database (xampp phpmyadmin), but I keep receiving this error. I tried looking up the exception but have not been able to find any answers.

Screenshot of error

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ProductMaintenance
{
    public partial class Form1 : Form
    {
        SqlConnection connection = new SqlConnection("server=localhost;user id=root123;database=test;allowuservariables=True;persistsecurityinfo=True");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            connection.Open();

            SqlCommand cmd = connection.CreateCommand();

            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "insert into [mytable] (Name,Surname,Address) values (' "+ textBox1.Text + " ', '" + textBox2.Text + " ', ' " + textBox3 + "')";

            cmd.ExecuteNonQuery();
            connection.Close();

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";

        }
Polynomial Proton
  • 5,020
  • 20
  • 37
Triptonix
  • 11
  • 5
  • 1
    Try changing the connection string to `server=localhost;user id=root123;database=test;Allow User Variables=True;persistsecurityinfo=True` – Polynomial Proton Apr 22 '19 at 22:48
  • Possible duplicate of [Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?](https://stackoverflow.com/questions/958953/is-it-possible-to-use-a-mysql-user-defined-variable-in-a-net-mysqlcommand) – Markus Safar Apr 22 '19 at 22:52
  • How is it a duplicate? My connection string has "Allow User Variables = True" – Triptonix Apr 22 '19 at 22:55
  • which version of .net framework are you using ??? – sayah imad Apr 22 '19 at 23:18
  • check this link https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/allow-user-variables/ – sayah imad Apr 22 '19 at 23:19

1 Answers1

1

You are using .Net-classes for Microsoft SQLServer to connect to a MySQL-server. Since 'allowuservariables' is a only supported by MySql, the SQLServer Connection class throws your error. Check out the MySqlConnector NuGet-Package - simply changing the connection string will not enable you to connect to your MySql-instance!

Sancho Panza
  • 670
  • 4
  • 11