Wednesday, March 28, 2012

String was not recognized as a valid DateTime.

I have a Datagrid that I'm trying to use to update a SQL table. The grid wor
ks and I get the EditCommand to work. When I make a change to a field, I ge
t a date error. I was getting invalid cast error, but I changed my Update sc
ript. All the scripts I found seem to work up to that point. What am I missi
ng? BTW-Im using c#.
Here is my Update Script, nor the datetime cast within the parameter assignm
ent:
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControl
s.DataGridCommandEventArgs e)
{
//--retrieve textbox controls from datagrid--//
string iID = e.Item.Cells[1].Text;
string dtDate = e.Item.Cells[3].Text;
string strMeal = e.Item.Cells[4].Text;
string strLodge = e.Item.Cells[5].Text;
//--assign parameters--//
this.sqlCommand1.Parameters.Add("@dotnet.itags.org.TripDate",Convert.ToDateTime(dtDate.ToStri
ng()));
this.sqlCommand1.Parameters["@dotnet.itags.org.Param1"].Value = strMeal.ToString();
this.sqlCommand1.Parameters["@dotnet.itags.org.Param2"].Value = strLodge.ToString();
this.sqlCommand1.Parameters["@dotnet.itags.org.id"].Value = iID;
//--execute update query script--//
// this.sqlConnection1.Open();
// this.sqlCommand1.ExecuteNonQuery();
//--deselect row for edting--//
this.DataGrid1.EditItemIndex = -1;
this.DataGrid1.ShowFooter = true;
BindGrid();
//--debugging code--//
Response.Write(Convert.ToDateTime(dtDate.ToString()));
}Hi hodgesp,
If after you click Edit button, some field become textboxe, in
datagrid_UpdateCommand event, you should get the textbox first. Then retriev
e
data:
TextBox txtDtDate = e.Item.Cells[3].Controls[0] as TextBox;
string dtDate = txtDtDate.Text;
HTH
Elton Wang
"hodgesp" wrote:

> I have a Datagrid that I'm trying to use to update a SQL table. The grid
> works and I get the EditCommand to work. When I make a change to a
> field, I get a date error. I was getting invalid cast error, but I
> changed my Update script. All the scripts I found seem to work up to
> that point. What am I missing? BTW-Im using c#.
> Here is my Update Script, nor the datetime cast within the parameter
> assignment:
>
> private void DataGrid1_UpdateCommand(object source,
> System.Web.UI.WebControls.DataGridCommandEventArgs e)
> {
>
> //--retrieve textbox controls from datagrid--//
> string iID = e.Item.Cells[1].Text;
> string dtDate = e.Item.Cells[3].Text;
> string strMeal = e.Item.Cells[4].Text;
> string strLodge = e.Item.Cells[5].Text;
>
> //--assign parameters--//
> this.sqlCommand1.Parameters.Add("@.TripDate",Convert.ToDateTime(dtDate.ToSt
ring()));
>
> this.sqlCommand1.Parameters["@.Param1"].Value = strMeal.ToString();
> this.sqlCommand1.Parameters["@.Param2"].Value = strLodge.ToString();
> this.sqlCommand1.Parameters["@.id"].Value = iID;
>
> //--execute update query script--//
> // this.sqlConnection1.Open();
> // this.sqlCommand1.ExecuteNonQuery();
>
> //--deselect row for edting--//
> this.DataGrid1.EditItemIndex = -1;
> this.DataGrid1.ShowFooter = true;
> BindGrid();
>
> //--debugging code--//
> Response.Write(Convert.ToDateTime(dtDate.ToString()));
> }
>
> --
> hodgesp
> ---
> Posted via http://www.codecomments.com
> ---
>

0 comments:

Post a Comment