Click here to Skip to main content
16,017,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am having a strange problem in mysql insert using dataadapter and datatable.... i am not able to figure it out what is the actual problem ..

Only one row insert and then error givien

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near;SP_InsertCTC; SP_InsertCTC; SP_InsertCTC; SP_InsertCTC; at line 1



C#
public void BulkCopyCTC(List<EmployeeDet> list)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("employee_id",typeof( System.String)));
        dt.Columns.Add(new DataColumn("employee_name", typeof(System.String)));
        dt.Columns.Add(new DataColumn("emp_ctc",typeof( System.Decimal)));


        foreach (EmployeeDet item in list)
        {
            DataRow dr = dt.NewRow();
            dr["employee_id"] = item.GetID();
            dr["employee_name"] = item.GetName();
            dr["emp_ctc"] = item.GetCTC();
            dt.Rows.Add(dr);
        }
       

        MySqlConnection con = new MySqlConnection(new ConnectionUtils().GetConnectionString());
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        MySqlCommand cmd = new MySqlCommand("SP_InsertCTC", con);
        cmd.CommandType = CommandType.StoredProcedure;


        cmd.UpdatedRowSource = UpdateRowSource.None;
      



        cmd.Parameters.Add("?e_id", MySqlDbType.String).SourceColumn= "employee_id";
        cmd.Parameters.Add("?e_name", MySqlDbType.String).SourceColumn=  "employee_name";
        cmd.Parameters.Add("?emp_ctc", MySqlDbType.Decimal).SourceColumn=  "emp_ctc";
      

        MySqlDataAdapter da = new MySqlDataAdapter();
        da.InsertCommand = cmd;
        da.UpdateBatchSize = 100;
        int records = da.Update(dt);
        Response.Write("<script>alert('inserted " +  records +" Rows')</script>");
        con.Close();
    }
}




Table

CREATE TABLE `employee_ctc` (
	`emp_id` VARCHAR(20) NULL DEFAULT NULL,
	`emp_name` VARCHAR(50) NULL DEFAULT NULL,
	`CTC` DECIMAL(10,2) NULL DEFAULT NULL
)

Store Procedure

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_InsertCTC`( e_id VARCHAR(20), e_name VARCHAR(50), emp_ctc DECIMAL(10,2))
BEGIN

INSERT INTO Employee_CTC(emp_id,emp_name,CTC) VALUES(e_id ,e_name,emp_ctc);
END $$

DELIMITER ;
Posted
Updated 4-Oct-16 19:16pm
v2
Comments
Member 832086 27-Oct-16 7:47am    
Hi Dear ,

I have a question .
I use the same example in the MySQL project .
But , da.Update(dt) doesn't working corectly .
I get this result from da.Update(dt) function : int records = 0;
records number is zero .
Why?

Thanks
Best Regards
Member 832086 27-Oct-16 7:51am    
I have a question .
MySqlDataAdaptor Update not updating database ?
I use the same example in the MySQL project .
But , da.Update(dt) doesn't working corectly .
I get this result from da.Update(dt) function : int records = 0;
records number is zero .
Why?

Thanks
Best Regards

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