Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
DataTable dt = ds.Tables[0];
 

                HolidayReg objHoliday = new HolidayReg();
 
                for (int row = 0; row < dt.Rows.Count; row++)
                {
                    objHoliday = new HolidayReg();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        foreach (PropertyInfo prop in objHoliday.GetType().GetProperties())
                        {
 
                            if (prop.Name == dc.ColumnName && prop.CanWrite)
                            {
                                if (dt.Rows[row][dc.ColumnName] != DBNull.Value)
                                {
 
                                    prop.SetValue(objHoliday, dt.Rows[row][dc.ColumnName], null);
                                }
                            }
                        }
                    }
 

                    ListHoliday.Add(objHoliday);
                }
 

            }
            catch (Exception ex)
            {
                throw ex;
            }
 
            return ListHoliday;
        }
i got an error at catch(Expression ex)
Posted 10-Jan-13 1:42am
Edited 10-Jan-13 2:11am
v3
Comments
OriginalGriff at 10-Jan-13 6:45am
   
Where did you get the error? Which line?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

1.) You didn't get error at catch. It happened much before that and was handled by catch block.
2.) It could be any where in the code. It's difficult for us to pin point where it could be. It could as early as in first statement:
ds.Tables[0];
 
where the ds could be null. To prevent that, you should also check if the object is not null like this:
 
DataTable dt;
if(ds != null)
{
    if(ds.Tables.count > 0)
    {
        dt = ds.Tables[0];
    }
}
3.) Use Visual Studio to debug the code, and you would know where the error occured. Look at the great article from Abhijit Jana here:
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Actually it is not possible to find out the error with this little code you have posted but my assumption is you should change this line
prop.SetValue(objHoliday, dt.Rows[row][dc.ColumnName], null);
to
prop.SetValue(objHoliday, dt.Rows[row][dc.ColumnName].ToString(), null);
  Permalink  
Comments
Manas Bhardwaj at 10-Jan-13 7:18am
   
Your first statement is correct, but I don't think the assuption is correct. Your propsoed solution will still give an error if there is no row at specified index.
 
P.S. : I did not vote.
Zafar Sultan at 10-Jan-13 7:31am
   
If there is no row at specified position is also an assumption and a wrong one because the OP is using for loop, for (int row = 0; row < dt.Rows.Count; row++). In case of 0 rows the control will never enter into the block. Anyways, I will vote for your solution
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Some null value is being passeed in your data table..check it ones by using quick watch
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
 
HolidayReg objHoliday = null;
 
then it will work fine!!!!!!!!!
  Permalink  
Comments
Manas Bhardwaj at 10-Jan-13 7:43am
   
Please don't poullute the forum with irrelevent answers.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 420
1 OriginalGriff 210
2 Kornfeld Eliyahu Peter 210
3 DamithSL 210
4 mukesh mourya 85
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100