Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created an app with C# WindowsForm and Sqlite. I can delete, but I can't insert records.

I use one connectionString, and I check my data and sql query for insert.

All of this seems OK, but I get an error such as this:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.SQLite.dll

Additional information: Operation is not valid due to the current state of the object.


and this is my InsertButton Click code :

C#
if (catID == 0 || txtRating.Text ==null || txtback.Text==null||txtgenerel.Text==null||txtimportant.Text==null|| FileName==null)
            {
                MessageBox.Show("لطفا اطلاعات را کامل وارد کنید");
            }
            else{
                CopyImage(FileName);
                string q2 = "INSERT INTO Persons (CatID,FullName,Rating,BackGround,GeneralDetale,ImportantDetale,PicName) VALUES (" + catID + ","+txtFullName.Text+"," + txtRating.Text + "," + txtback.Text + "," + txtgenerel.Text + "," + txtimportant.Text + "," + imgnumber + ")";
            SQLiteCommand cmd1 = new SQLiteCommand(q2, cn);
            //try0
            //{
                cn.Open();
                cmd1.ExecuteNonQuery();
                cn.Close();
                Refresh();
            //}
            //catch
            //{
            //    MessageBox.Show("An Error Was Occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            }


the Error is on this line : cn.open();
Posted
Updated 12-Dec-14 13:45pm
v2

Mostly, you left out the apostrophes to surround the string values.

The best solution to this is to use a parameterized statement.

C#
    CopyImage(FileName);
    string q2 = "INSERT INTO Persons (CatID,FullName,Rating,BackGround,GeneralDetale,ImportantDetale,PicName) VALUES ( @catID , @FullName , @Rating , @back , @txtgenerel , @important , @imgnumber)";
SQLiteCommand cmd1 = new SQLiteCommand(q2, cn);

cmd1.Parameters.AddWithValue ( "@CatID" , CatID ) ;
// Repeat for the other parameters


    cn.Open();
    cmd1.ExecuteNonQuery();
    cn.Close();
 
Share this answer
 
v2
Comments
[no name] 12-Dec-14 19:22pm    
Boah why I don't see that. My 5.
Answer 1 explain about your next issue you will face when you fix connection issue. You may have sharing the same connection and when you try to open the connection it may be already open or some other state which can't do the open operation. All these issue because of the shared connection you better follow the below practice.
C#
string sql = "INSERT INTO Persons (CatID,FullName,Rating,BackGround,GeneralDetale,ImportantDetale,PicName) VALUES ( @catID , @FullName , @Rating , @back , @txtgenerel , @important , @imgnumber)";
//don't reuse same connection over different methods, create new when you need
//and dispose it after use, using block will do it for you
using ( var oConn = new SqliteConnection (connectionString ) )
using( var oCmd = new SqliteCommand ( sql, oConn ))
{
    oConn.Open ();
    //set all parameter values

    //excute commad
    oCmd.ExecuteNonQuery ();
}
 
Share this answer
 
v2
Comments
PIEBALDconsult 12-Dec-14 22:12pm    
You may be right about the connection already being open, but reusing a connection a connection is prefereable to creating a new one all the time.
DamithSL 12-Dec-14 22:23pm    
yeh, my explanation seems to be misleading, I wanted to tell that sharing connection objects can be caused this kind of issues unless handled it properly. thanks

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