如何处理这个字符串转换异常?我的代码中有什么问题? [英] how to handle this string conversion exception? whats wrong in my code?

查看:98
本文介绍了如何处理这个字符串转换异常?我的代码中有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


foreach (GridViewRow row in GridView1.Rows)
            {
                if ((row.FindControl("chkSelect") as CheckBox).Checked)
                {
                    string dd1 = row.Cells[1].Text;
                    //DropDownList ddl = (DropDownList)row.FindControl("DropDownList1");
                    int catid = int.Parse(row.Cells[2].Text);
                    DateTime date = DateTime.Parse(row.Cells[3].Text);
                    int dcno =int.Parse( row.Cells[4].Text);
                    string company = row.Cells[5].Text;

                    string name = row.Cells[6].Text;
                    string size = row.Cells[7].Text;
                    string batch = row.Cells[8].Text;
                    DateTime exp = DateTime.Parse(row.Cells[9].Text);
                    dt.Rows.Add(dd1, catid, date, company, name, size, batch, exp);


                }





我得到 int <的异常/ code>和 datetime 类型。此代码中的数据类型与数据库中给出的数据类型匹配。然后是什么错误?对于date和exp字段数据库中的数据类型是 date not datetime。请建议更改



i'm getting exception for int and datetime types. Datatypes in this code match with those given in database. then whats the error? for date and exp fields datatype in database is date not datetime. pls suggest the changes

推荐答案

这给了我解决方案:

this gave me solution:
int catid;
                   int.TryParse(row.Cells[2].Text, out catid);
                   DateTime date;
                   DateTime.TryParse(row.Cells[3].Text, out date);
                   int dcno;
                   int.TryParse(row.Cells[4].Text, out dcno);


您好b $ b

请尝试





Hi
Please try


DateTime dt ;
DateTimeStyles dtStyles = DateTimeStyles.None;
CultureInfo culture =  CultureInfo.CreateSpecificCulture("en-US");;

DateTime.TryParse(dateTimeString, culture, dtStyles, out dt);









问候

Dominic





Regards
Dominic


这篇关于如何处理这个字符串转换异常?我的代码中有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆