1
Answer

How to Create a Datatable In C#

Step 1 :

Declare A Datatable

Ex
 DataTable Dt = new DataTable();

Step 2:

Create a Datatable Columns

Ex
            DataColumn RollNo= new DataColumn();
            DataColumn Name= new DataColumn();
            DataColumn Std= new DataColumn();
            DataColumn BDate= new DataColumn();

            RollNo.ColumnName = "RollNo";
            Name.ColumnName = "Name";
            Std.ColumnName = "Std";
            BDate.ColumnName = "BDate";

            Dt .Columns.Add(RollNo);
            Dt .Columns.Add(Name);
            Dt .Columns.Add(Std);
            Dt .Columns.Add(BDate);

Step 3:

Create A row And Fill It

Ex

  DataRow row;
  row = Dt.NewRow();
  row["RollNo"] ="1";
  row["Name"] = "Vicky";
  row["Std"] = "12Std";
  row["BDate"] ="17-11-1990";
  Dt.Rows.Add(row);


-----------------------------------------------------------------
Brijesh Kumar Prajapati
Jr.Software Devloper,
Softyoug Solution Pvt Ltd
Vapi,
Gujarat,
India
Answers (1)
0
Vulpes
NA 98.3k 1.5m 12y
You'll need to convert to strings to avoid this problem:

(DateTime.TryParse(maskedTextBox1.Text, out dtparsedDate) ? dtparsedDate.ToString() : "null") 

Notice that it's never necessary to explicitly test whether a bool expression is true.

Notice also that it's not necessary to convert dtParsedDate to a DateTime, as it's a DateTime already. 
Accepted
0
darma teja
NA 493 194.2k 12y
Hi Vulpes, Santhosh,

thanks allot for your fast replies.

If i don't convert null to datetime, it is giving me error.

because:
you can check this one: if i write like this it is giving me error of "can not implicit conversion between system.datetime and null"

(DateTime.TryParse(maskedTextBox1.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : null) 
 

Darma

0
Vulpes
NA 98.3k 1.5m 12y
darma,

If you use Convert.ToDateTime(null) when building your SQL command, then you'll end with a string containing this:

"01/01/0001 00:00:00"

which may be an invalid value for the corresponding column in the database.

Santhosh's point is that you should use "null" instead which should then be translated to NULL in the database assuming (Hemant's point) that the column allows nulls.
0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
Then check whether the columns tyou are tring to insert allow null values. also check whether any integer columns.IIf there are integer columns, then check and do conversion before insert
0
darma teja
NA 493 194.2k 12y
Hi Santhosh,

If user does not enters the date,  it is working well. 

Only my problem with textboxes only.

Darma
0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
Why you are converting null to datetime..Just save it as null. No conversion needed
0
darma teja
NA 493 194.2k 12y
Hi,

Here is my code:

DateTime dtparsedDate = new DateTime();
                string insertsql = "insert into Table1 (ProjectId, ID, PID, Nr, note, Out, Target, Delivery, name,  Out1, Target1, Delivery1, Notes) " +
                                          "values ('" + CmbProjectid.Text + "', '" + ID.SelectedItem + "', '" + textBox1.Text + "', '" + textBox2.Text +
                                          "', '" + textBox3.Text + "', '" + (DateTime.TryParse(maskedTextBox1.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(DBNull.Value)) + "', '" + (DateTime.TryParse(maskedTextBox2.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) + "', '" + (DateTime.TryParse(maskedTextBox3.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) +
                                          "', '" + textBox4.Text + "', '" + (DateTime.TryParse(maskedTextBox4.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) + "', '" + (DateTime.TryParse(maskedTextBox5.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) + "', '" + (DateTime.TryParse(maskedTextBox6.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) + "', '" + textBox4.Text + "');";
                SqlCommand insertcmd = new SqlCommand(insertsql, con1);
                insertcmd.ExecuteNonQuery();

                MessageBox.Show("Data Added Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);


Is user does not enter data in one textbox, than it is giving error.

Whatever user does not enter the data, that should save in the table1 as nulls.


Darma

0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
Show us ur c# code
0
Hemant Kumar
NA 3k 215.1k 12y
You have to alter the table column property as Allow Null =True.Then you can insert the null values also.