Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
when i try to back up my db then showing "error Microsoft.SqlServer.Smo"



below code i used

C#
internal void TakeBackUp()
        {
            //if(MessageConfirmation.MessageShowYesNo("Do you want to take back up?"))
            // if (MessageBox.Show("Do you want to take back up?", "A2zSchool", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            if (MessageBox.Show("Do you want to take back up?", "Bank", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (sqlcon.State == ConnectionState.Closed)
                {
                    sqlcon.Open();
                }
                string DBName = sqlcon.Database; ;
                string[] arr = DBName.Split('\\');
                DBName = arr[arr.Length - 1].Replace(".MDF", "");
                string DBFolder = arr[arr.Length - 2];
                ServerConnection srvConn = new ServerConnection(sqlcon);
                SaveFileDialog saveBackupDialog = new SaveFileDialog();
                Server srvSql = new Server(srvConn);
                string path = Application.StartupPath + @"\Data\DBBankCheque.mdf";
                srvSql = new Server(srvConn);
                DateTime BackupDate = DateTime.Now;
                string BakDt = BackupDate.ToString("ddMMyyyhhmmss");
                string FName = "Bank---" + DBFolder + "-" + BakDt + ".bak";
                saveBackupDialog.FileName = FName;
                if (saveBackupDialog.ShowDialog() == DialogResult.OK)
                {
                    Backup bkpDatabase = new Backup();
                    bkpDatabase.Action = BackupActionType.Database;
                    bkpDatabase.Database = path;
                    BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName, DeviceType.File);
                    bkpDatabase.Devices.Add(bkpDevice);
                    try
                    {
                        bkpDatabase.SqlBackup(srvSql);

                       MessageBox.Show("The backup of database  completed successfully ", "Bank", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //   MessageBoxes.messageBoxShow("The backup of database  completed successfully");

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Backup failed for Server .  (Microsoft.SqlServer.Smo)", "Bank Software", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        sqlcon.Close();
                    }
                }
            }
        }

index out of array
Posted
Updated 5-Aug-12 19:00pm
v3

1 solution

This is sloppy code. Use the System.IO.Path class to change extensions and file paths.

Set a break point and examine your exception for inner exceptions. The bottom one tells you the real error.
 
Share this answer
 
Comments
Cphashir 4-Aug-12 0:55am    
Backup failed for Server '\\.\pipe\FA774644-3099-42\tsql\query'.
this is erroe
Christian Graus 4-Aug-12 0:58am    
Looks to me like a back up was attempted. Is the path you're passing in valid, and does the DB have rights to write to it ?
_Amy 4-Aug-12 2:00am    
My +5. Well said.
Cphashir 6-Aug-12 0:58am    
index out of Range Exception
Christian Graus 6-Aug-12 1:26am    
I think perhaps that this "string DBFolder = arr[arr.Length - 2];" is the line that gives the error. You've made it as hard as it could be to help you, based on lack of info, but I think your path doesn't have enough sub folders for this code ( which is retarded ) to work. Replace this with sane, sensible code. The error means what it says.

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