Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Insert Command and access database

Insert Command and access database

Scheduled Pinned Locked Moved C#
databasehelp
16 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sourie
    wrote on last edited by
    #1

    Hi There, I want to insert a row into a table which is displayed in datagridview. The table is named "WireTbl" in "WireDb" database, and has 5 fields: Code, Street, Alley, Date and Time. Would you please help me to write a correct INSERT command for this code: Int32 x1 = System.Convert.ToInt32(textBox1.Text);//Code String x2 = textBox2.text;//Street String x3 = textBox3.Text;//Alley DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date DateTime x5 = System.Convert.ToDateTime(textBox5.Text);//Time cmd1.CommandText = "Insert into WireTbl("+ "Code, "+ "Street, "+ "alley, "+ "Date, "+ "Time, "+ ") VALUES('" + x1 + "', "', '"+ x2+ "','"+ x3+ "','"+ x4+ "', x5)"; Tnx

    Sourie

    M 1 Reply Last reply
    0
    • S Sourie

      Hi There, I want to insert a row into a table which is displayed in datagridview. The table is named "WireTbl" in "WireDb" database, and has 5 fields: Code, Street, Alley, Date and Time. Would you please help me to write a correct INSERT command for this code: Int32 x1 = System.Convert.ToInt32(textBox1.Text);//Code String x2 = textBox2.text;//Street String x3 = textBox3.Text;//Alley DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date DateTime x5 = System.Convert.ToDateTime(textBox5.Text);//Time cmd1.CommandText = "Insert into WireTbl("+ "Code, "+ "Street, "+ "alley, "+ "Date, "+ "Time, "+ ") VALUES('" + x1 + "', "', '"+ x2+ "','"+ x3+ "','"+ x4+ "', x5)"; Tnx

      Sourie

      M Offline
      M Offline
      Mbah Dhaim
      wrote on last edited by
      #2

      use appropriate parameters to your command and change your command text according to parameter identification i. e

      Sourie wrote:

      cmd1.CommandText = "Insert into WireTbl("+ "Code, "+ "Street, "+ "alley, "+ "Date, "+ "Time, "+ ") VALUES('" + x1 + "', "', '"+ x2+ "','"+ x3+ "','"+ x4+ "', x5)";

      change to

      cmd1.CommandText = "INSERT INTO WIRETBL(Code, Street, Alley, Date, Time) VALUES(@Code, @Street, @Alley, @Date, @Time)";
      cmd1.Parameters.Add("@Code", SqlDbType.Int).Value = x1;
      cmd1.Parameters.Add("@Street", SqlDbType.Varchar).Value = x2;
      cmd1.Parameters.Add("@Alley", SqlDbType.Varchar).Value = x3;
      cmd1.Parameters.Add("@Date", SqlDbType.DateTime).Value = x4;
      cmd1.Parameters.Add("@Time", SqlDbType.DateTime).Value = x5;
      cmd1.ExecuteNonQuery();

      hope it helps

      dhaim programming is a hobby that make some money as side effect :)

      S 1 Reply Last reply
      0
      • M Mbah Dhaim

        use appropriate parameters to your command and change your command text according to parameter identification i. e

        Sourie wrote:

        cmd1.CommandText = "Insert into WireTbl("+ "Code, "+ "Street, "+ "alley, "+ "Date, "+ "Time, "+ ") VALUES('" + x1 + "', "', '"+ x2+ "','"+ x3+ "','"+ x4+ "', x5)";

        change to

        cmd1.CommandText = "INSERT INTO WIRETBL(Code, Street, Alley, Date, Time) VALUES(@Code, @Street, @Alley, @Date, @Time)";
        cmd1.Parameters.Add("@Code", SqlDbType.Int).Value = x1;
        cmd1.Parameters.Add("@Street", SqlDbType.Varchar).Value = x2;
        cmd1.Parameters.Add("@Alley", SqlDbType.Varchar).Value = x3;
        cmd1.Parameters.Add("@Date", SqlDbType.DateTime).Value = x4;
        cmd1.Parameters.Add("@Time", SqlDbType.DateTime).Value = x5;
        cmd1.ExecuteNonQuery();

        hope it helps

        dhaim programming is a hobby that make some money as side effect :)

        S Offline
        S Offline
        Sourie
        wrote on last edited by
        #3

        Thanks fort heloping me. I am using access database. Do I have to change Sql to Ole in the code you wrote me? For Example: cmd1.CommandText = "INSERT INTO WIRETBL(Code, Street, Alley, Date, Time) VALUES(@Code, @Street, @Alley, @Date, @Time)";cmd1.Parameters.Add("@Code", OleDbType.Int).Value = x1;cmd1.Parameters.Add("@Street", OleDbType.Varchar).Value = x2;cmd1.Parameters.Add("@Alley", OleDbType.Varchar).Value = x3;cmd1.Parameters.Add("@Date", OleDbType.DateTime).Value = x4;cmd1.Parameters.Add("@Time", OleDbType.DateTime).Value = x5;cmd1.ExecuteNonQuery();

        Sourie

        B 1 Reply Last reply
        0
        • S Sourie

          Thanks fort heloping me. I am using access database. Do I have to change Sql to Ole in the code you wrote me? For Example: cmd1.CommandText = "INSERT INTO WIRETBL(Code, Street, Alley, Date, Time) VALUES(@Code, @Street, @Alley, @Date, @Time)";cmd1.Parameters.Add("@Code", OleDbType.Int).Value = x1;cmd1.Parameters.Add("@Street", OleDbType.Varchar).Value = x2;cmd1.Parameters.Add("@Alley", OleDbType.Varchar).Value = x3;cmd1.Parameters.Add("@Date", OleDbType.DateTime).Value = x4;cmd1.Parameters.Add("@Time", OleDbType.DateTime).Value = x5;cmd1.ExecuteNonQuery();

          Sourie

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          Yes you have to.


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          S 1 Reply Last reply
          0
          • B Blue_Boy

            Yes you have to.


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

            S Offline
            S Offline
            Sourie
            wrote on last edited by
            #5

            Unfortunately I got Syntax Error

            Sourie

            B 1 Reply Last reply
            0
            • S Sourie

              Unfortunately I got Syntax Error

              Sourie

              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              What error message you get?


              I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

              S 1 Reply Last reply
              0
              • B Blue_Boy

                What error message you get?


                I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                S Offline
                S Offline
                Sourie
                wrote on last edited by
                #7

                "String was not recognized as a valid DateTime." for this line: DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date Then I changed DateTime to String:For example for this line: String x5 = System.Convert.ToString(textBox5.Text);// cmd1.Parameters.AddWithValue("@Time", OleDbType.VarChar).Value = x5; but this time I got this error: Syntax error in INSERT INTO statement.

                Sourie

                B S 2 Replies Last reply
                0
                • S Sourie

                  "String was not recognized as a valid DateTime." for this line: DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date Then I changed DateTime to String:For example for this line: String x5 = System.Convert.ToString(textBox5.Text);// cmd1.Parameters.AddWithValue("@Time", OleDbType.VarChar).Value = x5; but this time I got this error: Syntax error in INSERT INTO statement.

                  Sourie

                  B Offline
                  B Offline
                  Blue_Boy
                  wrote on last edited by
                  #8

                  DateTime.ParseExact(textBox4.Text,"MM/dd/yyyy",System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat)

                  Sourie wrote:

                  Syntax error in INSERT INTO statement.

                  Show here how do you make insert command.


                  I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                  1 Reply Last reply
                  0
                  • S Sourie

                    "String was not recognized as a valid DateTime." for this line: DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date Then I changed DateTime to String:For example for this line: String x5 = System.Convert.ToString(textBox5.Text);// cmd1.Parameters.AddWithValue("@Time", OleDbType.VarChar).Value = x5; but this time I got this error: Syntax error in INSERT INTO statement.

                    Sourie

                    S Offline
                    S Offline
                    Sourie
                    wrote on last edited by
                    #9

                    con1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\WireDb.mdb"; con1.Open(); Int32 x1 = System.Convert.ToInt32(textBox1.Text);//Code String x2 = textBox2.Text;//Street String x3 = textBox3.Text;//Alley DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date DateTime x5 = System.Convert.ToDateTime(textBox5.Text);//Time cmd1.CommandText = "INSERT INTO WIRETBL(Code, Street, Alley, Date, Time) VALUES(@Code, @Street, @Alley, @Date, @Time)"; cmd1.Parameters.AddWithValue("@Code", OleDbType.Integer).Value = x1; cmd1.Parameters.AddWithValue("@Street", OleDbType.VarChar).Value = x2; cmd1.Parameters.AddWithValue("@Alley", OleDbType.VarChar).Value = x3; cmd1.Parameters.AddWithValue("@Date", OleDbType.DBDate).Value = x4; cmd1.Parameters.AddWithValue("@Time", OleDbType.DBTime).Value = x5; //cmd1.ExecuteNonQuery(); cmd1.CommandType = CommandType.Text; cmd1.Connection = con1; cmd1.ExecuteNonQuery(); adp1.SelectCommand = cmd1; DataTable tb1 = new DataTable(); adp1.Fill(tb1); dataGridView1.DataSource = tb1; con1.Close(); Dear Friend how can I learn working with Access Database in c#? I need a complete reference to not bother you alot.

                    Sourie

                    M 1 Reply Last reply
                    0
                    • S Sourie

                      con1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\WireDb.mdb"; con1.Open(); Int32 x1 = System.Convert.ToInt32(textBox1.Text);//Code String x2 = textBox2.Text;//Street String x3 = textBox3.Text;//Alley DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date DateTime x5 = System.Convert.ToDateTime(textBox5.Text);//Time cmd1.CommandText = "INSERT INTO WIRETBL(Code, Street, Alley, Date, Time) VALUES(@Code, @Street, @Alley, @Date, @Time)"; cmd1.Parameters.AddWithValue("@Code", OleDbType.Integer).Value = x1; cmd1.Parameters.AddWithValue("@Street", OleDbType.VarChar).Value = x2; cmd1.Parameters.AddWithValue("@Alley", OleDbType.VarChar).Value = x3; cmd1.Parameters.AddWithValue("@Date", OleDbType.DBDate).Value = x4; cmd1.Parameters.AddWithValue("@Time", OleDbType.DBTime).Value = x5; //cmd1.ExecuteNonQuery(); cmd1.CommandType = CommandType.Text; cmd1.Connection = con1; cmd1.ExecuteNonQuery(); adp1.SelectCommand = cmd1; DataTable tb1 = new DataTable(); adp1.Fill(tb1); dataGridView1.DataSource = tb1; con1.Close(); Dear Friend how can I learn working with Access Database in c#? I need a complete reference to not bother you alot.

                      Sourie

                      M Offline
                      M Offline
                      Mbah Dhaim
                      wrote on last edited by
                      #10

                      you can read this link[^] hope it helps

                      dhaim programming is a hobby that make some money as side effect :)

                      S 1 Reply Last reply
                      0
                      • M Mbah Dhaim

                        you can read this link[^] hope it helps

                        dhaim programming is a hobby that make some money as side effect :)

                        S Offline
                        S Offline
                        Sourie
                        wrote on last edited by
                        #11

                        Oh thanks alot it is a good start for me.I have to read it and test ur program myself. But another question, How I can use a datagridview to show a table's content without puting datagridview object on the form? I want to define datagridview in my code.

                        Sourie

                        M 1 Reply Last reply
                        0
                        • S Sourie

                          Oh thanks alot it is a good start for me.I have to read it and test ur program myself. But another question, How I can use a datagridview to show a table's content without puting datagridview object on the form? I want to define datagridview in my code.

                          Sourie

                          M Offline
                          M Offline
                          Mbah Dhaim
                          wrote on last edited by
                          #12

                          1. create DataGridView instance 2. Set it datasource to your DataTable or DataSet 3. if you need to show the grid just add it to container control like form or panel with method "containerControl to add".Controls.Add("your datagridview") ie.

                          form1.Controls.Add(myGrid);

                          dhaim programming is a hobby that make some money as side effect :)

                          S 1 Reply Last reply
                          0
                          • M Mbah Dhaim

                            1. create DataGridView instance 2. Set it datasource to your DataTable or DataSet 3. if you need to show the grid just add it to container control like form or panel with method "containerControl to add".Controls.Add("your datagridview") ie.

                            form1.Controls.Add(myGrid);

                            dhaim programming is a hobby that make some money as side effect :)

                            S Offline
                            S Offline
                            Sourie
                            wrote on last edited by
                            #13

                            Wow, Finally I wrote a program :). Now I know what's the exact meaning of adapter, connection, dataset, datatable, and command. I will try your suggestion to use datagridview also.

                            Sourie

                            S 1 Reply Last reply
                            0
                            • S Sourie

                              Wow, Finally I wrote a program :). Now I know what's the exact meaning of adapter, connection, dataset, datatable, and command. I will try your suggestion to use datagridview also.

                              Sourie

                              S Offline
                              S Offline
                              Sourie
                              wrote on last edited by
                              #14

                              Bravo! It worked also. thanks alotttttttttttttt. I won't forget your kind. may I get your help regard working with ports (Serial and USB) in c#?

                              Sourie

                              M 1 Reply Last reply
                              0
                              • S Sourie

                                Bravo! It worked also. thanks alotttttttttttttt. I won't forget your kind. may I get your help regard working with ports (Serial and USB) in c#?

                                Sourie

                                M Offline
                                M Offline
                                Mbah Dhaim
                                wrote on last edited by
                                #15

                                just follow this link[^] there are many results here that you maybe want to know. hope it helps

                                S 1 Reply Last reply
                                0
                                • M Mbah Dhaim

                                  just follow this link[^] there are many results here that you maybe want to know. hope it helps

                                  S Offline
                                  S Offline
                                  Sourie
                                  wrote on last edited by
                                  #16

                                  Hello again, according the lessons you gave me, I wrote a program that shows content of a table in a datagridview. Now I want to sort the content of the table based a field(ASC or DEC) and show the result in that datagridview. Database name= WirdDb, tablename= tbl2, fields: No, Code, Street, Alley, Status, Date and Time. I want to sort the content of tbl2 according field "Street" ascendingly. Would you please help me. OleDbDataAdapter adapter2 = new OleDbDataAdapter(); OleDbCommand command2 = new OleDbCommand("SELECT * from tbl2"+tbl2.Street, conn); adapter2.SelectCommand = command2; OleDbCommandBuilder cb2 = new OleDbCommandBuilder(adapter2); DataSet ds2 = new DataSet(); adapter2.Fill(ds2, "tbl2"); DataTable dt2 = ds2.Tables[0]; DataGridView dgv2 = new DataGridView(); dgv2.DataSource = dt2; this.Controls.Add(dgv2);

                                  Sourie

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups