1
Answer

DateTime Picker Problem

djohncon

djohncon

19y
2.3k
1
Hi All, has anyone had any problems with DatetimePicker for windows and C#. I have a program with MS Access DB and two fields ID and Date. Id is a number non auto increment and primary key. Date Short date. The ID field increments by 1 to create a new record in my program. If I use a text box for date everything works fine. If I use a DateTimePicker it cause the ID to keep incrementing by 1 as if a continuing loop without even using the save button to update DB. Sample Code. protected void saveClick(object sender, System.EventArgs e) { //code to save the inputed data in to the database //no code to validate the data implemented try { if(t_ID.Text!=""&&DTP1.Text!="") { string strConn="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=Dates.mdb" ; OleDbConnection myConn = new OleDbConnection(strConn) ; myConn.Open(); //the string to get values from the textboxes and form an "INSERT INTO" // statement. string strInsert = "INSERT INTO fDate (ID, firstDate) VALUES ( "; //NOTE for integers do not have apostrophe (') in the string text strInsert += t_ID.Text+", "; System.DateTime ddt = DateTime.Parse(DTP1.Text,System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat); string sNow = ""; sNow = ddt.ToString("dd/MM/yyyy"); DTP1.Text = '#'+sNow+'#'; strInsert += "CDate("+DTP1.Text+')'+")"; OleDbCommand inst = new OleDbCommand(strInsert,myConn) ; //Execute the statement inst.ExecuteNonQuery() ; statusBar.Text="Data Added to Database " ; //reset all the textboxes int i=int.Parse(t_ID.Text); i++; t_ID.Text=i.ToString(); DTP1.Text = ""; statusBar.Text="Connected - Now you can Add records"; myConn.Close() ; } else { MessageBox.Show("All fields must be completed.", "Error"); } } catch(Exception ed) { MessageBox.Show("Error in Saving "+ed.ToString(), "Error"); } } Any clues kamean
Answers (1)