尝试更新gridview中的多行,不断收到错误'ConnectionString属性尚未初始化'。

时间:2011-05-12 01:50:28

标签: c# sql connection-string sql-update

我已经看到这个问题了很多,但我发现的答案似乎已经过时了我的VWD版本,因为它称它们为“过时”。我刚上学,我对此很陌生。我正在尝试更新已检查的gridview中每一行的值。第一个检查值总是更新,但第二个,第三个等等永远不会工作,因为connectionstring属性尚未初始化。这些是我使用过的命名空间:

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;

这是让我遇到问题的代码(它位于按钮点击方法内)。错误似乎是第二次它通过'try'语句并尝试打开连接时出现:

    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 TRUCKS$ASNNumber=@ASNNumber WHERE RFID=@RFID", 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();
}

2 个答案:

答案 0 :(得分:0)

尝试

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

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

而不是

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

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

答案 1 :(得分:0)

这是因为你在finally语句中处理了对象。因此,在后续循环中,您的连接不再作为对象存在。删除处理线。关闭连接就足够了。在您不再需要它之前不要丢弃它。