Click here to Skip to main content
15,888,221 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I tried it this way to connect to a database using mysql , however when i run the code...Response.Write does not write anything at all to the website.

C#
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;



public partial class _Default : System.Web.UI.Page
{

      private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        // if it is not a post back method, page renders for first time 
        {

            connectdb();
        
        }

    }

        private void connectdb()
        {
        
             server = "localhost";
            database = "first_db";
            uid = "root";
            password = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            connection = new MySqlConnection(connectionString) ;



            try{
                connection.Open();

                Console.ReadKey();


                Response.Write("connection opened");

                
            }
            catch (MySqlException ex)
            {
                //When handling errors, you can your application's response based 
                //on the error number.
                //The two most common error numbers when connecting are as follows:
                //0: Cannot connect to server.
                //1045: Invalid user name and/or password.
                switch (ex.Number)
                {
                    case 0:
                        Response.Write("Cannot connect to server.  Contact administrator");
                        break;

                    case 1045:
                        Response.Write("Invalid username/password, please try again");
                        break;
                }
               
            }



        
        
          } //end of connectdb 


    


    
}


What I have tried:

***************************************************************************************************************************************************************************************************************************************************************
Posted
Updated 7-Nov-16 4:11am
Comments
F-ES Sitecore 7-Nov-16 3:46am    
Use the debugger to step through your code to see if it is throwing an exception. Your code will simply ignore any exception that isn't 0 or 1045.

1 solution

Read this article , it is helpful regarding your Question
How to Connect to MySQL Using C#[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900