Monday, July 22, 2013

How to dynamically update web config file in asp.net using c#?

I want to configure my web config connection string in my aspx page. That mean I want to change my web.config file dynamically. User can change the connection string any time without touching the server. For that the below asp.net c# code will help you to change connection string , appsetting information and ect..  in dynamically.




private void SetConfigSettings()
        {
            try
            {
                // App setting settings
                System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

                //APPsetting value change
                System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["SupportFormat"];
                if (null != setting)
                    config.AppSettings.Settings["SupportFormat"].Value = txtFormat.Text.Trim();

                //APPsetting value change
                System.Configuration.KeyValueConfigurationElement Pathsetting = config.AppSettings.Settings["Path"];
                if (null != Pathsetting)
                    config.AppSettings.Settings["Path"].Value = txtPath.Text.Trim();


                //Connection string changes
                string newConnectionString = "Data Source=" + txtServer.Text + ";Initial Catalog='" + txtDatabaseName.Text.Replace("'", "") + "';User ID=" + txtUserName.Text + ";Password=" + txtPassword.Text + ";Persist Security Info=True;";

                //Configuration openWebConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
                ConnectionStringsSection sections = config.GetSection("connectionStrings") as ConnectionStringsSection;
                if (sections != null)
                {
                    sections.ConnectionStrings["ConString"].ConnectionString = newConnectionString;
                    ConfigurationManager.RefreshSection("ConString");

                }
                config.Save();

                lblMSG.Text = "Updated successfully!!!";
                lblMSG.ForeColor = Color.Green;
            }
            catch (System.Exception exc)
            {
                lblMSG.Text = exc.Message;
                lblMSG.ForeColor = Color.Red;
            }
        }

How to create simple error log file in c#.net


How to write error log in ASP.NET and C#

The below code will help you to capture error log information in the text file format. In the exception part pass the error message to ErrorLog function. In the errorlog method will capture the error information as a log file.



  // ASP.NET Error Log
       
using System.IO;

public void Log(string sMessage)
        {
            StreamWriter objSw = null;
            try
            {
                string sFolderName = Server.MapPath(@"\Logs\");
                if (!Directory.Exists(sFolderName))
                    Directory.CreateDirectory(sFolderName);
                string sFilePath = sFolderName + "Transaction.log";

                objSw = new StreamWriter(sFilePath, true);
                objSw.WriteLine(DateTime.Now.ToString() + " " + sMessage + Environment.NewLine);

            }
            catch (Exception ex)
            {
                // Log("Error -" + ex.Message);
            }
            finally
            {
                if (objSw != null)
                {
                    objSw.Flush();
                    objSw.Dispose();
                }
            }
        }




 //Windows application Error Log

using System.IO;
       
 public static void ErrorLog(string sMessage)
        {
            StreamWriter objSw = null;
            try
            {
                string sFolderName = Application.StartupPath + @"\Logs\";
                if (!Directory.Exists(sFolderName))
                    Directory.CreateDirectory(sFolderName);
                string sFilePath = sFolderName + "Error.log";

                objSw = new StreamWriter(sFilePath, true);
                objSw.WriteLine(DateTime.Now.ToString() + " " + sMessage + Environment.NewLine);

            }
            catch (Exception ex)
            {
                 // Comman.ErrorLog("Error -" + ex.Message);
            }
            finally
            {
                if (objSw != null)
                {
                    objSw.Flush();
                    objSw.Dispose();
                }
            }
        }