Try to update multiple rows in a grid, continue to get the error 'The ConnectionString property has not been initialized.'

advertisements

I've seen this question asked a lot but the answers I've found seem out of date for my version of VWD because it calls them 'obsolete'. I am just in school and I am very new to this. I am trying to update a value for every line in a gridview that is checked. The first checked value always gets updated but the second, third, etc. never work because the connectionstring property has not been initialized. These are the namespaces I have used:

using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Configuration;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

This is the code that makes me run into problems (it is inside of a button click method). The error seems to come the second time it runs through the 'try' statement and tries to open the connection:

    string NewASNConnStr = ConfigurationManager.ConnectionStrings["Order"].ConnectionString;
    SqlConnection ShipConnection = new
    SqlConnection(ConfigurationManager.ConnectionStrings["Order"].ToString());

    string insertSQL = "INSERT INTO [TRUCKS] ([DateSent]) VALUES (@DateSent)";
    SqlCommand InsertCommand = new SqlCommand(insertSQL, ShipConnection);
    InsertCommand.Parameters.Add("@DateSent", SqlDbType.Date).Value = DateTime.Now;
    Int32 ASNNumber = GetASN();      

    foreach (GridViewRow grvRows in  grvShipPallets.Rows)
    {
        if (((CheckBox)grvRows.FindControl("chkShip")).Checked)
        {

            string RFID = Convert.ToString(grvRows.Cells[1].Text);

            SqlCommand UpdateCommand = new SqlCommand("UPDATE PALLETS SET [email protected] WHERE [email protected]", ShipConnection);
            UpdateCommand.Parameters.Add("@ASNNumber", System.Data.SqlDbType.Int).Value = ASNNumber;
            UpdateCommand.Parameters.Add("@RFID", System.Data.SqlDbType.VarChar).Value= RFID;
            ShipConnection.Open();
            InsertCommand.ExecuteNonQuery();
            ShipConnection.Close();

            int InsertChecker = -2;
            try
            {
                ShipConnection.Open();
                InsertChecker = UpdateCommand.ExecuteNonQuery();
                lblASNConfirmation.Text = "You have shipped the selected Order(s) on ASN # " + ASNNumber;
            }
            catch (Exception MyError)
            {
                lblError.Visible = true;
                lblError.Text = "There has been an error with the database. <br/>";
                lblError.Text += MyError.Message.ToString();
            }
            finally
            {
                ShipConnection.Dispose();
                ShipConnection.Close();
            }
        }
    }
    grvShipPallets.DataBind();
}


Try

string NewASNConnStr = ConfigurationManager.ConnectionStrings["Order"].ConnectionString;

and

SqlConnection ShipConnection =
    new SqlConnection(ConfigurationManager.ConnectionStrings["Order"].ConnectionString);

instead of

string NewASNConnStr = ConfigurationManager.ConnectionStrings["Order"].ToString();

and

SqlConnection ShipConnection =
       new SqlConnection(ConfigurationManager.ConnectionStrings["Order"].ToString());