none
need help using ado.net on csharp application.. RRS feed

  • Question

  • hi all..

    i am new to programming but now i started work on ado.net by myself.

    now i have been trying to set simple application in i want to set insert, update, delete function. but i cant able to start for first step..

    i am using ms access for database

    i am trying to insert button event but still not working . i hope some one will help. following is my code and error..

    error :Command text was not set for the command object.

     

    code :

    namespace PersoanlExpMgt
    {
        public partial class Form1 : Form
        {
            private OleDbConnection mycon;

            public Form1()
            {
                         
                      
                InitializeComponent();
            }


         
           
         

         
            private void Form1_Load(object sender, EventArgs e)
            {
               
              
                this.accountsTableAdapter.Fill(this.personal_expDataSet1.Accounts);
             

            }
           
          

            private void button1_Click_2(object sender, EventArgs e)
            {

                string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Personal_exp.accdb";
                OleDbConnection mycon = new OleDbConnection(connString);
                mycon.Open();
                string query = "Insert into [Accounts](Account_name) values ('test')";

                OleDbCommand cmd = new OleDbCommand();


                cmd.Connection = mycon;
                 cmd.CommandType = System.Data.CommandType.Text;
                cmd.ExecuteNonQuery();
                mycon.Close();
            }

          

          
        }
    }

     

    • Moved by Rowan Miller Thursday, March 24, 2011 9:05 PM (From:ADO.NET Entity Framework and LINQ to Entities (Pre-Release))
    Tuesday, March 22, 2011 3:07 AM

Answers

  • Hello monty,

     

    Welcome to the ADO.NET Managed Providers Forum!

    After looking  at your code, I didn't find the command text, so did you miss this?

    As I know, the code should be like this:

    [C#]
    public void CreateMyOleDbCommand()
    {
       OleDbCommand myCommand = new OleDbCommand();
       myCommand.CommandText = "SELECT * FROM Categories ORDER BY CategoryID";
       myCommand.CommandTimeout = 20;
    }

    If you have any time please check your code, expecially the command text.

    I hope this can help you.

     

    Have a nice weekend!


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 25, 2011 5:52 AM
    Moderator

All replies

  • Hello monty,

     

    Welcome to the ADO.NET Managed Providers Forum!

    After looking  at your code, I didn't find the command text, so did you miss this?

    As I know, the code should be like this:

    [C#]
    public void CreateMyOleDbCommand()
    {
       OleDbCommand myCommand = new OleDbCommand();
       myCommand.CommandText = "SELECT * FROM Categories ORDER BY CategoryID";
       myCommand.CommandTimeout = 20;
    }

    If you have any time please check your code, expecially the command text.

    I hope this can help you.

     

    Have a nice weekend!


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 25, 2011 5:52 AM
    Moderator
  •             string query = "Insert into [Accounts](Account_name) values ('test')";
                OleDbCommand cmd = new OleDbCommand();

                cmd.Connection = mycon;
                 cmd.CommandType = System.Data.CommandType.Text;
                cmd.ExecuteNonQuery();
                mycon.Close();

    you have a string with a query, but you forgot to assign it to your command;

    cmd.commandtext = query;

    Tuesday, March 29, 2011 2:54 PM