Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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();
        }
    }
}
Posted
Updated 17-May-13 4:24am
v2
Comments
Richard C Bishop 17-May-13 10:27am    
Try putting brackets [] around the column names in your select statement.

1 solution

Check your table definition.
Either you named it wrong in your table, or in your C# code, but it is saying that From_SLA_Non_Compliance_Report_Months_Parameter is not a column in your table. (You may have swapped the vowels in "Compliance" or something - I do it all the damn time...)

If it's not a column, you can't insert into it. Simples!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900