Share via


How to insert date/time into access database

Question

Friday, August 3, 2012 6:40 PM

I would like insert user defined dates and times into a access database... After doing some research, this is my first try.

Here is my code:

rotected void Button1_Click(object sender, EventArgs e)
        {
            DateTime startdate = DateTime.Parse(TextBox2.Text);
            DateTime enddate = DateTime.Parse(TextBox3.Text);
            using (OleDbConnection conn = new OleDbConnection())
            {
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\AIRSIGNDMC\AirSign Server\AirSignProgramming\AirSignDatabase.accdb";
                string sql = string.Format("INSERT INTO Events(EventName, StartDate&Time, EndDate&Time, Location, Notes, Clients) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", TextBox1.Text, startdate, enddate, TextBox4.Text, TextBox5.Text, TextBox6.Text);
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();

                    }
                    finally
                    {
                        conn.Close();
                    }
                    conn.Close();
                    {
                        GridView1.DataSource = null;
                        GridView1.DataBind();
                    }

                }
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

    }

thanks for all your help guys, I know I ask for help a lot on here...

All replies (3)

Friday, August 3, 2012 7:06 PM âś…Answered | 1 vote

Youre missing a parameter

"INSERT INTO Events(EventName, StartDate&Time, EndDate&Time, Location, Notes, Clients) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", TextBox1.Text, startdate, enddate, TextBox4.Text, TextBox5.Text, TextBox6.Text)
EventName = 0, StartDate&Time,=1 EndDate&Time=2, Location=3, Notes=4, Clients=5

6 ?

:)

//SFP


Friday, August 3, 2012 6:42 PM

oh, and this is the error I am getting: 

No value given for one or more required parameters.

**Description: **An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

**Exception Details: **System.Data.OleDb.OleDbException: No value given for one or more required parameters.


Friday, August 3, 2012 7:21 PM

myDataGrid.Columns[0].HeaderText = "My Header"

For further information please look at this

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.name.aspx

//SFP