连接没有关闭。连接的当前状态是打开错误

时间:2015-02-03 16:12:25

标签: c# asp.net

我有网格视图循环每一行,但我收到此错误:

The variable name '@UserId' has already been declared. Variable names must be unique within a query batch or stored procedure

我已修复连接问题,但现在我看到了新问题。请帮忙。

    protected void btn_App_Click(object sender, EventArgs e)
    {

        using (SqlConnection myCon = new SqlConnection(strConnString))
        {
            using (SqlCommand myCmd = new SqlCommand())
            {
                myCmd.Connection = myCon;
                myCon.Open();

                foreach (GridViewRow row in myGV.Rows)
                {

                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        Label UserId = row.FindControl("lblUsrID") as Label;
                        TextBox Start_Date = row.FindControl("txtStartDate") as TextBox;

                        TextBox End_Date = row.FindControl("txtEndDate") as TextBox;
                        CheckBox Reg_Appr = ((CheckBox)row.FindControl("txtchkUsrApp")) as CheckBox;

                        string myUserID = UserId.Text;


                             myCmd.CommandType = CommandType.Text;

                             myCmd.CommandText = "update myTable set Start_Date = @Start_Date, End_Date = @End_Date, Reg_Appr = @Reg_Appr where UserId = @UserId  ";
                             myCmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = UserId.Text;
                             myCmd.Parameters.Add("@Start_Date", SqlDbType.VarChar).Value = Start_Date.Text;
                             myCmd.Parameters.Add("@End_Date", SqlDbType.VarChar).Value = End_Date.Text;

                             myCmd.Parameters.Add("@Reg_Appr", SqlDbType.Bit).Value = Reg_Appr.Checked;   
                             myCmd.ExecuteNonQuery();                             
                             SendActivationEmail(myUserID);
                    }
               }

                myCon.Close();


            }

       }


    }


    //send email
    private void SendActivationEmail(string myUserID)
    {
        // send email
    }

2 个答案:

答案 0 :(得分:2)

移动这些陈述

 myCmd.Connection = myCon;
 myCmd.CommandType = CommandType.Text;
 myCon.Open();

在循环之外。

然后,您只会打开一次连接,而不是每行打开一次。

根据您的意见,您还必须执行以下操作:

在open语句下面添加以下内容:

var uParm = myCmd.Parameters.Add("@UserId", SqlDbType.VarChar);
var sdParm = myCmd.Parameters.Add("@Start_Date", SqlDbType.VarChar);
var edParm = myCmd.Parameters.Add("@End_Date", SqlDbType.VarChar);
var rParm = myCmd.Parameters.Add("@Reg_Appr", SqlDbType.Bit);

然后在使用参数add语句之前将其替换为。

uParm.Value = UserId.Text;
sdParm.Value = Start_Date.Text;
edParm.Value = End_Date.Text;
rParm.Value = Reg_Appr.Checked; 

答案 1 :(得分:1)

您在每次循环期间打开连接。您需要在再次打开它之前关闭它。

但是,在Close()的其他调用中,您可能会收到错误,因此您需要首先检查状态以验证连接是否已打开。

if (row.RowType == DataControlRowType.DataRow)
                    {
                        Label UserId = row.FindControl("lblUsrID") as Label;
                        TextBox Start_Date = row.FindControl("txtStartDate") as TextBox;

                        TextBox End_Date = row.FindControl("txtEndDate") as TextBox;
                        CheckBox Reg_Appr = ((CheckBox)row.FindControl("txtchkUsrApp")) as CheckBox;

                        string myUserID = UserId.Text;

                             myCmd.Connection = myCon;
                             myCmd.CommandType = CommandType.Text;
                             myCon.Open();
                             myCmd.CommandText = "update myTable set Start_Date = @Start_Date, End_Date = @End_Date, Reg_Appr = @Reg_Appr where UserId = @UserId  ";
                             myCmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = UserId.Text;
                             myCmd.Parameters.Add("@Start_Date", SqlDbType.VarChar).Value = Start_Date.Text;
                             myCmd.Parameters.Add("@End_Date", SqlDbType.VarChar).Value = End_Date.Text;

                             myCmd.Parameters.Add("@Reg_Appr", SqlDbType.Bit).Value = Reg_Appr.Checked;                                
                             SendActivationEmail(myUserID);
                             // added this line
                             myCon.Close();
                    }