locked
hi guys, RRS feed

  • Question

  • User849610253 posted

    I created one registration page using ASP.net and i want to insert these values in mysql database table...

    and i got an error " MySql.Data.MySqlClient.MySqlException: Access denied for user 'mysql'@'%' to database 'registrationconectionstring' " like this can u please help me.  

    following is my aspx.cs file.

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

    public partial class login : System.Web.UI.Page
    {
    MySqlConnection conn = new MySqlConnection("Server=localhost;Port=3306;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;Allow User Variables=True;");
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Btninsert_Click(object sender, EventArgs e)
    {



    MySqlCommand cmd = conn.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "INSERT INTO userdata(Username,Email,Password,Country) VALUES(@username,@email,@password,@country)";
    conn.Open();
    cmd.ExecuteNonQuery();

    conn.Close();

    }
    }

    Thank you.

    Wednesday, August 13, 2014 2:03 AM

Answers

  • User79986525 posted

    Hi ,

    Try this 

    Change Connerction string Name 
    
    <?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="DBConnectionString" connectionString="Server=localhost;Port=3306;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
    <compilation debug="true" targetFramework="4.0">
    <assemblies>
    <add assembly="MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
    <add assembly="MySql.Data.Entity, Version=6.8.3.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
    </assemblies>
    </compilation>
    </system.web>
    </configuration>
    
    
    ON Button Click event updateyour code like this 
    SqlConnection oConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
    
    protected void Btninsert_Click(object sender, EventArgs e)
    {
    
    InsertData(textboxUN.Text,textboxEmail.Text,textboxPwd.Text,ddllist.SelectedText);
    
    }
    
    public void InsertData(string username,string email,string password,string country)
    {
    
    // SqlConnection oConnection = new SqlConnection("Connection_String");
            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = oConnection;
            oCommand.CommandText = "INSERT INTO userdata(Username,Email,Password,Country) VALUES(@username,@email,@password,@country)";
            oCommand.CommandType = CommandType.Text;
            oCommand.Parameters.AddWithValue("@GroupName", GroupName); 
    oCommand.Parameters.AddWithValue("@username", username); 
    oCommand.Parameters.AddWithValue("@email", email); 
    oCommand.Parameters.AddWithValue("@password", password); 
    oCommand.Parameters.AddWithValue("@country", country); 
    oConnection.Open();
     Boolean Result = Convert.ToBoolean(oCommand.ExecuteNonQuery()); 
    oConnection.Close();
    }
    
    
    
    Also try this connection string
    <add name="DBConnectionString" connectionString="Server=localhost,3306;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;" providerName="System.Data.SqlClient"/>
    
    OR this 
    <add name="DBConnectionString" connectionString="Server=localhost;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;" providerName="System.Data.SqlClient"/>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 14, 2014 2:01 AM

All replies

  • User1283497924 posted

    cmd.CommandText = "INSERT INTO userdata(Username,Email,Password,Country) VALUES(@username,@email,@password,@country)";

    i think you have not passes any value.

    Wednesday, August 13, 2014 2:10 AM
  • User849610253 posted

    im passing values through the page which i was created..

    Wednesday, August 13, 2014 2:54 AM
  • User79986525 posted

    Hi ,

    Try something

     
    MySqlConnection oConnection= new MySqlConnection("Server=localhost;Port=3306;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;Allow User Variables=True;");
    // SqlConnection oConnection = new SqlConnection("Connection_String");
            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = oConnection;
            oCommand.CommandText = "INSERT INTO userdata(Username,Email,Password,Country) VALUES(@username,@email,@password,@country)";
            oCommand.CommandType = CommandType.Text;
            oCommand.Parameters.AddWithValue("@GroupName", GroupName); 
    oCommand.Parameters.AddWithValue("@username", username); 
    oCommand.Parameters.AddWithValue("@email", email); 
    oCommand.Parameters.AddWithValue("@password", password); 
    oCommand.Parameters.AddWithValue("@country", country); 
    oConnection.Open();
     Boolean Result = Convert.ToBoolean(oCommand.ExecuteNonQuery()); 
    oConnection.Close();
     

    Hope this will help you out

    like this 

    Wednesday, August 13, 2014 3:07 AM
  • User849610253 posted

    hi ,

    i tried your code but the error is "the name 'username' doesn't exists in the current context"

    please tell me..

    Wednesday, August 13, 2014 3:32 AM
  • User79986525 posted

    Hi ,

    public void InsertData(string username,string email,string password,string country)
    {
    MySqlConnection oConnection= new MySqlConnection("Server=localhost;Port=3306;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;Allow User Variables=True;");
    // SqlConnection oConnection = new SqlConnection("Connection_String");
            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = oConnection;
            oCommand.CommandText = "INSERT INTO userdata(Username,Email,Password,Country) VALUES(@username,@email,@password,@country)";
            oCommand.CommandType = CommandType.Text;
            oCommand.Parameters.AddWithValue("@GroupName", GroupName); 
    oCommand.Parameters.AddWithValue("@username", username); 
    oCommand.Parameters.AddWithValue("@email", email); 
    oCommand.Parameters.AddWithValue("@password", password); 
    oCommand.Parameters.AddWithValue("@country", country); 
    oConnection.Open();
     Boolean Result = Convert.ToBoolean(oCommand.ExecuteNonQuery()); 
    oConnection.Close();
    }

    InsertData("sanjay","sanjpwd","sanjay@testemail.com","india");

    Wednesday, August 13, 2014 3:42 AM
  • User1428246847 posted

    and i got an error " MySql.Data.MySqlClient.MySqlException: Access denied for user 'mysql'@'%' to database 'registrationconectionstring' " like this can u please help me.

    Does the user 'mysql' exist? Does the database 'registrationconectionstring' exist? Have you granted permissions for the user 'mysql' to the database 'registrationconectionstring'?

    Wednesday, August 13, 2014 3:56 AM
  • User849610253 posted

    yes mr wim sturkenboom

     i have granted all my permissions from database..  in the following sentence i got error "CS0103: The name 'Username' does not exist in the current context"...

    cmd.Parameters.AddWithValue("@Username", Username);

    Wednesday, August 13, 2014 5:02 AM
  • User79986525 posted

    Hi ,

    The error is because it's not able to access your database .

    As i check within databse you provide registrationconectionstring.

    Is it registrationconectionstring is your database name ?

    your connection string should be like thsi 

    Database=databasename; Server=localhost; User ID=sa; Password=abc;"

     

    Wednesday, August 13, 2014 5:30 AM
  • User1428246847 posted

     in the following sentence i got error "CS0103: The name 'Username' does not exist in the current context"...

    cmd.Parameters.AddWithValue("@Username", Username);

    'username' with lower case 'U'

    cmd.Parameters.AddWithValue("@Username", username);

    Wednesday, August 13, 2014 5:33 AM
  • User849610253 posted

    the registrationconectionstring is my database name itself 

    Wednesday, August 13, 2014 6:59 AM
  • User849610253 posted

    i tried with differnt cases(lower or upper) but it wont work

    Wednesday, August 13, 2014 7:00 AM
  • User79986525 posted

    Hi ,

    Check the Links that may help you 

    https://www.geeklog.net/faqman/index.php?op=view&t=44

    http://dev.mysql.com/doc/refman/5.1/en/privilege-system.html

    http://stackoverflow.com/questions/20353402/access-denied-for-user-testlocalhost-using-password-yes-except-root-user

    http://stackoverflow.com/questions/8484722/access-denied-for-user-rootlocalhost-while-attempting-to-grant-privileges

    Wednesday, August 13, 2014 8:05 AM
  • User1428246847 posted

    i tried with differnt cases(lower or upper) but it wont work

    Show us the exact code and the exact error message, please.

    Wednesday, August 13, 2014 8:20 AM
  • User849610253 posted

    hi guys good morning,

    my aspx page is...

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
    .style1
    {
    margin-left: 0px;
    }
    .style2
    {}
    .style4
    {
    width: 66px;
    }
    .style5
    {
    height: 26px;
    text-align: right;
    margin-left: 40px;
    }
    .style6
    {
    text-align: right;
    height:25px;
    }
    .style8
    {
    height: 25px;
    }
    </style>
    <%-- <script language="javascript" type="text/javascript">
    // <![CDATA[

    function Btnreset_onclick() {

    }

    // ]]>
    </script>--%>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    <h2 style="text-align:center">Registration Page</h2>
    <table style="margin-left:20%;">
    <tr>
    <td class="style6">Username:</td>
    <td class="style8"><asp:TextBox runat="server" ID="textboxUN" Height="20px" Width="168px"></asp:TextBox></td>
    <td class="style8">
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
    ErrorMessage="Username is required" ControlToValidate="textboxUN"
    ForeColor="Red"></asp:RequiredFieldValidator>
    </td></tr>

    <tr>
    <td class="style6">Email:</td>
    <td><asp:TextBox runat="server" ID="textboxEmail" Height="20px" Width="168px"></asp:TextBox></td>
    <td>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
    ErrorMessage="Email is required" ControlToValidate="textboxEmail"
    ForeColor="Red"></asp:RequiredFieldValidator>
    <br />
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
    ErrorMessage="You must enter valid email address" ForeColor="Red"
    ControlToValidate="textboxEmail"
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
    </td></tr>

    <tr>
    <td class="style6">Password:</td>
    <td><asp:TextBox runat="server" ID="textboxPwd" TextMode="Password" Height="20px" Width="168px"></asp:TextBox></td>
    <td>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
    ErrorMessage="password is Required" ControlToValidate="textboxPwd"
    ForeColor="Red"></asp:RequiredFieldValidator>
    </td></tr>

    <tr>
    <td class="style6">Confirm Password:</td>
    <td><asp:TextBox runat="server" ID="textboxCPwd" TextMode="Password" Height="20px" Width="168px"></asp:TextBox></td>
    <td>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
    ErrorMessage="Confirm password is required"
    ControlToValidate="textboxCPwd" ForeColor="Red"></asp:RequiredFieldValidator>
    <br />
    <asp:CompareValidator ID="CompareValidator1" runat="server"
    ErrorMessage="Password does not match" ControlToCompare="textboxPwd"
    ControlToValidate="textboxCPwd" ForeColor="Red"></asp:CompareValidator>
    </td></tr>

    <tr>
    <td class="style5">Country:</td>
    <td><asp:DropDownList runat="server" ID="ddllist" CssClass="style1" Height="25px"
    Width="173px">
    <asp:ListItem>Select Country</asp:ListItem>
    <asp:ListItem>India</asp:ListItem>
    <asp:ListItem>China</asp:ListItem>
    <asp:ListItem>USA</asp:ListItem>
    <asp:ListItem>Pakistan</asp:ListItem>
    <asp:ListItem>Australia</asp:ListItem>
    </asp:DropDownList></td>
    <td>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
    ControlToValidate="ddllist" ErrorMessage="Select a Country name"
    ForeColor="Red" InitialValue="Select Country" ></asp:RequiredFieldValidator>
    </td>
    </tr>



    <tr>
    <td></td>
    <td> <asp:Button runat="server" Text="Insert" CssClass="style2" Width="67px"
    ID="Btninsert" onclick="Btninsert_Click" /></td>
    <td></td></tr>
    </table>
    </div>
    </form>
    </body>
    </html>

    and aspx.cs page is ..

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

    public partial class login : System.Web.UI.Page
    {
    MySqlConnection conn = new MySqlConnection("Server=localhost;Port=3306;Database=registrationconectionstring;Uid=root;Pwd=registration;");
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Btninsert_Click(object sender, EventArgs e)
    {

    MySqlConnection conn = new MySqlConnection("Server=localhost;Port=3306;Database=registrationconectionstring;Uid=root;Pwd=registration;");
    conn.Open();
    MySqlCommand cmd = conn.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "INSERT INTO userdata(Username,Email,Password,Country) VALUES(@username,@email,@password,@country)";

    cmd.Parameters.AddWithValue("@Username", username);
    cmd.Parameters.AddWithValue("@Email", email);
    cmd.Parameters.AddWithValue("@Password", password);
    cmd.Parameters.AddWithValue("@Country", country);

    cmd.ExecuteNonQuery();


    conn.Close();

    }


    }

    ->"userdata" is my table name and "registrationconectionstring" is my database name..

    my config file is...

    <?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="registrationconectionstring" connectionString="Server=localhost;Port=3306;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
    <compilation debug="true" targetFramework="4.0">
    <assemblies>
    <add assembly="MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
    <add assembly="MySql.Data.Entity, Version=6.8.3.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
    </assemblies>
    </compilation>
    </system.web>
    </configuration>

    can u please check it and tell me were is the error why it is not executing in my system.

    thank you.

    Thursday, August 14, 2014 12:42 AM
  • User79986525 posted

    Hi ,

    Try this 

    Change Connerction string Name 
    
    <?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="DBConnectionString" connectionString="Server=localhost;Port=3306;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
    <compilation debug="true" targetFramework="4.0">
    <assemblies>
    <add assembly="MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
    <add assembly="MySql.Data.Entity, Version=6.8.3.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
    </assemblies>
    </compilation>
    </system.web>
    </configuration>
    
    
    ON Button Click event updateyour code like this 
    SqlConnection oConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
    
    protected void Btninsert_Click(object sender, EventArgs e)
    {
    
    InsertData(textboxUN.Text,textboxEmail.Text,textboxPwd.Text,ddllist.SelectedText);
    
    }
    
    public void InsertData(string username,string email,string password,string country)
    {
    
    // SqlConnection oConnection = new SqlConnection("Connection_String");
            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = oConnection;
            oCommand.CommandText = "INSERT INTO userdata(Username,Email,Password,Country) VALUES(@username,@email,@password,@country)";
            oCommand.CommandType = CommandType.Text;
            oCommand.Parameters.AddWithValue("@GroupName", GroupName); 
    oCommand.Parameters.AddWithValue("@username", username); 
    oCommand.Parameters.AddWithValue("@email", email); 
    oCommand.Parameters.AddWithValue("@password", password); 
    oCommand.Parameters.AddWithValue("@country", country); 
    oConnection.Open();
     Boolean Result = Convert.ToBoolean(oCommand.ExecuteNonQuery()); 
    oConnection.Close();
    }
    
    
    
    Also try this connection string
    <add name="DBConnectionString" connectionString="Server=localhost,3306;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;" providerName="System.Data.SqlClient"/>
    
    OR this 
    <add name="DBConnectionString" connectionString="Server=localhost;Database=registrationconectionstring;Uid=mysql;Pwd=mysql;" providerName="System.Data.SqlClient"/>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 14, 2014 2:01 AM
  • User849610253 posted

    thank u guys for ur support its working gud..Smile

    Thursday, August 14, 2014 2:12 AM