Skip to content Skip to sidebar Skip to footer

How To Add Key Value From Web.config File To Database By Uploading Excel Sheet?

I uploaded an excel sheet and inserted data in database. There are two columns names as financial year and financial quarter. I want to insert data from web.config for these two co

Solution 1:

This will insert the record of your AppsetingValue:

string consString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
       static  String AppSetting1=ConfigurationManager.AppSettings["keyFinancialYr"].ToString();
       static  String AppSetting2=ConfigurationManager.AppSettings["keyFinancialQtr"].ToString();
        String QueryStr = "insert into yourTable(Col1,Col2)values('" + AppSetting1 + "','" + AppSetting2 + "')";

        publicForm1()
        {
            InitializeComponent();
        }

        privatevoidForm1_Load(object sender, EventArgs e)
        {
            ExecuteQuery(consString,QueryStr);
        }

        publicintExecuteQuery(String connectionString,string query)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = query;
                    cmd.CommandType = CommandType.Text;
                    int result = cmd.ExecuteNonQuery();
                    return result;
                }
            }
        }

If your table column keyFinancialQtr is integer you can parse the AppSetting2 to int.

Just change the col1,col2 as your real column name.

Post a Comment for "How To Add Key Value From Web.config File To Database By Uploading Excel Sheet?"