Reputation: 11
I get an error when I debug my code (it doesn't show any error before debugging) Below is the code I wrote in Visual Studio 2017 using C#
using MySql.Data;
using MySql.Data.MySqlClient;
namespace MARUBI
{
public partial class addUser : Form
{
MySqlConnection con;
MySqlDataAdapter da;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public addUser()
{
InitializeComponent();
string str = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\MARUBI\MARUBI\MARUBI\MarubiDB.mdf;Integrated Security=True;Connect Timeout=30";
con = new MySqlConnection(str);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string username = textBox1.Text;
string password = textBox2.Text;
string name = textBox3.Text;
string surename = textBox4.Text;
string tel = textBox5.Text;
string email = textBox6.Text;
string sql = "INSERT INTO dbo.users(username, password, name, surename, tel, email) VALUES ('"+username+"', '"+password+"','"+name+"','"+surename+"','"+tel+"','"+email+"'";
try
{
con.Open();
da = new MySqlDataAdapter();
da.InsertCommand = new MySqlCommand(sql, con);
da.InsertCommand.ExecuteNonQuery();
MessageBox.Show("Values inserted succesfully");
load_grid();
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void load_grid()
{
dt.Clear();
MySqlDataAdapter da = new MySqlDataAdapter();
try
{
string sql = "SELECT * FROM USERS";
con.Open();
da.SelectCommand = new MySqlCommand(sql, con);
da.Fill(ds);
da.Fill(dt);
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void addUser_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'marubiDBDUSERS.users' table. You can move, or remove it, as needed.
load_grid();
}
}
This is my code and I get the error
System.ArgumentException: 'Option not supported.
Parameter name: attachdbfilename'
Upvotes: 0
Views: 3349
Reputation: 74615
Your C# code uses the MySQL driver:
MySqlConnection con;
MySqlDataAdapter da;
but the connection string you passed is one for a completely different database system from a rival vendor (Microsoft SQL Server):
"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\MARUBI\MARUBI\MARUBI\MarubiDB.mdf"
Either use the correct code, or the correct connection string; you didn't say which database you were actually using
Upvotes: 2