1
我是ASP.NET的新手,並且陷入了這個問題。我收到錯誤: 錯誤:System.InvalidOperationException:連接未關閉。連接的當前狀態已打開。在System.Data.ProviderBase.DbConnectionInternal。 這是我的代碼,錯誤點指向'while'語句。我試過很多東西,但沒有成功。我知道當if語句爲真,但不知道如何時,我需要關閉連接。誰可以幫我這個事?謝謝。如果在if語句後如果關閉mysql連接,如果它是真的?
protected void loadtour()
{
try
{
conn.Open();
string strSelect = "Select * From Agents where Agent_ID='" + AgID.Text + "'";
SqlCommand cmd = new SqlCommand(strSelect, conn);
SqlDataReader myReader = cmd.ExecuteReader();
while (myReader.Read())
{
if (myReader["Agent_Status"].ToString() == "On Tour")
{
Label1.Text = "Assigned Tour Details:";
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT PackageInfo.pkg_Name, PackageInfo.pkg_Type, PackageInfo.pkg_Status, Packages.pkg_Country, Packages.pkg_Start, Packages.pkg_End, PackageInfo.Agent_ID, PackageInfo.Agent_Name FROM PackageInfo INNER JOIN Packages ON PackageInfo.pkg_ID = Packages.pkg_ID WHERE(PackageInfo.Agent_ID = '" + AgID.Text + "')", conn);
DataTable ds = new DataTable();
da.Fill(ds);
gv1.DataSource = ds;
gv1.DataBind();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
conn.Close();
}
else
{
Label1.Text = "No Tour Assigned Yet!";
}
myReader.Close();
conn.Close();
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}