Hai
In our project we are using transactions.Between begin transaction and commit transaction a lot of database accessing is doing like selecting data from database many times,and for that we are using a common function selectdata().As the function selectdata() is a common function,we want to use this between transaction and sometime after commit transaction also.So now we are using two select data functions selectdata() and selectdatatrans();
SqlTransaction Transaction = null;
public void BeginTransaction()
{
DBConnection();
Transaction = sqlCon.BeginTransaction();
}
public SqlConnection DBConnection()
{
try
{
string strCon = string.Empty;
var configFile = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
var settings = configFile.GetSection("appSettings") as AppSettingsSection;
if (settings != null)
strCon =settings.Settings["MYCON"].Value;
else
strCon = ConfigurationManager.AppSettings["MYCON"].ToString();
if (sqlCon.State == ConnectionState.Open)
{
sqlCon.Close();
}
sqlCon.ConnectionString = strCon.Replace("pwd=","pwd=Gnusma$1");
sqlCon.Open();
SqlConnectionStringBuilder connBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
connBuilder.ConnectionString = strCon;
strServerName = connBuilder.DataSource; //-> this gives you the Server name.
strDataBaseName = connBuilder.InitialCatalog; // this gives you the DataBase name.
return sqlCon;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
public DataTable SelectData(string strQuery)
{
SqlCommand objCmd = new SqlCommand(strQuery, DBConnection());
DataTable dt = new DataTable();
SqlDataAdapter objSda = new SqlDataAdapter(objCmd);
objSda.Fill(dt);
return dt;
}
public DataTable SelectDataTrans(string strQuery)
{
SqlCommand objCmd = new SqlCommand(strQuery, sqlCon, Transaction);
DataTable dt = new DataTable();
SqlDataAdapter objSda = new SqlDataAdapter(objCmd);
objSda.Fill(dt);
return dt;
}
Now the problem is ,we need only one function for selecting data which we can use with transaction and with out transaction.If we are using selectdata() in between transaction, then when execute commit transaction() then error occuring as the transaction does not exist.
If we use selectdatatrans() outside transaction then some error comming as connection string property is not initialized.
Please help me.