Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#

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);


                }


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
Posted
Updated 4-Feb-15 7:38am
v4
Comments
Zoltán Zörgő 30-Jan-15 7:12am    
Where exactly? On Parse? And have you debugged to see what1s in those strings? Can it really be parsed? With dates you need to care about locale also.
Member 11377180 30-Jan-15 7:14am    
im 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
Zoltán Zörgő 30-Jan-15 7:17am    
Please edit your post and mark the code row, where the exception is thrown. Is it the last one?
What is the exact DDL of the table?
Member 11377180 30-Jan-15 8:58am    
int catid = int.Parse(row.Cells[2].Text);
this line is throwing exception.
Zoltán Zörgő 30-Jan-15 9:21am    
Ok, ad what exactly is in row.Cells[2].Text in that instant?

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);
 
Share this answer
 
Hi
Please try


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

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




Regards
Dominic
 
Share this answer
 
Comments
Philippe Mori 30-Jan-15 12:44pm    
Or maybe the invariant culture or explicit format. In any case, it should be explicit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900