3

I used the following code for backup the MYSQL database.

    private void button2_Click(object sender, EventArgs e)
    {
        string file = "D:\\backup.sql";
        //string conn = "server=localhost;user=root;pwd=qwerty;database=test;";
        String str = @"server=192.168.1.219;database=abc;userid=sha;password='123';";
        MySqlBackup mb = new MySqlBackup(str);
        mb.ExportInfo.FileName = file;
        mb.Export();
    }

my stack trace is following -

A first chance exception of type 'System.NullReferenceException' occurred in MySqlBackup.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>TestAppMysqlDBConnect.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Object reference not set to an instance of an object.</Message><StackTrace>   at MySql.Data.MySqlClient.MySqlBackup.ExportExecute()
   at MySql.Data.MySqlClient.MySqlBackup.Export()
   at TestAppMysqlDBConnect.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Shashika\Documents\Visual Studio 2010\Projects\TestAppMysqlDBConnect\TestAppMysqlDBConnect\Form1.cs:line 52
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   ..

But there is a exception and is says there is a null references exception. when i pass the data to the database through the C# program. it was successfully inserted there were no exception. this exception only occur in when i try to backup the database through the C# program. i used 2 Dll files that was in above link. those are - MySql.Data.dll MySqlBackup.dll

I can not solve this exception. Please help.

Exception

5
  • Please include a text copy of the stack trace. It is shown in the lower right frame. Commented Feb 18, 2013 at 5:53
  • @pst - I think i include it now Commented Feb 18, 2013 at 6:14
  • @pst - Sorry for the late. i think this is what you want. isn't it ? Commented Feb 18, 2013 at 7:00
  • Yup. It's not very useful here - but it does show the exception comes from somewhere (directly) within MySqlBackup.Export. Commented Feb 18, 2013 at 7:04
  • 1
    @pst - thank you. i think i solved it now. MySqlBackup.dll need another dll called Ionic.Zip. i included it to my program and problem was solved. thank you again. because your comment help to solve this. Commented Feb 18, 2013 at 9:57

1 Answer 1

1

Try to use this function (Don't forget that you have to add MySqlBackup.dll to your project reference):

public void Backup()
    {
        try
        {
            // Backup...
            DateTime Time = DateTime.Now;
            year = Time.Year;
            month = Time.Month;
            day = Time.Day;
            hour = Time.Hour;
            minute = Time.Minute;
            second = Time.Second;
            millisecond = Time.Millisecond;

            //Save file to Path with the current date as a filename
            string path;
            path = txb_Path.Text + year + "-" + month + "-" + day + "--" + hour + "-" + minute + "-" + second + ".sql";
            string file = path;
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ExportToFile(file);
                        conn.Close();
                    }
                }
            }
            //Done----
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error , unable to backup!" + ex.Message);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.