Creating a class to get datarow

Hi all,

Is there a way I can create a CLASS, with constructor, mainly run thought the dataset to obtain updated, deleted, modified rows. Using of sqldataadapter. Need some sample codes as soon as possible.

Thanks.
YannieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

baskar_ramCommented:
0
YannieAuthor Commented:
Hi baskar, that doesn't really help much, need a much simpler code. I got something going and this is part of the code i attached and it belongs to the compare function.

In this code, I actually check every row in the old dataset, then check that if in the new dataset contains the primary key of the old row.

But i keep getting the error that it does not contain the primary key in the table. I suppose the .contains method does not help to find the PK and i need to create a new function to check this..anyone can kindly create a new small function to replace the contain method?

Need this quite urgently. Thanks alot. :)
foreach (DataRow oRow in oldDs.Tables[0].Rows)
     {
         if (newDs.Tables[0].Rows.Contains(oRow[PKey]))
         {
              DataRow myRow = newDs.Tables[0].Rows.Find(pRow[PKey]);
 
              if (!CompareRow(pRow, myRow))
              {
                    ChangedRow.Add(myRow);
              }

Open in new window

0
YannieAuthor Commented:
Had made slight changes to the code, please refer to this instead of earlier post. Thanks.
foreach (DataRow oRow in oldDs.Tables[0].Rows)
     {
         if (newDs.Tables[0].Rows.Contains(oRow[PKey]))
         {
              DataRow myRow = newDs.Tables[0].Rows.Find(oRow[PKey]);
 
              if (!CompareRow(oRow, myRow))
              {
                    ChangedRow.Add(myRow);
              }

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

baskar_ramCommented:
Hi,

From where you get both the datasets(new and old).
From Sql Server, or DataGrid?
Are you want to update the Datas in the DataGrid?
0
baskar_ramCommented:
Hi,

I didnt get your requirement clearly
From where you get both the datasets(new and old).
From Sql Server, or DataGrid?
Are you want to update the Datas in the DataGrid to Sql Server?
0
YannieAuthor Commented:
Oh i got the new dataset from SQL..will read in from the Main() every 5 mins to get the differences..the OldDs will be copied from new dataSet...so as to compare...no dataGrid..

So just compare...then will add to the List ChangedRow.
0
YannieAuthor Commented:
I sort of did it in a way to compare the  (newDs.Tables[0].Rows.Contains(oRow[PKey])) by using
(ComparePKName(newDs)). So if it's true, it will continue with the main codes paste earlier to find myRow. Is the the right way? Cos I cant seem to use the.Contains() method. if my logic is wrong please let me know.


 
private bool ComparePKName(DataSet ds)
        {
            foreach (DataColumn col in ds.Tables[0].Columns)
            {
                if (((col.ColumnName).Equals(PrimaryKey, StringComparison.OrdinalIgnoreCase)))
                {
                    return true;
                }
            }
            return false;
        }

Open in new window

0
YannieAuthor Commented:
I also having problem with the statement

 DataRow myRow = newDs.Tables[0].Rows.Find(oRow[PKey]);

So i did my own compare function...

This will replace the statement on my main():
DataRow myRow = newDs.Tables[0].Rows.Find(oRow[PKey]);
to
DataRow foundRow = ComparePrimaryKeyValue(newDs, pRow[PKey]);

But it's getting error of "System.NullReferenceException - Object reference not set to an instance of an object"
Need some help soon!
private DataRow ComparePKValue(DataSet ds, object valuePK)
        {
            DataRow foundrow = null;

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if ((row[PK]) == (PKvalue))
                {
                    foundrow = row;
                }
            }
            return foundrow;
        }

Open in new window

0
YannieAuthor Commented:
I got it solved it, please close the question.



foreach (DataRow oRow in oldDS.Tables[0].Rows)
                {
                    if ((newDS.Tables[0].Rows.Contains(oRow[PrimaryKey])))
                    {
                        DataRow foundRow = newDS.Tables[0].Rows.Find(oRow[PrimaryKey]);
                        if (!CompareRow(oRow, foundRow))
                        {
                            ChangedRow.Add(foundRow);
                        }
                    }
                    else
                    {
                        DeletedRow.Add(oRow);
                    }
                }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.