locked
MysqlException..Access denied for user RRS feed

  • Question

  • User-298623194 posted

    Hi,

    Can anyone please guide me where i am going wrong..i am trying to connect to mysql database and display the data in the form of gridview and in the below code i have highlighted in bolt where i am getting an exception as: "Access denied for user 'xxx'@'%' to database 'acl' "

    cs file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using MySql.Data.MySqlClient;

    namespace TOR_Tool
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                DataUtiliities dt = new DataUtiliities();
                GridView1.DataSource = dt.ExecuteDataSet("select t.id,t.Subject,t.Status,substring(a.Content,1,2000),t.created from rt.Tickets t,rt.ObjectCustomFieldValues o,rt.Attachments a where t.created = o.created and t.created = a.created and status = 'pending' ");
                GridView1.DataBind();

            }
        }
    }

    data layer..cs file:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.Configuration;
    using MySql.Data.MySqlClient;
    using System.Configuration;

    /// <summary>
    /// Summary description for DataUtiliities
    /// </summary>
    public class DataUtiliities
    {

        private string _connectionString = null;
        public DataUtiliities()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public DataUtiliities(string connectionString)
        {
            _connectionString = WebConfigurationManager.ConnectionStrings[connectionString].ConnectionString; ;
        }
        #region MYMySqlConnection

        /// <summary>
        /// This method gets the connection string.
        /// </summary>
        /// <returns>Connection String</returns>
        public string GetConnectionString()
        {
            string strConnection = null;

            try
            {
                /* This code takes connection string from the web.config file.*/
                strConnection = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

            }
            catch (ConfigurationErrorsException ex)
            {
                strConnection = null;
            }
            return strConnection;
        }


        /// <summary>
        /// This method returns MySqlConnection object.
        /// </summary>
        /// <returns>MyMySqlConnection</returns>
        public MySqlConnection GetMySqlConnection()
        {
            string strConnection = _connectionString ?? GetConnectionString();
            if (strConnection == null)
                return null;
            MySqlConnection objMySqlConnection = new MySqlConnection(strConnection);
            return objMySqlConnection;
        }

        #endregion


        #region EXECUTE DATASET

        /// <summary>
        /// This method returns the data in dataset form.
        /// </summary>
        /// <param name="commandText">Command text</param>
        /// <returns>Data in the form of Dataset.</returns>
        public DataSet ExecuteDataSet(string commandText)
        {
            DataSet dsData = new DataSet();
            MySqlDataAdapter objMyDataAdapter = new MySqlDataAdapter();
            MySqlConnection objMySqlConn = GetMySqlConnection();

            if (objMySqlConn == null)
            {
                return null;
            }
            MySqlCommand objMyCommand = new MySqlCommand
            {
                Connection = objMySqlConn,
                CommandType = CommandType.Text,
                CommandText = commandText
            };
            objMyDataAdapter.SelectCommand = objMyCommand;
            try
            {
                objMyDataAdapter.Fill(dsData);
            }
            catch (Exception ex)
            {

                return null;
            }

            return dsData;
        }


        /// <summary>
        /// This method returns the data in dataset form.
        /// </summary>
        /// <param name="commandText">Command Text</param>
        /// <param name="htParameters">Hash Table</param>
        /// <returns>Data in the form of Dataset</returns>
        public DataSet ExecuteDataSet(string commandText, Hashtable htParameters)
        {
            DataSet dsData = new DataSet();
            MySqlDataAdapter objMyDataAdapter = new MySqlDataAdapter();
            MySqlConnection objMySqlConn = GetMySqlConnection();
            if (objMySqlConn == null)
            {
                return null;
            }
            MySqlCommand objMySqlCommand = new MySqlCommand
            {
                Connection = objMySqlConn,
                CommandType = CommandType.StoredProcedure,
                CommandText = commandText
            };

            try
            {
                foreach (DictionaryEntry parameter in htParameters)
                {
                    objMySqlCommand.Parameters.AddWithValue(parameter.Key.ToString(), parameter.Value);
                }

                objMyDataAdapter.SelectCommand = objMySqlCommand;
                objMyDataAdapter.Fill(dsData);
            }
            catch (Exception ex)
            {
                return null;
            }
            return dsData;
        }

        #endregion

    }

    web.config:

    <?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
        <connectionStrings>
            <add name="ApplicationServices" connectionString="Server=134.19.2.123;Port=3306;Database=acl;Uid=xxx;Pwd=abc;" providerName="MySql.Data.MySqlClient"/>
        </connectionStrings>
        <system.web>
            <compilation debug="true"/></system.web></configuration>

    Thanks!!

    Wednesday, October 17, 2012 10:59 AM

All replies

  • User-298623194 posted

    and i am able to access the tables from mysql gui tool..MYSQL Administrator..Thanks!!

    Wednesday, October 17, 2012 11:02 AM
  • User1264447444 posted

    Try to login to the database you are accessing using the username and password in the connection string.If you can't then that means the user does not have access to the database.First add the user to the database and give the user the correct permissions to execute the queries and then use credentails in the connection string.

    Wednesday, October 17, 2012 11:04 AM
  • User-298623194 posted

    yes i am able to login to the database from mysql administrator and view the tables,but when i try to connect from the application i get that exception i had metioned in my earlier post.

    Thanks!!

    Wednesday, October 17, 2012 11:37 AM
  • User1264447444 posted

    After login are you able to execute the query?

    Wednesday, October 17, 2012 1:39 PM