各位请帮帮我,没有分了.我找了好久也没找出来原因是什么,我的字段类型是正确的,只有filedate 和loaddate是datatime型,可是我无论更新的是哪一个单元格,都提示相同的错误.
页面部分:
<td colspan="3" style="height: 200px; width: 741px;">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="ID" ForeColor="#333333"
Width="100%" AllowPaging="True" Height="100%" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing1" OnRowUpdating="GridView1_RowUpdating1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="资料ID" InsertVisible="False"
SortExpression="ID" />
<asp:BoundField DataField="name" HeaderText="资料名称" SortExpression="name" />
<asp:BoundField DataField="type" HeaderText="所属分类" SortExpression="type" />
<asp:BoundField DataField="filedate" HeaderText="资料产生时间" SortExpression="filedate" />
<asp:BoundField DataField="loaddate" HeaderText="资料上传时间" SortExpression="loaddate" />
<asp:BoundField DataField="des" HeaderText="资料描述" SortExpression="des" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>相关代码:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();//第一次加载页面的时候绑定数据;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("delwj.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("loadwj.aspx");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void bind()
{
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["MySqlConnection"]);
SqlConnection myconn = new SqlConnection(settings);
string str = "select * from Info";
SqlDataAdapter myda = new SqlDataAdapter(str, myconn);
DataSet myds = new DataSet();
myconn.Open();
myda.Fill(myds, "Info");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
myconn.Close(); }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (e.RowIndex != null && e.RowIndex >= 0)
{
string str = "delete from Info where ID='" + GridView1.DataKeys[GridView1.Rows[e.RowIndex].RowIndex].Value.ToString() + "'"; string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["MySqlConnection"]);
SqlConnection mycon = new SqlConnection(settings);
SqlCommand sqlcom = new SqlCommand(str, mycon);
mycon.Open();
sqlcom.ExecuteNonQuery();
mycon.Close();
bind(); }
}
protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["MySqlConnection"]);
SqlConnection mycon = new SqlConnection(settings);
// string str = "update Tuser set UserId='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Admin='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Password='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "'where UserId='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; // string ID=((TextBox)e.Row.FindControl("你的TextBox控件名称")).Text;
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString(); string ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString();
string type = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
string filedate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
string loaddate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
string des = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString(); //string strSql = "update Tuser set UserId=@UserId,name=@name,Admin=@Admin,Password=@Password where UserId=@UserId";
string strSql = "update Tuser set name=@name,type=@type,filedate=@filedate,loaddate=@loaddate,des=@des where ID=@ID";
SqlCommand cmd = new SqlCommand(strSql, mycon);
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
cmd.Parameters.Add("@name", SqlDbType.VarChar, 100).Value = name;
cmd.Parameters.Add("@type", SqlDbType.Int).Value = type;
cmd.Parameters.Add("@filedate", SqlDbType.DateTime).Value = filedate;
cmd.Parameters.Add("@loaddate", SqlDbType.DateTime).Value = loaddate;
cmd.Parameters.Add("@des", SqlDbType.NText).Value = name; // SqlCommand sqlcom = new SqlCommand(str, mycon);
mycon.Open();
cmd.ExecuteNonQuery();
mycon.Close();
GridView1.EditIndex = -1;
bind();
//this.Label2.Text = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text .ToString();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
} 出错信息写在下一栏
页面部分:
<td colspan="3" style="height: 200px; width: 741px;">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="ID" ForeColor="#333333"
Width="100%" AllowPaging="True" Height="100%" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing1" OnRowUpdating="GridView1_RowUpdating1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="资料ID" InsertVisible="False"
SortExpression="ID" />
<asp:BoundField DataField="name" HeaderText="资料名称" SortExpression="name" />
<asp:BoundField DataField="type" HeaderText="所属分类" SortExpression="type" />
<asp:BoundField DataField="filedate" HeaderText="资料产生时间" SortExpression="filedate" />
<asp:BoundField DataField="loaddate" HeaderText="资料上传时间" SortExpression="loaddate" />
<asp:BoundField DataField="des" HeaderText="资料描述" SortExpression="des" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>相关代码:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();//第一次加载页面的时候绑定数据;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("delwj.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("loadwj.aspx");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void bind()
{
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["MySqlConnection"]);
SqlConnection myconn = new SqlConnection(settings);
string str = "select * from Info";
SqlDataAdapter myda = new SqlDataAdapter(str, myconn);
DataSet myds = new DataSet();
myconn.Open();
myda.Fill(myds, "Info");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
myconn.Close(); }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (e.RowIndex != null && e.RowIndex >= 0)
{
string str = "delete from Info where ID='" + GridView1.DataKeys[GridView1.Rows[e.RowIndex].RowIndex].Value.ToString() + "'"; string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["MySqlConnection"]);
SqlConnection mycon = new SqlConnection(settings);
SqlCommand sqlcom = new SqlCommand(str, mycon);
mycon.Open();
sqlcom.ExecuteNonQuery();
mycon.Close();
bind(); }
}
protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["MySqlConnection"]);
SqlConnection mycon = new SqlConnection(settings);
// string str = "update Tuser set UserId='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Admin='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Password='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "'where UserId='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; // string ID=((TextBox)e.Row.FindControl("你的TextBox控件名称")).Text;
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString(); string ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString();
string type = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
string filedate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
string loaddate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
string des = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString(); //string strSql = "update Tuser set UserId=@UserId,name=@name,Admin=@Admin,Password=@Password where UserId=@UserId";
string strSql = "update Tuser set name=@name,type=@type,filedate=@filedate,loaddate=@loaddate,des=@des where ID=@ID";
SqlCommand cmd = new SqlCommand(strSql, mycon);
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
cmd.Parameters.Add("@name", SqlDbType.VarChar, 100).Value = name;
cmd.Parameters.Add("@type", SqlDbType.Int).Value = type;
cmd.Parameters.Add("@filedate", SqlDbType.DateTime).Value = filedate;
cmd.Parameters.Add("@loaddate", SqlDbType.DateTime).Value = loaddate;
cmd.Parameters.Add("@des", SqlDbType.NText).Value = name; // SqlCommand sqlcom = new SqlCommand(str, mycon);
mycon.Open();
cmd.ExecuteNonQuery();
mycon.Close();
GridView1.EditIndex = -1;
bind();
//this.Label2.Text = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text .ToString();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
} 出错信息写在下一栏
解决方案 »
- 谁有用VB.NET调用AspNetPager分页控件的实例?
- 请教有效性验证
- js 引号嵌套
- ===========datalist传递嵌套的TextBox值,百思不得其解=====================
- imagebutton如何提交表单??????
- 怎么我在.NET中DllImport之后,那些值调用不回来啊!()
- 打开网页问题???
- 这个需求ASP.NET页继承可以实现吗?
- 精通MVC的大虾帮我看下, 共用视图的问题
- 要把这个串2003-01-16 15:51:28格式化成 2003-01-16 03:51:28 PM该怎么处理?
- 关于返回值问题
- 身份验证session cookies
“/documentsys”应用程序中的服务器错误。
--------------------------------------------------------------------------------该字符串未被识别为有效的 DateTime。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 该字符串未被识别为有效的 DateTime。源错误:
行 110: // SqlCommand sqlcom = new SqlCommand(str, mycon);
行 111: mycon.Open();
行 112: cmd.ExecuteNonQuery();
行 113: mycon.Close();
行 114: GridView1.EditIndex = -1;
源文件: f:\设计\documentsys\displaydoc.aspx.cs 行: 112 堆栈跟踪:
[FormatException: 该字符串未被识别为有效的 DateTime。]
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +2271778
System.DateTime.Parse(String s, IFormatProvider provider) +26
System.Convert.ToDateTime(String value, IFormatProvider provider) +49
System.String.System.IConvertible.ToDateTime(IFormatProvider provider) +10
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +2562369
System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +738[FormatException: 将参数值从 String 转换到 DateTime 失败。]
System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +784
System.Data.SqlClient.SqlParameter.GetCoercedValue() +30
System.Data.SqlClient.SqlParameter.Validate(Int32 index) +136
System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters) +133
System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc) +256
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1020
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +314
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +413
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +115
_Default.GridView1_RowUpdating1(Object sender, GridViewUpdateEventArgs e) in f:\设计\documentsys\displaydoc.aspx.cs:112
System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +99
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +781
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +853
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +87
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +117
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +86
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +153
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +172
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921
cmd.Parameters.Add("@loaddate", SqlDbType.DateTime).Value = loaddate; 这两个地方打个断点看看
filedate,loaddate取到的值是不是日期格式的,不然会出先你的这个错误
loaddate;
這兩個值有問題.
必須確保這兩個是可以有校轉化成datetime格式的字符 。
比如:2009年04月12日,上午9點。(錯誤)
2009-04-12 9:20 (正確)
loaddate;
這兩個值為空,也會出錯