Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code for the insert query in c#.net with mysql db:
C#
private void btnsave_Click(object sender, EventArgs e)
      {
         string test = "@name,@age,@sex,@refby,@refno,@date,@hemoglobin,@rbc_count,@pcv,@mcv,@mch,@mchc,@rdw,@total_wbc_count,@polymorphs,@lymphocyts,@monocytes,@eosinophils,@Basophils,@Normoblasts,@Lymphoblasts,@Prolymphocytes,@Myeloblasts,@Promyeloblasts,@Myelocytes,@Metamyelocytes,@BandCells,@esr,@Plateletcount,@plateletsmear1,@PlateletSmearFg,@Bloodgroup,@rh,@AEC,@ReticCount,@Sicklingtest_Metabisulphite,@Sicklingtest_dithionite,@FetalHb,@HbA2,@mp1,@ThickThinSmear,@ThickThinSmearFg,@ps_rbcseries,@ps_rbcseriesfg,@ps_wbcseries,@ps_wbcseriesfg,@ps_platelet,@CoombsTest_Direct,@CoombsTest_Indirect,@remarks,@remarksfg,@timeofcollection,@correctedtc,@ESR2hr,@MPQBC,@MPMethod,@Morpho1,@Density1,@Morpho2,@Density2,@Morpho3,@Density3,@Morpho4,@Density4,@Morpho5,@Density5,@Densitynm,@BT,@CT,@Anisocytosis,@Hypochromia,@Microcytosis,@Macrocytosis,@Poikilocytosis,@Polychromesia,@TargetCell,@TearCell,@SickleCell,@PRNFLG,@New1,@New2,@New3,@CorrectedRetic,@Sno,@HBF,@bondtransfusion,@History,@ElectroComments,@ElectroCommentsfg,@RapidCard,@ReportExam,@BloodMp,@blood,@Pfalci,@FalciR,@Pvivex,@PVR,@BYstd,@wbcgraph,@pltgraph,@rbcgraph,@prgid,@ps";
          string[] field = test.Split(',');
          int size = field.Length;
          Control[] formcontrol = new Control[103] { txtname, txtage, txtage, txtref, txtaccno, dtp1, txtheamo, txtrcbcount, txtpcv, txtmcv, txtmch, txtmchc, txtrdw, txtwbc, txtpolymorphs, txtlymp, txtmono, txteosi, txtbaso, txtNormoblasts, txtLymphoblasts, txtProlymphocytes, txtMyeloblasts, txtPromyeloblasts, txtMyelocytes, txtMetamyelocytes, txtband, txtesr, txtplate, txtblank, txtblank, cbbloodgrp, cbrh, txtaec, txtretic, cbsicking1, cbsicking2, txtfetal, txthba, txtthick, txtthick, txtplateletsmearfg, txtrbcseries, txtrbcfg, txtwbc, txtwbcfg, txtplateletson, cbdirect, cbindirect, txtremarks, txtremarkfg, dtptime, txtCorrectedTLC, txtesr2, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtbt, txtct, txtAnisocytosis, txtHypochromia, txtMicrocytosis, txtMacrocytosis, txtPoikilocytosis, txtPolychromesia, txtTargetCell, txtTearCell, txtSickleCell, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank, txtblank };
       str = "INSERT INTO cbc_new(name,age,sex,refby,refno,date,hemoglobin,rbc_count,pcv,mcv,mch,mchc,rdw,total_wbc_count,polymorphs,lymphocyts,monocytes,eosinophils,Basophils,Normoblasts,Lymphoblasts,Prolymphocytes,Myeloblasts,Promyeloblasts,Myelocytes,Metamyelocytes,BandCells,esr,Plateletcount,plateletsmear1,PlateletSmearFg,Bloodgroup,rh,AEC,ReticCount,Sicklingtest_Metabisulphite,Sicklingtest_dithionite,FetalHb,HbA2,mp1,ThickThinSmear,ThickThinSmearFg,ps_rbcseries,ps_rbcseriesfg,ps_wbcseries,ps_wbcseriesfg,ps_platelet,CoombsTest_Direct,CoombsTest_Indirect,remarks,remarksfg,timeofcollection,Heading,correctedtc,ESR2hr,MPQBC,MPMethod,Morpho1,Density1,Morpho2,Density2,Morpho3,Density3,Morpho4,Density4,Morpho5,Density5,Densitynm,BT,Btnm,CT,Ctnm,esr2hrnm,Anisocytosis,Hypochromia,Microcytosis,Macrocytosis,Poikilocytosis,Polychromesia,TargetCell,TearCell,SickleCell,PRNFLG,New1,New2,New3,CorrectedRetic,Sno,HBF,bondtransfusion,History,ElectroComments,ElectroCommentsfg,RapidCard,ReportExam,BloodMp,blood,Pfalci,FalciR,Pvivex,PVR,BYstd,wbcgraph,pltgraph,rbcgraph,prgid,ps) VALUES(@name,@age,@sex,@refby,@refno,@date,@hemoglobin,@rbc_count,@pcv,@mcv,@mch,@mchc,@rdw,@total_wbc_count,@polymorphs,@lymphocyts,@monocytes,@eosinophils,@Basophils,@Normoblasts,@Lymphoblasts,@Prolymphocytes,@Myeloblasts,@Promyeloblasts,@Myelocytes,@Metamyelocytes,@BandCells,@esr,@Plateletcount,@plateletsmear1,@PlateletSmearFg,@Bloodgroup,@rh,@AEC,@ReticCount,@Sicklingtest_Metabisulphite,@Sicklingtest_dithionite,@FetalHb,@HbA2,@mp1,@ThickThinSmear,@ThickThinSmearFg,@ps_rbcseries,@ps_rbcseriesfg,@ps_wbcseries,@ps_wbcseriesfg,@ps_platelet,@CoombsTest_Direct,@CoombsTest_Indirect,@remarks,@remarksfg,@timeofcollection,@correctedtc,@ESR2hr,@MPQBC,@MPMethod,@Morpho1,@Density1,@Morpho2,@Density2,@Morpho3,@Density3,@Morpho4,@Density4,@Morpho5,@Density5,@Densitynm,@BT,@CT,@Anisocytosis,@Hypochromia,@Microcytosis,@Macrocytosis,@Poikilocytosis,@Polychromesia,@TargetCell,@TearCell,@SickleCell,@PRNFLG,@New1,@New2,@New3,@CorrectedRetic,@Sno,@HBF,@bondtransfusion,@History,@ElectroComments,@ElectroCommentsfg,@RapidCard,@ReportExam,@BloodMp,@blood,@Pfalci,@FalciR,@Pvivex,@PVR,@BYstd,@wbcgraph,@pltgraph,@rbcgraph,@prgid,@ps)";
          cmd = new MySqlCommand(str, Classmysql.conmain);
          int i = 0;
          foreach (string w in field)
          {
              cmd.Parameters.AddWithValue(w.Trim(), formcontrol[i].Text);

              i++;
          }
          cmd.ExecuteNonQuery();
          MessageBox.Show("saved");
     }

error details:
C#
MySql.Data.MySqlClient.MySqlException was unhandled
  Message="Column count doesn't match value count at row 1"
  Source="MySql.Data"
  ErrorCode=-2147467259
  Number=1136

please help to resolve this problem its urgent.
thanks in advance.
Posted

hello first you must check your query properly....

Btnm,CT,Ctnm,esr2hrnm for these values you are not pass parameters
 
Share this answer
 
you are missing to pass the parameter to

Heading->@Heding



this is at here: "Heading",correctedtc,ESR2hr,MPQBC,
you are missing this one
 
Share this answer
 
v2
Comments
Member 8825505 8-Mar-13 2:21am    
thabks for help i have changed query but again it gives same error please help..
str = "INSERT INTO cbc_new(nm,age,sex,refby,refno,date,hemoglobin,rbc_count,pcv,mcv,mch,mchc,rdw,total_wbc_count,polymorphs,lymphocyts,monocytes,eosinophils,Basophils,Normoblasts,Lymphoblasts,Prolymphocytes,Myeloblasts,Promyeloblasts,Myelocytes,Metamyelocytes,BandCells,esr,Plateletcount,plateletsmear1,PlateletSmearFg,Bloodgroup,rh,AEC,ReticCount,Sicklingtest_Metabisulphite,Sicklingtest_dithionite,FetalHb,HbA2,mp1,ThickThinSmear,ThickThinSmearFg,ps_rbcseries,ps_rbcseriesfg,ps_wbcseries,ps_wbcseriesfg,ps_platelet,CoombsTest_Direct,CoombsTest_Indirect,remarks,remarksfg,timeofcollection,correctedtc,ESR2hr,MPQBC,MPMethod,Morpho1,Density1,Morpho2,Density2,Morpho3,Density3,Morpho4,Density4,Morpho5,Density5,Densitynm,BT,CT,Anisocytosis,Hypochromia,Microcytosis,Macrocytosis,Poikilocytosis,Polychromesia,TargetCell,TearCell,SickleCell,PRNFLG,New1,New2,New3,CorrectedRetic,Sno,HBF,bondtransfusion,History,ElectroComments,ElectroCommentsfg,RapidCard,ReportExam,BloodMp,blood,Pfalci,FalciR,Pvivex,PVR,BYstd,wbcgraph,pltgraph,rbcgraph,prgid,ps) VALUES(?nm,?age,?sex,?refby,?refno,?date,?hemoglobin,?rbc_count,?pcv,?mcv,?mch,?mchc,?rdw,?total_wbc_count,?polymorphs,?lymphocyts,?monocytes,?eosinophils,?Basophils,?Normoblasts,?Lymphoblasts,?Prolymphocytes,?Myeloblasts,?Promyeloblasts,?Myelocytes,?Metamyelocytes,?BandCells,?esr,?Plateletcount,?plateletsmear1,?PlateletSmearFg,?Bloodgroup,?rh,?AEC,?ReticCount,?Sicklingtest_Metabisulphite,?Sicklingtest_dithionite,?FetalHb,?HbA2,?mp1,?ThickThinSmear,?ThickThinSmearFg,?ps_rbcseries,?ps_rbcseriesfg,?ps_wbcseries,?ps_wbcseriesfg,?ps_platelet,?CoombsTest_Direct,?CoombsTest_Indirect,?remarks,?remarksfg,?timeofcollection,?correctedtc,?ESR2hr,?MPQBC,?MPMethod,?Morpho1,?Density1,?Morpho2,?Density2,?Morpho3,?Density3,?Morpho4,?Density4,?Morpho5,?Density5,?Densitynm,?BT,?CT,?Anisocytosis,?Hypochromia,?Microcytosis,?Macrocytosis,?Poikilocytosis,?Polychromesia,?TargetCell,?TearCell,?SickleCell,?PRNFLG,?New1,?New2,?New3,?CorrectedRetic,?Sno,?HBF,?bondtransfusion,?History,?ElectroComments,?ElectroCommentsfg,?RapidCard,?ReportExam,?BloodMp,?blood,?Pfalci,?FalciR,?Pvivex,?PVR,?BYstd,?wbcgraph,?pltgraph,?rbcgraph,?prgid,?ps)";
Member 8825505 8-Mar-13 2:37am    
thanks for helping
Check your no. of fields and no. of values. Both fields and values should be equal.
 
Share this answer
 
Comments
Member 8825505 8-Mar-13 0:31am    
i am 99% sure no. of fields and no. of values are same.
phil.o 8-Mar-13 4:15am    
Then you have to make that number a hundred percent.

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