An unhandled exception of type MySql.Data.MySqlClient.MySqlException occurred in MySql.Data.dll
Additional information:
Access denied for user 'groupdes_ling'#'60.50.32.226' (using password: YES)
This error occurs whenever i'm trying to run my program. The coding is pasted below:
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 MySql.Data.MySqlClient;
namespace GroupDesignProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string MyConnectionString = "Server=31.220.105.189;Database=groupdes_GDP;Uid=groupdes_ling;Pwd=0164851286;";
MySqlConnection connection = new MySqlConnection(MyConnectionString);//create connection
MySqlCommand cmd;
connection.Open();//connect to database
//write INSERT command and execute
cmd = connection.CreateCommand();//create command
cmd.CommandText = "INSERT INTO trial (Name)values ('" + textBox1.Text + "');";
cmd.ExecuteNonQuery();
//disconnect from database
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
}
I still can't manage to solve this error. Thanks for the help
You need to add permissions for that user/host/password combination to the MySQL instance. For example;
GRANT ALL ON *.* to 'groupdes_ling'#'60.50.32.226' IDENTIFIED BY 'Password';
Related
My code is like this : I input the caseid in the textbox and try to click Find button to find the corresponding result. But Visual studio always says :error occored The connection was not closed The connection current state is open.
This is my first time design ADO.NET myself and I need to finish my advisor research tasks.
Could you check the code and tell me what wrong?
Thanks
Sophia
using System;
using System.Data.SqlClient;
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;
namespace casestudy
{
public partial class Form1 : Form
{
SqlConnection vcon1 = new SqlConnection(#"Data Source=SOPHIA-PC\SQLEXPRESS;Initial Catalog=casestudy;Integrated Security=True");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
vcon1.Open();
}
catch (Exception ex)
{
MessageBox.Show("error.occured" + ex.Message);
this.Dispose();
}
}
private void Find_Click(object sender, EventArgs e)
{
string querystring = "SELECT * FROM AssignedSolution WHERE CASEID = #caseid";
SqlCommand Vcom = new SqlCommand(querystring, vcon1);
Vcom.Parameters.AddWithValue("#caseid", txtCASEID);
SqlDataReader rdr = null;
try
{
Vcom.Connection = vcon1;
vcon1.Open();
Vcom.ExecuteNonQuery();
//DataSet vds1 = new DataSet();
rdr = Vcom.ExecuteReader();
//vDa1.Fill(vds1, "res");
//dataGridView1.DataSource = vds1.Tables["res"];
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
Vcom.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("error.occured" + ex.Message);
}
finally
{
vcon1.Close();
vcon1.Dispose();
}
}
The connection is being opened on Form1_Load event, then you are trying to open the same connection again in the Find_Click event. You can't open a connection that is already open. Just remove all code from the load event, you don't need it.
Sorry to be asking this I know there are many other questions and have tried to use the solutions provided but I just cannot get my code to work. Thanks for looking!
Connection String as shown in Properties:
Data
Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\Jacob\Documents\Visual
Studio
2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\ChatDB.mdf";Integrated
Security=True
Connection string in app.config:
Data
Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ChatDB.mdf;Integrated
Security=True
Error: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near the keyword 'User'.
Code:
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;
//NC-1 More namespaces.
using System.Data.SqlClient;
using System.Configuration;
namespace WindowsFormsApplication2
{
public partial class SignUp : Form
{
string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication2.Properties.Settings.ChatDBConnectionString"].ToString();
public SignUp()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void SubmitBtn_Click(object sender, EventArgs e)
{
string Name = NameText.Text;
string Pwd = PwdText.Text;
//make sure they have entered text
if (Name.Length > 0 && Pwd.Length > 0)
{
SqlConnection conn = new SqlConnection(connstr);
//NC-10 try-catch-finally
try
{
//NC-11 Open the connection.
conn.Open();
SqlCommand insert = new SqlCommand();
insert.Connection = conn;
insert.CommandText = "INSERT INTO [User] (Name,Password) VALUES ('" + Name + "','" + Pwd + "')";
insert.ExecuteNonQuery();
MessageBox.Show("Congrats!!!");
}
catch
{
//NC-14 A simple catch.
MessageBox.Show("User was not returned. Account could not be created.");
}
finally
{
//NC-15 Close the connection.
conn.Close();
}
}
//if no text make them enter
else
{
MessageBox.Show("Please enter Text in both fields.");
}
}
}
}
Again thank you for looking.
The problem is your SQL Query because you use a Reserved Keywords
Try to change your table name to tblUser.
I also suggest to use a parameterize query to prevent future SQL injection: (For Example)
#"INSERT INTO [User] (Name,Password) VALUES (#Name, #Password);"
first off sorry to ask how to fix this error (i know its a common question) but i am quite new to C# and I cannot seem to find a solution for it.
I am making a windows form that imports data from an excel file and displays it in a DataGridView. When executing I get the error:
"An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll Additional information: No value given for
one or more required parameters."
Here is my code:
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.OleDb;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class CurrentOrders : Form
{
public CurrentOrders()
{
InitializeComponent();
}
private void CurrentOrders_Load(object sender, EventArgs e)
{
}
private void BackBtn_Click(object sender, EventArgs e)
{
NewOrder NewOrd = new NewOrder();
this.Hide();
NewOrd.Show();
}
private void DataGridViewLOG_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tombies\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\PCSsheet.xls" + #";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
OleDbCommand command = new OleDbCommand
(
"SELECT DATE, CUSTOMER, PO, COMMENTS, PCS FROM [LOG$]", conn
);
DataSet DsOrderLOG = new DataSet();
OleDbDataAdapter Adapter = new OleDbDataAdapter(command);
conn.Open();
Adapter.Fill(DsOrderLOG);
conn.Close();
DataGridViewLOG.DataSource = DsOrderLOG.Tables[0];
}
}
}
I know it has something to do with the 'Adapter.Fill' at the bottom, but from there on I'm lost.
Any help is appreciated!
Date is probably the culprit. Try putting it (an all other column names, for that matter) in brackets:
"SELECT [DATE], [CUSTOMER], [PO], [COMMENTS], [PCS] FROM [LOG$]", conn
I have some problems using OracleDependency.
I've read docs on msdn and oracle and copy some code to try it.
However this is not working, the event on_my_event doesn't fire when the insert is done.
Does anyone know why ?
My user has CHANGE NOTIFICATION rights on the database. Oracle Server is 11.2.0.3.0.
Here is the code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string connection_string = "Data Source=My_srv;User Id=My_usr;Password=My_pwd;";
OracleConnection connection = null;
OracleDependency dependency = null;
OracleCommand my_select = null;
OracleCommand my_insert = null;
public Form1()
{
InitializeComponent();
}
private void TB_insert_event(object sender, EventArgs e)
{
if (TB_insert.Text == "Name of your insert")
TB_insert.Clear();
}
private void insert(object sender, EventArgs e)
{
connection = new OracleConnection(connection_string);
my_insert = connection.CreateCommand();
my_insert.CommandText = "INSERT INTO USR_DEV_TRUNK.WPARAM (wpa_codeparam) VALUES ('" + TB_insert.Text + "')";
connection.Open();
my_insert.ExecuteNonQuery();
connection.Close();
}
private void Set_dep(object sender, EventArgs e)
{
OracleDependency.Port = 3048;
connection = new OracleConnection(connection_string);
connection.Open();
my_select = connection.CreateCommand();
my_select.CommandText = "SELECT wpa_codeparam FROM USR_DEV_TRUNK.WPARAM";
dependency = new OracleDependency();
dependency.AddCommandDependency(my_select);
my_select.Notification.IsNotifiedOnce = false;
my_select.ExecuteNonQuery();
dependency.OnChange += new OnChangeEventHandler(on_my_event);
TB_dependency.Text = "The dependency is set, do your insert to see if it works";
connection.Close();
}
public void on_my_event(object obj, OracleNotificationEventArgs arg)
{
TB_dependency.Text = "Yay ! It worked !";
}
}
}
I have two buttons :
One to set my dependency (function (on click) : Set_dep)
One to do my insert (function (on click) : insert)
And i have two textboxs :
One to get my insert (name : TB_insert)
One to show the dependency state (name : TB_dependency)
Did you make sure packets on port 3048 are not getting blocked by a firewall?
Also, once your dependency has been set, can you see it by querying the USER_CHANGE_NOTIFICATION_REGS / DBA_CHANGE_NOTIFICATION_REGS views?
Update:
I tried to run this on a local instance of sql-server and sadly it worked!!!
now I know that the code is right and there is some kind of DBA restriction I need to find (and ask the DBA to remove)
Any ideas?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace testDBMessages
{
public class CGeneral
{
// Declare and instantiate connection
private static Form1 caller;
public CGeneral(Form1 caller1)
{
caller = caller1;
string connString = "server=(local)\\SQLEXPRESS;database=tests;Integrated Security=SSPI";
SqlConnection cn = new SqlConnection(connString);
cn.InfoMessage += new SqlInfoMessageEventHandler(CnInfoMessage);
cn.FireInfoMessageEventOnUserErrors = true;
SqlCommand cmd = new SqlCommand();
String sql = "dbo.fillTables";
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sql;
cmd.Parameters.Add(new SqlParameter("#test", 6));
try
{
cn.Open();
SqlDataReader sdr;
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
cn.Close();
}
}
static void CnInfoMessage(object sender, SqlInfoMessageEventArgs ev)
{
foreach (SqlError err in ev.Errors)
{
Console.WriteLine("Message- " + err.Message);
caller.addMessage(err.Message);
}
}
}
}
form code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace testDBMessages
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CGeneral a = new CGeneral(this);
}
private void Form1_Load(object sender, EventArgs e)
{
Application.DoEvents();
}
public void addMessage(string msg)
{
listView1.Items.Add(msg);
listView1.Refresh();
}
}
}
stored procedure
ALTER PROCEDURE [dbo].[fillTables]
(
#test smallint
)
AS
BEGIN
declare #counter as int
SET #counter=1
while #counter<100
BEGIN
Insert into tests.dbo.tToFill (id,description,testNum)
Values (#counter,'test_1',#test)
RAISERROR ('RECORD NUM %d',10,1,#counter)
SET #counter=#counter+1
END
END
GO
Does the user that you connect to your database as using the Integrated security have EXECUTE permission on the stored procedure (dbo.filltables) as that indicates only dbo (database owner) has full permissions on the procedure.
You will need to grant permissions for anyone that wants to use it. Be careful of granting EVERYONE the right if security is a concern.
Your RAISERROR with severity of 10 is classed as a warning so does not flow to client code.
Use 16, which is defined as "Indicates general errors that can be corrected by the user"
(Edit) I'm sure that used to be different...
RAISERROR ('RECORD NUM %d',16,1,#counter)