Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DateTime DataTable
I have a database MS 2007 with various data types of columns. I am binding this database to a datatable in C#. i want to write data from a textbox to a particular column in the main database whose type is date time . the difficulty is that every time I have to enter a valid datetime value in the text box column before writing it to the database. if it do not have a valid datetime it create error.
 
what i want is that i should be able to write to the database column from the textbox even when the value of text box is null. i dont want to keep any default value in the datetime column of database when the text box is null.
 
I use the following code
 
 
Projects.dt.Rows[j]["DATERECEIVED"] = txtDTR.Text;
Projects.dt.Rows[j]["DATEDISPATCHED"] = txtDTD.Text;
 
                    Projects.da = new OleDbDataAdapter(Projects.cmdstring, Projects.connection);
                    Projects.cmdb = new OleDbCommandBuilder(Projects.da);                    
                    Projects.da.Fill(Projects.dt);
                    Projects.dt.GetChanges();
 
                    DialogResult result;
                    result = MessageBox.Show("Do you Want To Save ", "", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
 
                        if (Projects.dt.GetChanges() != null)
                        {
 
                            Projects.da.Update(Projects.dt);
                            Projects.connection.Close();
 
                        }
                        Projects.dt.AcceptChanges();
                    }
 
 
thanks in advance
Posted 25-Oct-11 23:06pm
Edited 25-Oct-11 23:10pm
Mehdi Gholam249.4K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use DBNull.Value.
 
Try as below code.
if (string.IsNullOrEmpty(txtDTR.Text))
{
 
  Projects.dt.Rows[j]["DATERECEIVED"] = DBNull.Value;
}
else
{
  Projects.dt.Rows[j]["DATERECEIVED"] = txtDTR.Text;
}
 
if (string.IsNullOrEmpty(txtDTD.Text))
{
  Projects.dt.Rows[j]["DATEDISPATCHED"] = DBNull.Value;
}
else
{
 
  Projects.dt.Rows[j]["DATEDISPATCHED"] = txtDTD.Text;
}
  Permalink  
Comments
MohammadIqbal at 26-Oct-11 4:27am
   
thanks it works
RaisKazi at 26-Oct-11 4:32am
   
Perfect.! Please do not forget to "Accept Answer", so that it can be a reference solution for some one else.
Mehdi Gholam at 26-Oct-11 4:34am
   
my 5!
RaisKazi at 26-Oct-11 4:34am
   
Thank you Mehdi. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 26 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100