locked
Inserting issue RRS feed

  • Question

  • User1052662409 posted

    Hi All I am unable to insert session variable into table. my code is like "insert into tblComment(DP,DC,OP,OC,CP,CC,FP,FC,CLRP,CLRC,IP,IC,user)values('" + txtP1.Text + "','" + txtC1.Text + "','" + txtP2.Text + "','" + txtC2.Text + "','" + txtP3.Text + "','" + txtC3.Text + "','" + txtP4.Text + "','" + txtC4.Text + "','" + txtP5.Text + "','" + txtC5.Text + "','" + txtP6.Text + "','" + txtC6.Text + "',' "+Session["uname"].ToString()+" ')"; if I remove "user" from query then it would save all values but when I put user n it's value in query then it gives

    error "Syntax error in INSERT INTO statement."[I m using MS access]

    when the error is comming I can see the value of "Session["uname"].ToString()" in Call Stack. It shows right value. But unbale to insert into table.

     

    pls help me out

    Thanx to all the members

    Thursday, January 22, 2009 6:18 AM

Answers

  • User289008742 posted

    first of all did u put a break point and see whether u r getting proper insert statement or not..? Then try thsi (string)(Session["id"]])

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 1:11 AM

All replies

  • User-1621240137 posted

    I suggest that you use parameters and not string concatenation. It will help you to detect where is syntax error. Read the following article http://www.mikesdotnetting.com/Article.aspx?ArticleID=26. Also you can take a look at the SqlEditor that allows you to create and test your queries at design time to avoid error when running your application. The SQL Editor is deployed with the FREE version of ADO.NET Accelerator software.

    Luis Ramirez.

    ADO.NET Accelerator code snippet.

    1    OrderedDictionary parameterValue = new OrderedDictionary();
    2    parameterValue.Add("CustomerName", "Scott");
    3    int affectedRows = SqlNetFramework.Management.DbManager.Instance.ExecuteNonQuery(1, "Northwind", parameterValue); 
    
     
    Thursday, January 22, 2009 4:47 PM
  • User1052662409 posted

    But I m using a class file in App_Code folder how can I add parameters to OleDbcommand my class file is here

    SO please let me how to add parameters to this class file. It wud be better if u write a insert query after defining parameters in this class

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;

    /// <summary>
    /// Summary description for gaurav
    /// </summary>
    public class gaurav
    {
        public OleDbConnection con;
        public OleDbCommand com;

        private void Open_Connection()
        {
            string conStr=ConfigurationManager.ConnectionStrings["contest"].ConnectionString;
            if(con==null)
            {
                con=new OleDbConnection(conStr);
                con.Open();
            }
            com=new OleDbCommand();
            com.Connection=con;
        }

        private void Close_Connection()
        {
            con.Close();
        }

        private void Dispose_Connection()
        {
            if(con!=null)
            {
                con.Dispose();
                con=null;
            }
        }
        public int Execute_Sql(string strSql)
        {
            this.Open_Connection();
            com.CommandText=strSql;
            com.CommandType=CommandType.Text;
            int row=com.ExecuteNonQuery();
            this.Close_Connection();
            this.Dispose_Connection();
            return row;
        }

        public DataSet My_Dataset(string strSql)
        {
            this.Open_Connection();
            OleDbDataAdapter da=new OleDbDataAdapter(strSql,con);
            DataSet ds=new DataSet();
            da.Fill(ds);
            this.Close_Connection();
            this.Dispose_Connection();
            return ds;
        }
       
        public DataTable My_Datatable(string strSql)
        {
            this.Open_Connection();
            OleDbDataAdapter da=new OleDbDataAdapter(strSql,con);
            DataTable dt=new DataTable();
            da.Fill(dt);
            this.Close_Connection();
            this.Dispose_Connection();
            return dt;
        }

        public bool Record_Is_Exit(string strSql)
        {
            this.Open_Connection();
            com.CommandText=strSql;
            com.CommandType=CommandType.Text;
            int val=Convert.ToInt32(com.ExecuteScalar());
            this.Close_Connection();
            this.Dispose_Connection();
            if(val==1)
                return
                    true;
            else
                return
                    false;
        }

      




        public gaurav()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }

     

    Thanx A LOT

    Friday, January 23, 2009 12:05 AM
  • User1024576976 posted

    GET THE COMMANDTEXT FOR THIS QUERY AND POST IT HERE.

    YOU CAN ALSO PASTE THE COMMAND TEXT IN MSACCESS AND CHECK WHAT THE PROBLEM IS.

     

    THANKS

     

    Friday, January 23, 2009 12:16 AM
  • User1052662409 posted

    Thanx Dear for replying. How to get commandText

    It is

    string strSql = "insert into tblComment(DP,DC,OP,OC,CP,CC,FP,FC,CLRP,CLRC,IP,IC,user)values('"+txtP1.Text+"','"+txtC1.Text+"','"+txtP2.Text+"','"+txtC2.Text+"','"+txtP3.Text+"','"+txtC3.Text+"','"+txtP4.Text+"','"+txtC4.Text+"','"+txtP5.Text+"','"+txtC5.Text+"','"+txtP6.Text+"','"+txtC6.Text+"','"+Session["uid"].ToString()+"')";

     

    it wud be better if u write this with parameter. Thanx u so much

    Friday, January 23, 2009 12:42 AM
  • User289008742 posted

    first of all did u put a break point and see whether u r getting proper insert statement or not..? Then try thsi (string)(Session["id"]])

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 1:11 AM
  • User1052662409 posted

     THANX U SO MUCH DEAR[:)]

    Friday, January 23, 2009 1:23 AM
  • User1052662409 posted

      THANX U SO MUCH DEARSmile

    Friday, January 23, 2009 1:24 AM