您好朋友我正在尝试在asp.net中开发一个喊话框,但它以错误结束连接未关闭。连接的当前状态是开放的。你能帮我解决一下代码中的问题。
Shout.aspx.cs
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace finalWork
{
public partial class Shout : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateTextBox();
}
}
private void PopulateTextBox()
{
SqlConnection con = ConnectionManager.GetDatabaseConnection();
DataTable dt = new DataTable();
string name = string.Empty, message = string.Empty;
StringBuilder sb = new StringBuilder(string.Empty);
try
{
con.Open();
string sqlStatement = "SELECT * FROM Message";
SqlCommand cmd = new SqlCommand(sqlStatement, con);
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//get the data stored from the DataTable
name = dt.Rows[i]["Username"].ToString();// where Name is the FieldName from database
message = dt.Rows[i]["Message"].ToString();
sb.AppendFormat("Name:{0}Date Posted:{1}{2}", name + Environment.NewLine
, DateTime.Now.ToShortDateString() + Environment.NewLine
, message + Environment.NewLine);
}
// get the concated and formatted values from string builder and display the result in TextBoxPrintMessage
TextBoxPrintMessage.Text = sb.ToString();
}
con.Close();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
private void AddNewPost(string name, string message)
{
SqlConnection con = ConnectionManager.GetDatabaseConnection();
string sqlStatement = string.Empty;
sqlStatement = "INSERT INTO Message" +
"(Username,Message)" +
"VALUES (@Username,@Message)";
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, con);
cmd.Parameters.AddWithValue("@Username", name);
cmd.Parameters.AddWithValue("@Message", message);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
protected void ButtonPost_Click(object sender, EventArgs e)
{
// Check for empty values fieds before inserting the record
if (TextBoxName.Text != string.Empty && TextBoxMessage.Text != string.Empty)
{
//insert new post to database
AddNewPost(TextBoxName.Text.Trim(), TextBoxMessage.Text.Trim());
//Populate the TextBox to reflect changes made
PopulateTextBox();
}
else
{
//display message if the field was not supplied
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Script",
"alert('Please supply the required fields!');", true);
}
}
}
}
答案 0 :(得分:0)
我只需手动添加它就可以了:)
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
答案 1 :(得分:0)
尝试这种方式。它会对你有所帮助。
SqlConnection connection = new SqlConnection(youCconnectionString);
connection.Close();
connection.Open();
SqlCommand command = new SqlCommand("YourQuery", connection);
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
connection.Close();
command.Dispose();
答案 2 :(得分:0)
在下面的代码中添加注释,它已在PopulateTextBox()函数的finally块中调用。
//con.Close();
答案 3 :(得分:0)
删除con.Close();在Try块中,因为你在finally块中使用它。