Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to insert in db in C# windows application here is my code:
C#
string str = "Data Source=|DataDirectory|\\Database1.sdf;Persist Security Info=False;";
             Qconnection.ConnectionString = str;
            Qcommand.Connection = Qconnection;
            Qconnection.Open();
            
              string commandText = "INSERT INTO order1 VALUES('10','20','o','u')";
                Qcommand.CommandText = commandText;
                Qcommand.CommandType = CommandType.Text;
              
               // Qcommand.Parameters.AddWithValue("@RID",10 );
               // Qcommand.Parameters.AddWithValue("@amount", orderColection.list[i].amount);
              //  Qcommand.Parameters.AddWithValue("@type", orderColection.list[i].type);
              //  Qcommand.Parameters.AddWithValue("@date", orderColection.list[i].date );
                //Qcommand.ExecuteNonQuery();
                Qcommand.ExecuteReader();
         
            Qconnection.Close();

but nothing will happen. and my table is empty
Posted
Comments
__TR__ 7-Sep-12 3:36am    
Use
Qcommand.ExecuteNonQuery();
instead of
Qcommand.ExecuteReader();.

i am using from try cache to execute insert command and in cache write this command:
C#
catch (Exception ex)
            {
 
                throw new Exception(ex.Message);
            }

but no message showing to me and nothing writed in tabel
 
Share this answer
 
I try to use Dataset to do the most basic functions (INSERT,EDIT,DELETE,UPDATE).

See:

http://csharp.net-informations.com/dataset/csharp-dataset-tutorial.htm[^]

And this:

http://www.dotnetperls.com/dataset[^]
 
Share this answer
 
First check whether connection to database is done or not?
And put your code in try & catch.And explain Your problem?
 
Share this answer
 
dont solved my problem with this way also what am i doing
 
Share this answer
 
public int ExecuteMyQuery(string StrQuery, string ConnStr)
    {

        try
        {
            SqlCommand SqlComm = new SqlCommand();
            SqlConnection SqlConn = new SqlConnection(ConnStr);
            SqlConn.Open();
            SqlComm.CommandText = StrQuery;
            SqlComm.CommandType = CommandType.Text;
            SqlComm.Connection = SqlConn;

            return SqlComm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            throw new Exception(ex.Message);
        }
    }

private void BtnOk_Click(object sender, EventArgs e)
{
string InsertQuery = "INSERT INTO order1 VALUES('10','20','o','u')";
ExecuteMyQuery(InsertQuery,ConnStr);
}
 
Share this answer
 
with this way also not solved my compelete code is:
C#
static public SqlCeConnection Qconnection = new SqlCeConnection();
       static public SqlCeCommand Qcommand = new SqlCeCommand ();
       private void button2_Click(object sender, EventArgs e)
       {

           string str = "Data Source=|DataDirectory|\\Database1.sdf;Persist Security Info=False;";
            Qconnection.ConnectionString = str;
           Qcommand.Connection = Qconnection;
           Qconnection.Open();

             string commandText = "INSERT INTO order1 VALUES(@RID,@amount,@type,@date)";
               Qcommand.CommandText = commandText;
               Qcommand.CommandType = CommandType.Text;

               Qcommand.Parameters.AddWithValue("@RID",1 );
               Qcommand.Parameters.AddWithValue("@amount", 1);
               Qcommand.Parameters.AddWithValue("@type", "12wd");
               Qcommand.Parameters.AddWithValue("@date", "dfds" );
               Qcommand.ExecuteNonQuery();
             //  Qcommand .

           Qconnection.Close();
       }
 
Share this answer
 
Comments
[no name] 7-Sep-12 11:43am    
You should comment this or improve your question.
The solutions are just for answers.
Execute reader is the wrong command: ExecuteNonQuery is correct.
However it is a very good idea to name the fields as you insert them, and (as you tried) to use parametrized queries.
Look at this template - it should give you the idea:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        com.Parameters.AddWithValue("@C1", myValueForColumn1);
        com.Parameters.AddWithValue("@C2", myValueForColumn2);
        com.ExecuteNonQuery();
        }
    }
If you are using SDF files, you probably should be using SqlCEConnections, and SqlCECommands rather than the SqlConnection and SqlCommand in the above though.
 
Share this answer
 
Comments
AmitGajjar 7-Sep-12 4:13am    
Ofcourse Correct Answer 5+

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