取当前日期插入datagrid中碰到的问题,高手相助

binghe7 2003-04-26 12:44:01
代码如下:
mycommand.Parameters .Add (new SqlParameter ("@cidate", SqlDbType.DateTime,8 ));
mycommand.Parameters ["@cidate"].Value =DateTime.Today;

有错吗?
系统报错如下:
输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.FormatException: 输入字符串的格式不正确。

源错误:


行 252: mycommand.Parameters ["@cideduct"].Value =TextBox9.Text.Trim ();
行 253: mycommand.Connection .Open ();
行 254: mycommand.ExecuteNonQuery();
行 255: message.Style ["color"]="blue";
行 256: message.InnerHtml ="数据添加成功";

...全文
55 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
legendraingod 2003-04-26
  • 打赏
  • 举报
回复
我上次是用DateTime.Now和DateTime.Now.Add(date)往数据库中插的,没问题,可能是shortdate的事吧,可能是你DateTime后的8这个参数和数据库中的不一致吧
cmsoft 2003-04-26
  • 打赏
  • 举报
回复
SqlCommand cmd=new SqlCommand(sqlEmployee,Conn);
cmd.Parameters.Add("@empName",SqlDbType.VarChar,50);
cmd.Parameters["@empName"].Value=empName;
cmsoft 2003-04-26
  • 打赏
  • 举报
回复
DateTime.Today.ToShortDateString();
binghe7 2003-04-26
  • 打赏
  • 举报
回复
我把上面那句删除,在insert into 语句中把以前的@cidate 改为getdate();
错误依然
我数据库中其他元素的数据类型为int
其他元素的插入语句如下
mycommand.Parameters .Add (new SqlParameter ("@cirest",SqlDbType.Int ,4));
mycommand.Parameters["@cirest"].Value =TextBox3.Text.Trim ();


高手指教,兄弟俺不晓得哪儿错拉
binghe7 2003-04-26
  • 打赏
  • 举报
回复
恩,那是俺一时心急,,后来我用System.Convert.ToInt32(TextBox2.Text.Trim ());
报错信息和没改之前一样啊~
能加我Q吗
41311228
xiongliang2003 2003-04-26
  • 打赏
  • 举报
回复
faint ,我那是打个比方呀,System.Convert.ToInt16()这个是方法是把 类型转换为
整型的,看看是不是有些别的不能转换成整型的的字符而已,

mycommand.Parameters.Add(new SqlParameter ("@ciname", SqlDbType.VarChar, 20));
mycommand.Parameters ["@ciname"].Value
=System.Convert.ToInt16(TextBox1.Text.Trim ());
//-------------------------------------------
你这明显是字符型呀,你就不要转了呀
binghe7 2003-04-26
  • 打赏
  • 举报
回复
我又做了一遍,按您说的添上System.Convert.ToInt16,没用,报的错还是和刚开始一样啊
binghe7 2003-04-26
  • 打赏
  • 举报
回复
TO xiongliang2003()
我按你的方法做了,例如在修改mycommand.Parameters["@cirest"].Value
=System.Convert.ToInt16(TextBox1.Text.Trim ());
执行添加操作时它就报错如下:
输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.FormatException: 输入字符串的格式不正确。

源错误:


行 221: SqlCommand mycommand=new SqlCommand (sinsert,myconnection);
行 222: mycommand.Parameters.Add(new SqlParameter ("@ciname", SqlDbType.VarChar, 20));
行 223: mycommand.Parameters ["@ciname"].Value =System.Convert.ToInt16(TextBox1.Text.Trim ());
行 224: mycommand.Parameters.Add(new SqlParameter ("@cidate", SqlDbType.DateTime , 8));
行 225: mycommand.Parameters ["@cidate"].Value =DateTime.Today.ToShortDateString();

binghe7 2003-04-26
  • 打赏
  • 举报
回复
很感谢你不厌其烦的指教~
但我改了还是没用~
错误依旧~
天啊,没有理由了啊
xiongliang2003 2003-04-26
  • 打赏
  • 举报
回复
有空格应该没有问题吧,应该是哪个输入字符串有问题
先转换下看看,可以查出哪个错,比如

mycommand.Parameters["@cirest"].Value
=System.Convert.ToInt16(TextBox3.Text.Trim ());
cmsoft 2003-04-26
  • 打赏
  • 举报
回复
当然有空格不行了,
binghe7 2003-04-26
  • 打赏
  • 举报
回复
谢谢`
不过你的话是什么意思呢,要还是不要空格~
我记得以前写插入时也没注意这个啊,好象这里的空格无所谓吧~
cmsoft 2003-04-26
  • 打赏
  • 举报
回复
mycommand.Parameters.Add (new SqlParameter ("@cilate",SqlDbType.Int,4));


你里面有不少Parameters.Add 之间的.前面有空格
binghe7 2003-04-26
  • 打赏
  • 举报
回复
MONEY型的可以直接这样插入数据库吗》
是这个错误吗?
binghe7 2003-04-26
  • 打赏
  • 举报
回复
不至于这里的人都不能解决吧~
我顶
binghe7 2003-04-26
  • 打赏
  • 举报
回复
下面把整个添加操作源码贴出,望大家帮俺看看,谢谢拉
俺在做编辑,更新操作时是没有问题的~怪了怪了
private void Buttonaddci_Click(object sender, System.EventArgs e)
{
SqlConnection myconnection=new SqlConnection ("initial catalog=app0636;data source=localhost;uid=sa;pwd=;connect timeout=30");
string sinsert="insert into checkin (ciname,cidate,cidays,cirest,ciydays,cisdays,ciabsent,cileave,cilate,ciearly,ciover,cifill,cigo,cipay,cideduct) values(@ciname,@cidate,@cidays,@cirest,@ciydays,@cisdays,@ciabsent,@cileave,@cilate,@ciearly,@ciover,@cifill,@cigo,@cipay,@cideduct)";
SqlCommand mycommand=new SqlCommand (sinsert,myconnection);
mycommand.Parameters.Add(new SqlParameter ("@ciname", SqlDbType.VarChar, 20));
mycommand.Parameters ["@ciname"].Value =TextBox1.Text.Trim ();
mycommand.Parameters.Add(new SqlParameter ("@cidate", SqlDbType.DateTime , 8));
mycommand.Parameters ["@cidate"].Value =DateTime.Today.ToShortDateString();
mycommand.Parameters .Add (new SqlParameter ("@cidays",SqlDbType.Int,4));
mycommand.Parameters["@cidays"].Value =TextBox2.Text.Trim ();
mycommand.Parameters .Add (new SqlParameter ("@cirest",SqlDbType.Int ,4));
mycommand.Parameters["@cirest"].Value =TextBox3.Text.Trim ();
mycommand.Parameters .Add (new SqlParameter ("@ciydays",SqlDbType.Int ,4));
mycommand.Parameters["@ciydays"].Value =TextBox4.Text .Trim();
mycommand.Parameters .Add (new SqlParameter ("@cisdays",SqlDbType.Int,4));
mycommand.Parameters["@cisdays"].Value =TextBox5.Text.Trim ();
mycommand.Parameters .Add (new SqlParameter ("@ciabsent",SqlDbType.Int,4));
mycommand.Parameters["@ciabsent"].Value =TextBox6.Text.Trim ();
mycommand.Parameters .Add (new SqlParameter ("@cileave",SqlDbType.Int,4));
mycommand.Parameters["@cileave"].Value =TextBox7.Text.Trim ();
mycommand.Parameters .Add (new SqlParameter ("@cilate",SqlDbType.Int,4));
mycommand.Parameters["@cilate"].Value =TextBox10.Text.Trim ();
mycommand.Parameters .Add (new SqlParameter ("@ciearly",SqlDbType.Int ,4));
mycommand.Parameters["@ciearly"].Value =TextBox11.Text .Trim();
mycommand.Parameters.Add(new SqlParameter ("@ciover", SqlDbType.Int, 4));
mycommand.Parameters ["@ciover"].Value =TextBox12.Text.Trim ();
mycommand.Parameters.Add(new SqlParameter ("@cifill", SqlDbType.Int,4));
mycommand.Parameters ["@cifill"].Value =TextBox13.Text.Trim ();
mycommand.Parameters.Add(new SqlParameter ("@cigo", SqlDbType.Int,4));
mycommand.Parameters ["@cigo"].Value =TextBox14.Text.Trim ();
mycommand.Parameters.Add(new SqlParameter ("@cipay", SqlDbType.Money,8));
mycommand.Parameters ["@cipay"].Value =TextBox8.Text.Trim ();
mycommand.Parameters.Add(new SqlParameter ("@cideduct", SqlDbType.Money ,8));
mycommand.Parameters ["@cideduct"].Value =TextBox9.Text.Trim ();
mycommand.Connection .Open ();
mycommand.ExecuteNonQuery();
message.Style ["color"]="blue";
message.InnerHtml ="数据添加成功";
mycommand.Connection .Close ();
BindGrid();

}
binghe7 2003-04-26
  • 打赏
  • 举报
回复
没有啊,我数据库中用的是DateTime型啊~,8位有问题吗?
TO cmsoft(韦小宝是我的老乡.net)
我也尝试过你说的那种,依然是报哪个错~

哎,出了什么错误~

62,041

社区成员

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

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

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

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