in both database table column and xls file column are same but they give error .Column 'From_SLA_Non_Compliance_Report_Months_Parameter' does not belong to table .please provide me solution.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using System.IO;
namespace WebApplication1.admin
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
MySqlConnection con = new MySqlConnection("Server=Localhost;DataBase=password1;user=root;password=nectar");
protected void btn_save(object sender, EventArgs e)
{
string file = FileUpload1.PostedFile.FileName;
file = Path.GetFileName(file);
string filepath = Server.MapPath("file") + file;
FileUpload1.SaveAs(filepath);
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string[] value = line.Split(';');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(';');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
string sql = "Insert into new_table(From_SLA_Non_Compliance_Report_Months_Parameter,Target,Unit,OM,Finance,InbndMRO,InbndRM,Maximo,Payroll,HILAllied,Hardware,Network,Software,DBA,OPM) values(@From_SLA_Non_Compliance_Report_Months_Parameter,@Target,@Unit,@OM,@Finance,@InbndMRO,@InbndRM,@Maximo,@Payroll,@HILAllied,@Hardware,@Network,@Software,@DBA,@OPM)";
foreach (DataRow r in dt.Rows)
{
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@From_SLA_Non_Compliance_Report_Months_Parameter", r["From_SLA_Non_Compliance_Report_Months_Parameter"]);
cmd.Parameters.AddWithValue("@Target", r["Target"]);
cmd.Parameters.AddWithValue("@Unit", r["Unit"]);
cmd.Parameters.AddWithValue("@OM", r["OM"]);
cmd.Parameters.AddWithValue("@Finance", r["Finance"]);
cmd.Parameters.AddWithValue("@InbndMRO", r["InbndMRO"]);
cmd.Parameters.AddWithValue("@InbndRM", r["InbndRM"]);
cmd.Parameters.AddWithValue("@Maximo", r["Maximo"]);
cmd.Parameters.AddWithValue("@Payroll", r["Payroll"]);
cmd.Parameters.AddWithValue("@HILAllied", r["HILAllied"]);
cmd.Parameters.AddWithValue("@Hardware", r["Hardware"]);
cmd.Parameters.AddWithValue("@Network", r["Network"]);
cmd.Parameters.AddWithValue("@Software", r["Software"]);
cmd.Parameters.AddWithValue("@DBA", r["DBA"]);
cmd.Parameters.AddWithValue("@OPM", r["OPM"]);
cmd.ExecuteNonQuery();
}
con.Close();
}
}
}