In my asp.net application I have added the connection string in each page Page_Load function and declared the string as public in top of the page.
I know this is wrong thing. The whole application is running the same connection, so I want to declare the connection string in only one place. Where should I declare the connection string? How can I access the connection string in each Connection?
public string connstring = "";
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack != true)
{
connstring = ConfigurationManager.ConnectionStrings["connString"].ToString();
MySqlConnection connection = new MySqlConnection(connstring);
// ----
}
}
You already have defined the connection string in
web.config, you don’t need to define it again in each page, just use the connection string from ConfigurationManager instead of creating a new string variable on each page.its better if you check for null prior to using the connection string.