0
我已經搜索的連接字符串屬性尚未初始化錯誤連接字符串屬性未初始化
。
谷歌以及堆棧溢出,但無法找到解決方案。我創建了一個與數據庫交互的數據庫類,所有相關的代碼都寫在這個文件中。問題是相同的代碼在其他頁面上運行良好,它只是不能在名爲「addevent.aspx」的頁面上工作。我不明白爲什麼它不能正常運行。
下面是我在database.cs創建的方法文件
public void CreateConnection()
{
var ConfiguredString = ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString;
obj_sqlconnection = new SqlConnection(ConfiguredString);
}
//This property will set the connection string for database
public string ConnectionString
{
get
{ //if _connectionString is already created or set, only then it will return the value of _connectionString
if (_connectionString != string.Empty && _connectionString != "" && _connectionString != null)
return _connectionString;
else
return string.Empty;
}
// When you want to set the connection string set block is called.
set
{ // this line sets the connection string to the _connectionString data member for the first time.
if (_connectionString == string.Empty || _connectionString == "" || _connectionString == null)
_connectionString = value;
}
}
// Open database connection.
public void OpenConnection()
{
obj_sqlconnection.Open();
}
// Close database connection.
public void CloseConnection()
{
obj_sqlconnection.Close();
obj_sqlconnection.Dispose();
}
public SqlConnection GetCurrentConnection
{
get { return obj_sqlconnection; }
set { obj_sqlconnection = value; }
}
我根本就不懂這個錯誤,其發生的邏輯。我得到這個錯誤,當我打開連接
如何調用這些方法,我已經創建database.cs類的對象與對象名稱MYDB方法的addEvent
public int AddEvent(string _title, string _description, string _place, int _eventTypeID, string _startingTime, string _endingTime, string _startingDate, string _endingDate, string _creatorID, string _picture)
{
string[] blacklist = { _title, _description, _place, _picture };
if (Jvalidate.FilterBlackLIstKeywords(blacklist))
{
int eventid = Convert.ToInt32(mydb.GetLastValueByColumnName("event_id", "tbl_events"));
int rowsaffected = 0;
mydb.CreateConnection();
mydb.InitializeSQLCommandObject(mydb.GetCurrentConnection, "spAddEvent", true);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventID", eventid + 1);
mydb.obj_sqlcommand.Parameters.AddWithValue("@title", _title);
mydb.obj_sqlcommand.Parameters.AddWithValue("@description", _description);
mydb.obj_sqlcommand.Parameters.AddWithValue("@place", _place);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventType", _eventTypeID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@startingTime", _startingTime);
mydb.obj_sqlcommand.Parameters.AddWithValue("@endingTime", _endingTime);
mydb.obj_sqlcommand.Parameters.AddWithValue("@startDate", _startingDate);
mydb.obj_sqlcommand.Parameters.AddWithValue("@endDate", _endingDate);
mydb.obj_sqlcommand.Parameters.AddWithValue("@schoolID", SchoolID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventCreatorID", _creatorID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventPicture", _picture);
try
{
//mydb.obj_sqlconnection.ConnectionString = ConfigurationManager.ConnectionStrings["cesConnectionString"].ToString();
mydb.OpenConnection();
rowsaffected = mydb.obj_sqlcommand.ExecuteNonQuery();
}
finally
{
mydb.CloseConnection();
mydb.obj_sqlcommand.Dispose();
}
return rowsaffected;
}
return 0;
}
請說明你如何使用這個類的方法 – Steve
我已經更新了這篇文章。請看這裏 –
從這段代碼我不能確定這個錯誤來自哪裏。你會在哪一行發生異常?發生異常時,您是否嘗試使用調試器來檢查連接的狀態? – Steve