0

I cannot connect to MySql from my C# windows form application. I also have posted a question already in invalid username/password

but got nothing solution. Suppose my website name is www.google.com and cpanel user name is imrancpanel, my cpanel password is imranpd, I create a database imrandb, create a database user imrandbuser and check all permissions to this user for this website. whats the problem in this 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 MySql.Data.MySqlClient;

namespace CSharpMySql
{
    class Program
    {
        static string getString(string msg)
        {
            Console.Write(msg);
            return Console.ReadLine();
        }

        static void Main(string[] args)
        {
            string myConnectionString = @"server=google.com;userid=imrancpanel_imrandbuser;
            password=imranpd;database=imrandb";

            MySqlConnection con = null;
            MySqlDataReader reader = null;
            string cretablestr = "CREATE TABLE IF NOT EXISTS AutoCaptchaTable (LoginID       char(25), Password char(25), ActivationStatus char(25), NofTransactions int );"

            try
            {
                //MySqlConnection 
                conn = new MySqlConnection(myConnectionString);
                conn.Open();
                MessageBox.Show("OK");
                conn.Close();
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                    MessageBox.Show("Cannot connect to server.  Contact administrator");
                    break;
                    case 1042:
                    MessageBox.Show("Can't get hostname address. Check your internet connection. If does not solve, contact Administrator");
                    break;
                    case 1045:
                    MessageBox.Show("Invalid username/password");
                    break;
                }
            }
        }
    }
}
Community
  • 1
  • 1
Imran Kanjoo
  • 57
  • 2
  • 13
  • possible duplicate of [invalid username/password](http://stackoverflow.com/questions/18452865/invalid-username-password) – Hans Olsson Sep 01 '13 at 11:34

2 Answers2

1

Most website hosters don't allow remote access to MySQL only with localhost

you need to create a webservice with php to allow that or get an alternative

Goran Štuc
  • 581
  • 4
  • 15
  • I think the tag is C#? – Bhushan Firake Aug 27 '13 at 18:39
  • considering he uses a website and MySQL i assume that he has a linux webhost which means php is there since he wants to connect to it via a windows forms app he has to create a web service in php to make it work ;) – Goran Štuc Aug 27 '13 at 18:41
  • Having linux and running mysql doesn't mean he has php on it..:) mysql is a separate package – Bhushan Firake Aug 27 '13 at 18:43
  • i know but 99% of all webhosts with mysql have php – Goran Štuc Aug 27 '13 at 18:44
  • It may be a case, but as he is using C#, he doesn't need PHP ,so he mustn't have installed that.. – Bhushan Firake Aug 27 '13 at 18:46
  • Bhushan Firake: you don't get what i'm talking about, what i'm saying most(like almost all) webhosts DON'T allow remote access to database, so he won't be able to connect with C# directly to the database, my advice is that he creates a web service to be able to talk with the database – Goran Štuc Aug 27 '13 at 19:01
  • thanks to all. @GoranStuc I got your proposed solution of web servie. but there hundered of sites which say how to connect with mysql using c#. of course C# is not a web language. people also make web application in C#. – Imran Kanjoo Aug 27 '13 at 19:27
  • i would say that c# is indeed a web language(asp.net uses c#) if you have a asp.net web hosting you could create a wcf web service. If you have a asp.net web hosting i could help you create it – Goran Štuc Aug 27 '13 at 20:10
0

Enable remote access in cpanel by adding host(IP OR DOMIAN etc)

Imran Kanjoo
  • 57
  • 2
  • 13