我已经搜索过错误
连接字符串属性尚未初始化。
在谷歌以及Stack Overflow上但无法找到解决方案。我已经创建了一个用于与数据库交互的数据库类,所有相关代码都写在这个文件中。问题是相同的代码在其他页面上运行良好,它只是在名为“addevent.aspx”的页面上不起作用我不明白为什么它没有正常运行。
以下是我在database.cs文件中创建的方法
SitePaymentReportByBranch = function () {
$('#btnprintSitePaymentByBranch').on('click', function (e) {
e.preventDefault();
if ($("#form1").validationEngine('validate')) {
var _employerID = "";
if ($('#cmbEmployerSitPaymentByParameter :selected').text() == "-Select-") {
alert('Plz Select Employer');
}
var url = '/Reports/frmSitePayment.aspx?_EmployerID=' + $('#cmbEmployerSitPaymentByParameter :selected').val() + '&_Formdate=' + $("#formdate").val() + '&_Todate=' + $("#todate").val() +'_type=1';
commonStartup.openReportWindow(url);
}
});
},
我根本不理解这个错误的逻辑及其发生。打开连接时出现此错误
如何调用这些方法,我已经在AddEvent方法之外创建了一个database.cs类的对象,对象名为mydb
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; }
}
答案 0 :(得分:1)
这是太复杂的解决方案......这将解决您的理解问题和不必要的代码行:
溶液:
namespace Stackoverflow
{
public static class Solution
{
static readonly string _connectionStringName =
@"mainConnectionStringName";
static readonly string _connectionString =
_connectionStringName.getConnectionString();
// string extended method like .ToLower() or .Trim()
public static string getConnectionString(
this string connectionStringName)
{
return
System.
Configuration.
ConfigurationManager.
ConnectionStrings[connectionStringName].
ConnectionString;
}
public static object SqlExecute(
string connectionStringName,
string storedProcedureName,
System
.Collections
.Generic
.Dictionary<string, object> parameters,
bool isScalar)
{
object result = null;
using (System
.Data
.SqlClient
.SqlConnection connection =
new System.Data.SqlClient.SqlConnection(
string.IsNullOrWhiteSpace(connectionStringName)
? _connectionString
: connectionStringName.getConnectionString()))
if (connection != null)
using (System.Data.SqlClient.SqlCommand command =
new System.Data.SqlClient.SqlCommand()
{
CommandText = storedProcedureName,
CommandType = System
.Data
.CommandType
.StoredProcedure,
Connection = connection
})
if (command != null)
{
if (parameters != null)
foreach (System
.Collections
.Generic
.KeyValuePair<string, object>
pair in parameters)
command.Parameters.AddWithValue(
pair.Key, pair.Value);
command.Connection.Open();
result = isScalar
? command.ExecuteScalar()
: command.ExecuteNonQuery();
if (command.Connection.State ==
System.Data.ConnectionState.Open)
command.Connection.Close();
}
return result;
}
}
}
用法:
namespace SomeNamespace
{
public sealed class SomeClass
{
public int Example()
{
return (int)Stackoverflow
.Solution
.SqlExecute(
@"anyConnectionStringName", // or null for main connection string
@"anyStoredProcedureName",
new System
.Collections
.Generic
.Dictionary<string, object>()
{
{ @"field0", "value" },
{ @"field1", -1.5 },
{ @"field2", System.DateTime.Now },
{ @"field3", 3.5 },
{ @"field4", 7 },
},
false // for ExecuteNonQuery or true for ExecuteScalar
);
}
}
}