Pandiani Posted May 30, 2004 Posted May 30, 2004 I have problem using datetimepicker in visual C# .net. I created databes with tables. One column in my Table need to store date. I created appropriate dataset dsAB1. I placed datetimepicker on my form and want to bind its value to desired column. I have sql adapter and in my fuction Load() I wrote. sqlDataAdap.Fill(dsAB1,"tablename"); I don't want to bind value of datetimepicker in properties with desired colum, because I want to do it in code. In Databindings property of datetimepicker I bind only text property with my desired column datesell. When I click button new to enter infos ablut new sell (which all workin ok) and left datetime picker value as is I get following run time error: Cannot insert NULL in tables column. When I click on datetime picker and choose desired date everything works fine. Problem is that it is not connected until I choose, even when current display of today's date is just what I want. I don't know if I was enough clear what is my problem exactly, bu in short: I have colum that is created as not null. I have datetimepicker which display today's date which I need to store (with all others columns) like new row in table. When I don't touch datetimepicker and try to save get error cannot insert null, but when I click on datetimepicker and choose the same date it bind it somehow, so user of my program must manually choose date that was already displayed. How to connect value of datetimepicker to desired table and colum after user click to enter new record. I rtied to paset this code im button_click.... DTP.DataBindings.Add("Value",dsAB1,"name_table"); but get exception...!?! Please, help! Quote
Joe Mamma Posted May 31, 2004 Posted May 31, 2004 I think one way around it would be to change the table schema so the date column defaults to today's date? Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Pandiani Posted May 31, 2004 Author Posted May 31, 2004 (edited) Do you mean in my sql script when creating table to put Create Table.... ( ..... datesell datetime not null default (getdate()), .... ) ??? Edited May 31, 2004 by Pandiani Quote
Joe Mamma Posted May 31, 2004 Posted May 31, 2004 Do you mean in my sql script when creating table to put Create Table.... ( ..... datesell datetime not null default (getdate()), .... ) ??? datesell datetime not null default getdate() Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
*Experts* Nerseus Posted June 1, 2004 *Experts* Posted June 1, 2004 As a guess, are you defaulting the DateTimePicker control's value or the DataSet's value? If you're "defaulting" your new row with something like: dateTimePicker1.Text = DateTime.Now.ToString(); If that's what you're doing then don't.... When working with bound controls, you should always set default values through the DataSet and let the bound control update itself. You'd want something like this instead: ds.Tables[0].Rows[0]["dateColumn"] = DateTime.Now; -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Pandiani Posted June 1, 2004 Author Posted June 1, 2004 Well, actualyI placed datetime picker on form and with other text boxes for entering new record about selling I want to insert data like new row in my table. I have buttnos for adding new and for save. I use sqladpaters. When I click new and enter all infos, my datetime picker show correct date , when I try to save I get an answer that column value must not be null. So I must click on picker and choose date which is often the same as it's value before modification. So problem is that it "connect" value of picker with column only after I click on it otherwise not! Quote
*Experts* Nerseus Posted June 1, 2004 *Experts* Posted June 1, 2004 If I remember correctly, a DateTimePicker can't show a "NULL" value. Meaning, the picker won't show up "blank". It sounds like your DataSet has a NULL value in the column, its default. The DateTimePicker is showing Today's date as it CAN'T show a NULL. Even though it shows Today's date, the DataSet isn't in sync with the DateTimePicker. If you set the DataSet's value to Today's date as mentioned above (or some other default) the DateTimePicker will be in sync with the DataSet. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Pandiani Posted June 1, 2004 Author Posted June 1, 2004 Of course datetimepicker is showing correct date (todays) and I just want to store that dan with all other values as one new row in my table. Problem is that when I ckick on button save I get error messag datecell column cannot be null. I bind datepicker text with correct column, but when I try to save without touching datetime picker I get above error message, bu when I click on DTP and choose some date or the same date which was earlier shown in DTP everything is OK! Quote
*Experts* Nerseus Posted June 1, 2004 *Experts* Posted June 1, 2004 Did you read my message at all? Your behavior would be expected since your DataSet has a NULL value in it. It doesn't matter that the DateTimePicker shows a date - it's not in the DataSet. Re-read my message about setting the DataSet's value and all should be good. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Pandiani Posted June 1, 2004 Author Posted June 1, 2004 I tired that but I'm havin exception: Heres my code buttons nwe an save: private void btnNew_Click(object sender, System.EventArgs e) { try { check=true; this.BindingContext[dsAB1,"tablename"].EndCurrentEdit(); this.BindingContext[dsAB1,"tablename"].AddNew(); } catch (System.Exception eNovi) { MessageBox.Show("Error: " + eNovi.Message); } } and for button Save: private void btnSave_Click(object sender, System.EventArgs e) { check=false; bool ok=false; try { this.BindingContext[dsAB1,"tablename"].EndCurrentEdit(); ok=this.dsAB1.HasChanges(); dsAB1.Tables[0].Rows[0]["datecolumn"]=DateTime.Now; } catch(System.Exception err) { MessageBox.Show("Eroor check your values "+err.Message); } if (ok) { try { if (sqlCon1.State ==ConnectionState.Closed ) sqlCon1.Open(); sqlDataAdapPratDij.Update(dsAB1,"tablename"); MessageBox.Show("Saved"); } catch (System.Exception eSnimi) { MessageBox.Show("Error: " + eSnimi.Message); } finally { sqlCon1.Close(); } } else { MessageBox.Show("Nothing to save!"); } } I decided to put that connection between column and todays day in Save, but get exception to check values. There is no index 0 or something like that. I'm begginer with this stuff and need help. I nedd to figure how to set index to current position? Quote
Pandiani Posted June 1, 2004 Author Posted June 1, 2004 private void btnSave_Click(object sender, System.EventArgs e) { check=false; bool ok=false; try { this.BindingContext[dsAB1,"tablename"].EndCurrentEdit(); ok=this.dsAB1.HasChanges(); int col=this.dsAB1.Tables["pratilacDijete"].Rows.Count; if(this.dsAB1.Tables["pratilacDijete"].Rows[col-1]["datum"]==null) this.dsAB1.Tables["pratilacDijete"].Rows[col-1]["datum"]=DateTime.Now; } catch(System.Exception err) { MessageBox.Show("Eroor check your values "+err.Message); } if (ok) { try { if (sqlCon1.State ==ConnectionState.Closed ) sqlCon1.Open(); sqlDataAdapPratDij.Update(dsAB1,"tablename"); MessageBox.Show("Saved"); } catch (System.Exception eSnimi) { MessageBox.Show("Error: " + eSnimi.Message); } finally { sqlCon1.Close(); } } else { MessageBox.Show("Nothing to save!"); } } I tried this also, but again good old exception cannot insert null, like that if not exists. I'm really getting desperate Quote
*Experts* Nerseus Posted June 2, 2004 *Experts* Posted June 2, 2004 The code in your "new" button is hard to understand, but I don't see you defaulting the date to today - you only do that in the Save. Normally you add a row to the DataTable directly, not through a control. I would expect something like the following which creates a new row, defaults the datecolumn to today, and then adds the row to the DataSet (NewRow just creates a DataRow - you have to use Rows.Add to get it in the dataset): void btnNew_Click(object sender, System.EventArgs e) { try { DataRow newRow = dsAB1.Tables["tablename"].NewRow(); newRow["datecolumn"] = DateTime.Now; dsAB1.Tables["tablename"].Rows.Add(newRow); } catch (System.Exception exception) { MessageBox.Show("Error: " + exception.Message); } } In the above, datecolumn is the date I assume you have bound to the DateTimePicker? Also, in your save, the following will never hit: if(this.dsAB1.Tables["pratilacDijete"].Rows[col-1]["datum"]==null) this.dsAB1.Tables["pratilacDijete"].Rows[col-1]["datum"]=DateTime.Now; A column won't be "null". You'll want to check for System.DBNull.Value: if(this.dsAB1.Tables["pratilacDijete"].Rows[col-1]["datum"]==System.DBNull.Value) this.dsAB1.Tables["pratilacDijete"].Rows[col-1]["datum"]=DateTime.Now; While you could set this date in the Save, it's generally better to set it in the "new" if you can. Actually, if this date is ALWAYS going to be the current date/time then you'd set it in the stored procedure, if that functionality exists in your database. That way it gets the server's date/time and can't be "fudged" by a user's computer clock. I assume you don't want it to ALWAYS be today, otherwise you wouldn't be offering the user a chance to change it via a DateTimePicker. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Pandiani Posted June 4, 2004 Author Posted June 4, 2004 New problem arise! When I move this code: int col=this.dsAB1.Tables["tablename"].Rows.Count; if(this.dsAB1.Tables["tablename"].Rows[col-1]["date"]==System.DBNull.Value) this.dsAB1.Tables["tablename"].Rows[col-1]["date"]=DateTime.Now; in button function for adding new records: private void btnNovi_Click(object sender, System.EventArgs e) { try { check=true; this.BindingContext[dsAB1,"tablename"].EndCurrentEdit(); this.BindingContext[dsAB1,"tablename"].AddNew(); int col=this.dsAB1.Tables["tablename"].Rows.Count; if(this.dsAB1.Tables["tablename"].Rows[col-1]["date"]==System.DBNull.Value) this.dsAB1.Tables["tablename"].Rows[col-1]["date"]=DateTime.Now; } catch (System.Exception eNovi) { MessageBox.Show("GRESKA: je ovdje " + eNovi.Message); } } I'm getting same error message again that cannot be inserted NULL, what is wrong? Maybe that col is not counted good? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.