String was not recognized as a valid DateTime.

H

hodgesp

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.ToString()));


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()));

}
 
G

Guest

Hi hodgesp,

If after you click Edit button, some field become textboxe, in
datagrid_UpdateCommand event, you should get the textbox first. Then retrieve
data:

TextBox txtDtDate = e.Item.Cells[3].Controls[0] as TextBox;
string dtDate = txtDtDate.Text;

HTH

Elton Wang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top