I think you should never use the sqldatasource in your page.
Try this way
- Firstly we will create a Class Lib. In this Class Lib, we will create different class file which will improve the Reusability.
1.1 Add the Class Lib (To go File -> New Project -> Add ClassLibrary)
- Now we will create the db connection class
2.1 Add the class file in the class lib project as'Classbll'
public class DBConnection
{
#region"private variables"
private string _sConnectionString = ConfigurationManager.AppSettings["DBConnectString"].ToString();
private static string _sErrorMessage = string.Empty;
#endregion
#region "Public Properties"
public string DataConnectionString
{
get
{
return _sConnectionString;
}
}
public string ErrorMessage
{
get
{
return _sErrorMessage;
}
set
{
_sErrorMessage = value;
}
}
#endregion
}
2.2 Define the connection string in the web.config in the main project
<appSettings>
<add key="DBConnectString" value="SERVER=XYZSERVER;UID=XXYY;Password=ZZZXXX;Database=DBXXYYZZ;"/>
</appSettings>
3 Now create another class file in the same class lib project..
Let us say File named as'FillCommon.cs'
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Data;
using System.Web.UI.WebControls;
namespace Clssbll
{
public class FillCommon : DBConnection
{
#region "Private variables"
private string _smRoleMasterRoleID;
#endregion
#region "Public variables"
public string mRoleMasterRoleID
{
get { return _smRoleMasterRoleID; }
set { _smRoleMasterRoleID = value; }
}
#endregion
#region "Public Methods"
public DataSet GetmRoleMaster_infoSingle()
{
try
{
DataSet oDS = new DataSet();
SqlParameter[] oParam = new SqlParameter[1];
oParam[0] = new SqlParameter("@m_RoleID", _smRoleMasterRoleID);
oDS = SqlHelper.ExecuteDataset( DataConnectionString, CommandType.StoredProcedure, "SelectmRoleMaster", oParam);
return oDS;
}
catch (Exception e)
{
ErrorMessage = e.Message;
return null;
}
}
Now add Classbll(Class Lib) in the main project reference or bin.
In the main project class file
You can use this anywhere you want'Using Classbll' namespace
If you find it useful, please mark it as your answer else let me know...