.NET C# 输入字符串的格式不正确。

destnyhill 2012-05-18 01:08:17
类的方法
public bool SaveTeacher(string strID, string strTime, string strLesson, int iPeriod, float fScale, float fSalary)
{
string strComm = @"insert into [Teacher](ID,Time,Lesson,Period,Scale,Salary)
values(@ID,@Time,@Lesson,@Period,@Scale,@Salary)";

SqlConnection sqlConn = this.GetSqlServerConn(); //调用GetSqlServerConn()方法获得数据库连接

SqlCommand sqlComm = new SqlCommand(); //生成数据库命令操作对象
try
{

sqlComm.CommandText = strComm; //指定要执行的SQL命令

sqlComm.Connection = sqlConn; //指定要使用的SQL连接

sqlComm.Parameters.AddWithValue("@ID", strID); //为SQL命令的参数赋值
sqlComm.Parameters.AddWithValue("@Time", strTime);
sqlComm.Parameters.AddWithValue("@Lesson", strLesson);
sqlComm.Parameters.AddWithValue("@Period", iPeriod);
sqlComm.Parameters.AddWithValue("@Scale", fScale);
sqlComm.Parameters.AddWithValue("@Salary", fSalary);
sqlComm.ExecuteNonQuery(); //执行SQL命令
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
this.CloseSqlServerConn(sqlConn); //调用方法关闭数据库
}
}


程序里
bool bResult = dc.SaveTeacher(txtID1.Text.Trim(), ddlTime.SelectedItem.Text.ToString(), ddlLesson.SelectedItem.Text.ToString(),int.Parse(ddlPeriod.SelectedItem.Text.ToString()), float.Parse(ddlScale.SelectedItem.Text.ToString()),float.Parse(lblSalary.Text));


调试结果
输入字符串的格式不正确。 错误就在这一行。但我没看出来哪不对了。。求指导
...全文
1366 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xboxeer 2012-05-18
  • 打赏
  • 举报
回复
float.Parse(ddlScale.SelectedItem.Text.ToString()),float.Parse(lblSalary.Text)
这两个其中一个出错
  • 打赏
  • 举报
回复
int.Parse(ddlPeriod.SelectedItem.Text.Trim())
destnyhill 2012-05-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

stacktrack里面看下到底是哪个强制转换出错了 不然你这么多参数谁知道是哪个
[/Quote]

[FormatException: 输入字符串的格式不正确。]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7470759
System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) +115
System.Single.Parse(String s, NumberStyles style, NumberFormatInfo info) +192
System.Single.Parse(String s) +23
images_Test_Add.btnSubmit_Click(Object sender, EventArgs e) in c:\Users\Administrator\Desktop\BS\images\Test\Add.aspx.cs:24
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
destnyhill 2012-05-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

估计是你得time得格式不正确,把time换成'2001-12-12'试试,确认一下
[/Quote]

额。我主要是用来标记一个时间段的。
destnyhill 2012-05-18
  • 打赏
  • 举报
回复
如果我写成
bool bResult = dc.SaveTeacher(txtID1.Text.Trim(), ddlTime.SelectedItem.Text.ToString(), ddlLesson.SelectedItem.Text.ToString(),
int.Parse(ddlPeriod.SelectedItem.Text.ToString()), float.Parse(ddlScale.SelectedItem.Text.ToString()),float.Parse(lblSalary.Text));
他就说是第一行字符串出错,那么也就是说和下面的Parse部分无关。迷惑了
mabaolin 2012-05-18
  • 打赏
  • 举报
回复
估计是你得time得格式不正确,把time换成'2001-12-12'试试,确认一下
bdmh 2012-05-18
  • 打赏
  • 举报
回复
肯定是不符合规则 Parse转换错误,仔细检查每一个 Parse后的值
xboxeer 2012-05-18
  • 打赏
  • 举报
回复
stacktrack里面看下到底是哪个强制转换出错了 不然你这么多参数谁知道是哪个

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧