Problem inserting data .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arial
    New Member
    • Sep 2007
    • 109

    Problem inserting data .net

    Hi all I am getting this error message while try to insert data using my .net webform.

    Code:
    Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near '1'.
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
    
    Stack Trace: 
    
    
    [SqlException: Line 1: Incorrect syntax near '1'.]
       System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +180
       QualityQI.WebForm1.btnadd_Click(Object sender, EventArgs e) +1756
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain() +1292
    I'll appreciate all yours help.

    Thank You,

    and my insert command is:

    Code:
    SqlCommand cmd = new SqlCommand();
    			cmd.Connection = sqlConnection1;
    			cmd.Connection.Open();
    			cmd.CommandText = "insert into gingercqi (QI_TRACK,DATE_RCV,INC_NUM,INC_DATE,INC_TIME,QANUM,BASE_HSP,ISSUE_IDENTIFY,MICN_INVOLVED,1ST_RESP1,1ST_RESP2,TRANSPORT1,INVESTIGATE,DATE_ASSIGN,CNE,DATE_CNE,INVPARTY1,INVPARTY2,INVPARTY3,INVPARTY4,OTHERINV,ISSUECATEGORY,ISSUEMEMO,QILEVEL,PACREPORTABLE,PACOUTCOME,CLOSEDATE,RES_CATEGORY,RES_MEMO,DISCIPLINE,DISC_TYPE,MD_REVIEW,POLICY_VIOLATION) VALUES ('"+txtqitrack.Text+"','"+Datercv.SelectedDate+"','"+ txtincnum.Text+"','"+ Dateinc.SelectedDate+"','"+txtinctime.Text+"','"+txtqanum.Text+"','"+dpbasehsp.SelectedValue+"','"+dpissueidentify.SelectedValue+"','"+txtmicninvl.Text+"','"+txt1stresp1.Text+"','"+txt1stresp2.Text+"','"+txttransport1.Text+"','"+dpinvestigate.SelectedValue+"','"+Dateasn.SelectedDate+"','"+dpcne.SelectedValue+"','"+Datecne.SelectedDate+"','"+dpinvlparty1.SelectedValue+"','"+dpinvlparty2.SelectedValue+"','"+dpinvlparty3.SelectedValue+"','"+dpinvlparty4.SelectedValue+"','"+txtotherinvl.Text+"','"+dpissuecate.SelectedValue+"','"+txtissuememo.Text+"','"+dpqilevel.SelectedValue+"','"+chkpacreport.Checked+"','"+txtpacoutcome.Text+"','"+Dateclose.SelectedDate+"','"+dpreslcate.SelectedValue+"','"+txtresolutionmemo.Text+"','"+chkdiscipline.Checked+"','"+txtdisctype.Text+"','"+chkmdreview.Checked+"','"+chkpolicyvio.Checked+"')";
    			cmd.ExecuteNonQuery();
    			cmd.Connection.Close();
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Hi arial,
    As a good gesture, could you please run the program in debug mode, place a break point at where the commandtext is being created, and get the actaul text generated there.

    Will help out debugging that.
    It seems to be a syntax error with the sql statement

    edit: Could you replace "1ST_RESP1,1ST_ RESP2" in the field names to
    [1ST_RESP1],[1ST_RESP2]

    the square brackets lets sql know that it is a field name (field names generally do not start with number, or contain spaces, but the square brackets skips these constraints)

    Comment

    • arial
      New Member
      • Sep 2007
      • 109

      #3
      Thank Shashi.

      You have been a great help.

      The error I am getting while inserting checkbox value.

      Which I am inserting like '"+chkmdreview. checked+"'.

      Is it a correct way to insert check box value?

      Thank You,

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        you will have to explicityly convert the checked value to zero or one.
        With the above way, it will be converted to "true" or "false", while what you need to do it convert it to 0 or 1

        Comment

        • arial
          New Member
          • Sep 2007
          • 109

          #5
          actually that is what I need to insert in database like true or false so, in database "T " for true and "F" for false.

          how can I accomplish this?

          Thank You,

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            If you represent true and false using a varchar 'T' or 'F'
            then use if statements !!!!

            Comment

            • arial
              New Member
              • Sep 2007
              • 109

              #7
              Thank You Shashi for you help.

              So, this is what I did.

              My datafield in database is char(1).

              I put in my code.
              Code:
              string md="";
              
              if(chkpolicyvio.checked)
                     md = "T";
              
              else
                  md = "F";
              insert into table (policyvio) values ('"+Convert.ToChar(md)+"')
              this gives me error message.

              Error converting data type varchar to numeric.

              Please help me.

              Thank You,

              Comment

              Working...