“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误:
行 136: {
行 137: // 呼叫 ExecuteNonQuery() 方法以便针对数据来源执行 UPDATE 命令
行 138: MyCommand.ExecuteNonQuery();
行 139: Message.Text = "<b>已更新数据纪录</b><br>";
行 140:
源文件: e:\aspnet\manage\manageclass1.aspx.cs 行: 138 堆栈跟踪:
[FormatException: 输入字符串的格式不正确。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +742
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +194
localhost.manage.manageclass1.DataGridClass1_UpdateCommand(Object source, DataGridCommandEventArgs e) in e:\aspnet\manage\manageclass1.aspx.cs:138
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292源码如下:
private void DataGridClass1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
// 建立 UPDATE 命令字符串
string UpdateCmd = "UPDATE jq_class1 SET class1_name = @SName WHERE id = @uid";
SqlCommand MyCommand = new SqlCommand(UpdateCmd, MyConnection);
// 设定 UPDATE 命令字串中各个参数的数据类型与长度
MyCommand.Parameters.Add(new SqlParameter("@uid",SqlDbType.Int));
MyCommand.Parameters.Add(new SqlParameter("@SName",SqlDbType.NVarChar,30));
MyCommand.Parameters["@uid"].Value = DataGridClass1.DataKeys[e.Item.ItemIndex];
string[] Cols = new string[] {"@uid","@SName"};
int NumCols = e.Item.Cells.Count;
for (int i = 1; i<= NumCols - 3; i++)
{
TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text.Trim();
// 检查字段(储存格)的内容是否为 Null 值
if (ColValue == "")
{
Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
return;
}
// 将各字段(储存格)的数据指派给 UPDATE 命令中的参数
MyCommand.Parameters[Cols[i-1]].Value = ColValue;
}
// 开启连接
MyCommand.Connection.Open();
try
{
// 呼叫 ExecuteNonQuery() 方法以便针对数据来源执行 UPDATE 命令
MyCommand.ExecuteNonQuery();
Message.Text = "<b>已更新数据纪录</b><br>";
// 完成更新作业后使数据行跳出编辑模式
DataGridClass1.EditItemIndex = -1;
}
catch (SqlException Exp)
{
if (Exp.Number == 2627)
Message.Text = "错误: 具有相同主索引键的数据纪录已经存在。";
else
Message.Text = "错误: 无法更新数据纪录,请确定各字段是否都已正确输入。";
}
// 关闭连接
MyCommand.Connection.Close();
BindGridToSource();
}
}
--------------------------------------------------------------------------------输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误:
行 136: {
行 137: // 呼叫 ExecuteNonQuery() 方法以便针对数据来源执行 UPDATE 命令
行 138: MyCommand.ExecuteNonQuery();
行 139: Message.Text = "<b>已更新数据纪录</b><br>";
行 140:
源文件: e:\aspnet\manage\manageclass1.aspx.cs 行: 138 堆栈跟踪:
[FormatException: 输入字符串的格式不正确。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +742
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +194
localhost.manage.manageclass1.DataGridClass1_UpdateCommand(Object source, DataGridCommandEventArgs e) in e:\aspnet\manage\manageclass1.aspx.cs:138
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292源码如下:
private void DataGridClass1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
// 建立 UPDATE 命令字符串
string UpdateCmd = "UPDATE jq_class1 SET class1_name = @SName WHERE id = @uid";
SqlCommand MyCommand = new SqlCommand(UpdateCmd, MyConnection);
// 设定 UPDATE 命令字串中各个参数的数据类型与长度
MyCommand.Parameters.Add(new SqlParameter("@uid",SqlDbType.Int));
MyCommand.Parameters.Add(new SqlParameter("@SName",SqlDbType.NVarChar,30));
MyCommand.Parameters["@uid"].Value = DataGridClass1.DataKeys[e.Item.ItemIndex];
string[] Cols = new string[] {"@uid","@SName"};
int NumCols = e.Item.Cells.Count;
for (int i = 1; i<= NumCols - 3; i++)
{
TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text.Trim();
// 检查字段(储存格)的内容是否为 Null 值
if (ColValue == "")
{
Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
return;
}
// 将各字段(储存格)的数据指派给 UPDATE 命令中的参数
MyCommand.Parameters[Cols[i-1]].Value = ColValue;
}
// 开启连接
MyCommand.Connection.Open();
try
{
// 呼叫 ExecuteNonQuery() 方法以便针对数据来源执行 UPDATE 命令
MyCommand.ExecuteNonQuery();
Message.Text = "<b>已更新数据纪录</b><br>";
// 完成更新作业后使数据行跳出编辑模式
DataGridClass1.EditItemIndex = -1;
}
catch (SqlException Exp)
{
if (Exp.Number == 2627)
Message.Text = "错误: 具有相同主索引键的数据纪录已经存在。";
else
Message.Text = "错误: 无法更新数据纪录,请确定各字段是否都已正确输入。";
}
// 关闭连接
MyCommand.Connection.Close();
BindGridToSource();
}
}
建议你跟踪调试.一步一步看.应该是数据类型转换的问题.
id int 4 自增字段
class1_name nvarchar 30
我在程序里面写的是:
MyCommand.Parameters.Add(new SqlParameter("@uid",SqlDbType.Int));
MyCommand.Parameters.Add(new SqlParameter("@SName",SqlDbType.NVarChar,30));
不知道这样写对不对?
MyCommand.Parameters.Add(new SqlParameter("@uid"));
MyCommand.Parameters.Add(new SqlParameter("@SName"));
这样不行还是报错
for (int i = 1; i<= NumCols - 3; i++)
{
TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text.Trim();
// 检查字段(储存格)的内容是否为 Null 值
if (ColValue == "")
{
Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
return;
}
// 将各字段(储存格)的数据指派给 UPDATE 命令中的参数
MyCommand.Parameters[Cols[i-1]].Value = ColValue;
}
这段代码有问题,不要用for循环给字段赋值。:MyCommand.Parameters["uid"].value=int.Parse(((TextBox)e.Item.Cells[0].Controls[0]).Text);
MyCommand.Parameters["sname"].value=((TextBox)e.Item.Cells[1].Controls[0]).Text;
e:\aspnet\manage\manageclass1.aspx.cs(132,3): error CS0117: “System.Data.SqlClient.SqlParameter”并不包含对“value”的定义
e:\aspnet\manage\manageclass1.aspx.cs(133,3): error CS0117: “System.Data.SqlClient.SqlParameter”并不包含对“value”的定义
{
TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text.Trim();
// 检查字段(储存格)的内容是否为 Null 值
if (ColValue == "")
{
Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
return;
}
// 将各字段(储存格)的数据指派给 UPDATE 命令中的参数
MyCommand.Parameters[Cols[i-1]].Value = ColValue;
}
到底是什么意思?
特别是:
TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text.Trim();Cells、Controlsstring ColValue = CurrentTextBox.Text.Trim();代表什么意思?
谁能告诉我?
是什么意思啊?
你的for语句不对 你的字段 uid前没有其他列的话
那应该这样
for (int i = 0; i< NumCols - 3; i++)
{
TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text.Trim();
// 检查字段(储存格)的内容是否为 Null 值
if (ColValue == "")
{
Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
return;
}
// 将各字段(储存格)的数据指派给 UPDATE 命令中的参数
MyCommand.Parameters[Cols[0]].Value = ColValue;
}
应该就没有问题他 你视情况改