remote database restore problem

Asked By Reena Jain
20-Nov-09 09:15 AM
Earn up to 0 extra points for answering this tough question.
I'm using the following code to back-up and restore the database.

try
        {
            string restoreConnStr = ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString.ToString();

            string tableName = ListBox1.SelectedValue;

            using (SqlConnection conn = new SqlConnection(restoreConnStr))
            {

                using (DataSet dSetBackup = new DataSet())
                {

                    // get the schema of the selected table from the database to restore

                    using (SqlDataAdapter dAd = new SqlDataAdapter("select * from " + tableName, conn))
                    {

                        // Following line will get the schema of the selected table in the dSetBackup DataSet

                        dAd.Fill(dSetBackup, tableName);



                        // get the data for selected table from backed up XML file

                        using (DataSet dSet = new DataSet())
                        {

                            dSet.ReadXml(Server.MapPath("~/backup/" + tableName + ".xml"));

                            // Loop through all rows of the selected table and add into dSetBackup DataSet

                            foreach (DataRow row in dSet.Tables[0].Rows)
                            {

                                dSetBackup.Tables[0].NewRow();

                                dSetBackup.Tables[0].Rows.Add(row.ItemArray);

                            }



                            // Create a command builder to update dSetBackup DataSet

                            SqlCommandBuilder cmd = new SqlCommandBuilder(dAd);



                            // Following update command will push all added rows of dSetBackup DataSet into the database

                            dAd.Update(dSetBackup, tableName); // We are done !!!

                        }

                    }

                    lblMessage.Text = "Restore of table <b>" + tableName + "</b> successful!";

                }

            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.ToString();
        }

backup is working fine but when i click on restore button its give me following error


System.IndexOutOfRangeException: Cannot find table 0. at System.Data.DataTableCollection.get_Item(Int32 index) at DatabaseAdmin.RestoreNow(Object sender, EventArgs e) in e:\Reena\harrison\DatabaseAdmin.aspx.cs:line 181

  Things to Check

F Cali replied to Reena Jain
20-Nov-09 10:16 AM

There are a couple of places where the error might have happened:

foreach (DataRow row in dSet.Tables[0].Rows)

If the error happened on this line of code, check your XML file if it exists and if it contains the information you need.

dSetBackup.Tables[0].NewRow();

If the error happened on this line of code, check if your table exists and if contains data.

Either way, make sure to check if Tables[0] exist before accessing any of its properties.

Regards,
SQL Server Helper

  Adding to FCali's reply,

[)ia6l0 iii replied to Reena Jain
22-Nov-09 05:09 AM
 The ConnectionString is already a string. You don't need to call the ToString() on top of it. The following piece of code is enough.
string restoreConnStr = ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString;

The piece ListBox1.SelectedValue is too buggy. The user can not select any values in the listbox and still press the button. not even select and press restore. I think there is a count object in the Selected items collection that should be used extensively.
Like, 
if (ListBox1.SelectedItems.Count > 0)
{
   string tableName = ListBox1.SelectedValue;
}
else
{
    MessageBox.Show("Please select a value");
    ListBox1.Focus();
}

Thirdly, check for the tables count , before you hard card values such as "Tables[0]". I would instead recommend using Typed Datasets for safety. and place all your code inside that check condition.

 using (DataSet dSet = new DataSet())
 {
dSet.ReadXml(Server.MapPath("~/backup/" + tableName + ".xml"));
if (dSet.Tables.Count > 0)
{
foreach (DataRow row in dSet.Tables[0].Rows)
                 {
                        dSetBackup.Tables[0].NewRow();
                        dSetBackup.Tables[0].Rows.Add(row.ItemArray);
 }

                //Create a command builder to update dSetBackup DataSet
                SqlCommandBuilder cmd = new SqlCommandBuilder(dAd);

                //Following update command will push all added rows of dSetBackup DataSet into the //database
                dAd.Update(dSetBackup, tableName); // We are done !!!
}
else
{
MessageBox.Show("We are not done yet !!!");
}
}

And lastly, i would show the "Message" Property of the exception , instead of the Exception.ToString();
catch (Exception ex)
{
         lblMessage.Text = ex.Message;
 }

  thanks for valuable suggestion from your side

Reena Jain replied to [)ia6l0 iii
23-Nov-09 01:38 AM
but sometimes it gives error and sometimes not. is it the problem that its not able to retrive xml file?
  Yes, Indeed.
[)ia6l0 iii replied to Reena Jain
23-Nov-09 04:07 AM
The line number 181 in the Database.Admin.aspx.cs that is present in e:\Reena\harrison\DatabaseAdmin.aspx.cs line is the faulty block.

and moreover, i still don't see your code that writes the XML from the dataset. But frankly speaking, I still don't get the relation straight..dumping a dataset, reading a dataset , adding rows.....
Create New Account