0

I am trying to create WinForm accounting application using c# and Microsoft Visual Studio. I want to create a new column in the table. The column name should be fetch from the datepicker.

form1.cs

 private void button5_Click(object sender, EventArgs e)
    {

        CON.Open();
        SqlDataAdapter SDA = new SqlDataAdapter("alter table SAMPLETABLE add "+dateTimePicker1.Text + " VARCHAR[50]", CON);
        SDA.SelectCommand.ExecuteNonQuery();
        CON.Close();

        MessageBox.Show("CREATED SUCCESSFULLY");
    }

When run the program, which showing an error

"The definition for column Thursday' must include a data type."

Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
Ajith
  • 775
  • 1
  • 13
  • 47

1 Answers1

0

You need to add a column to your DataTable first.

DataColumn myColumn = new DataColumn("ColumnName",System.DateTime);

Then fill... How to Bind specific Columns of a datatable to a datagridView?

Then insert the column in the beginning of your table:

myTable.Columns.Add(myColumn);
myColumn.SetOrdinal(0);// to put the column in position 0;
Community
  • 1
  • 1
Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21