Alex
Alex

Reputation: 9720

Cannot interpret token '.' at position 6

I have a textbox and a calendar extender, here's the code in page Default.aspx:

 <asp:TextBox ID="tbdate" runat="server"></asp:TextBox>

 <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="tbdate" 
      Format="dd.MM.yyyy">
 </asp:CalendarExtender>

Now in Default.aspx.cs page I try to insert the value from the TextBox in new columns from DataTable

 dt.Columns.Add("date", typeof(string), tbdate.Text);

and I have this error

Cannot interpret token '.' at position 6.

Upvotes: 1

Views: 7102

Answers (2)

Zaki
Zaki

Reputation: 5600

You need to add your column first :

dt.Columns.Add(new DataColumn("date", typeof(DateTime));

then do a loop and add the required date:

foreach(DataRow row in dt.Rows)
{
 row["date"] = DateTime.Parse(tbdate.Text);
}

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460058

I assume that you don't want to add this date-value as DataColumn but as DataRow:

DataTable dt = new DataTable();
dt.Columns.Add("date", typeof(DateTime));
dt.Rows.Add(DateTime.Parse(tbdate.Text));

By the way, the overload of DataColumnCollection.Add that you've used is for an Expression.

If you want to add this value to all rows:

var selectedDate = DateTime.Parse(tbdate.Text));
dt.Columns.Add("date", typeof(DateTime));
foreach(DataRow row in dt.Rows)
{
    row["date"] = selectedDate;
}

Upvotes: 0

Related Questions