Click here to Skip to main content
14,774,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am importing exel file into database but giving error The parameterized query '(@OrderID int,@CustomerName varchar(20),@Address varchar(50),@Em' expects the parameter '@OrderID', which was not supplied.

DataTable dt = new DataTable();
        dt.Columns.Add("OrderID");
        dt.Columns.Add("CustomerName");
        dt.Columns.Add("Address");
        dt.Columns.Add("Email");
        dt.Columns.Add("Total");
        string path = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Imported file/" + path));
        path = Server.MapPath("~/Imported file/" + path);
        TextFieldParser txtp = new TextFieldParser(path);
        txtp.Delimiters = new string[] { "," };
        txtp.TrimWhiteSpace = true;
        
        while (!txtp.EndOfData)
        {
            string[] fields = txtp.ReadFields();
            dt.Rows.Add(fields.Equals("OrderID"));
            dt.Rows.Add(fields.Equals("CustomerName"));
            dt.Rows.Add(fields.Equals("Address"));
            dt.Rows.Add(fields.Equals("Email"));
            dt.Rows.Add(fields.Equals("Total"));

        }

        con = Connectivity.GetConnection();
        string import = "Insert into Customer (OrderID,CustomerName,Address,Email,Total) values(@OrderID,@CustomerName,@Address,@Email,@Total)";
        SqlCommand cmd = new SqlCommand(import, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        //cmd.Parameters.AddWithValue("@OrderID", "OrderID");
        //cmd.Parameters.AddWithValue("@CustomerName","CustomerName");
        //cmd.Parameters.AddWithValue("@Address",  "Address");
        //cmd.Parameters.AddWithValue("@Email",  "Email");
        //cmd.Parameters.AddWithValue("@Total", "Total");

        //SqlCommand cmd = new SqlCommand();
        //cmd.CommandType = CommandType.Text;
        //cmd.CommandText = import;
        //cmd.Connection = con;

        cmd.Parameters.Clear();
        cmd.Parameters.Add("@OrderID", SqlDbType.Int);
        cmd.Parameters.Add("@CustomerName", SqlDbType.VarChar, 20, "CustomerName");
        cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50, "Address");
        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 50, "Email");
        cmd.Parameters.Add("@Total", SqlDbType.Float);
        // SqlDataAdapter da = new SqlDataAdapter();
        cmd.ExecuteNonQuery();
        // da.InsertCommand = cmd;
        int result = da.Update(dt);
Posted
Updated 20-Jan-14 23:42pm
v2

I don't need to read your code, I just need to read the error. It means what it says. In fact, all your parameters have not been given values, and so will all fail.

Bulk insert is the way to import an entire csv in one go. Or you could read my articles on passing abitrary sets of data to SQL Server.
   
Try this


while (!txtp.EndOfData)
        {
            string[] fields = txtp.ReadFields();
            var newRow = dt.NewRow();
            newRow.ItemArray = fields;
            dt.Rows.Add(newRow);

        }
   

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