Click here to Skip to main content
14,774,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 10-Jan-13 2:11am
v3
Comments
OriginalGriff 10-Jan-13 6:45am
   
Where did you get the error? Which line?
[no name] 10-Jan-13 7:01am
   
it means there is some null value getting to your variable.

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[^]
   
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);
   
Comments
Manas Bhardwaj 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 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
Some null value is being passeed in your data table..check it ones by using quick watch
   
Hi,

HolidayReg objHoliday = null;

then it will work fine!!!!!!!!!
   
Comments
Manas Bhardwaj 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
Top Experts
Last 24hrsThis month



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