Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

WinForms, DataSets, and C# - Passing DataSet bewteen two Forms

Posted on 2007-04-04
4
1,340 Views
Last Modified: 2013-11-26
MS Visual Studio 2005, SQL 2005, C#, XP Pro, WinForms

I am an ASP.Net/C# programmer with a WinForms Question:  I have two forms, A and B.  The user navs from Form A to Form B.  In Form B, I retrieve a dataset from SQL, and I give it a name (dsPrint).  The user modifies the dataset via a DataGrid in Form B.  I do not save it to SQL.  The user then returns to Form A to View and Print from the DataSet.

Question: Can you give me an example of how I identify the dsPrint dataset in Form A, so I can bind it, scan through it, etc?



0
Comment
Question by:Eric Christianson
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:dungla
ID: 18855034
1. In form A declare a DataSet with your name (ex: myDataSet).
2. In form B make DataSet dsPrint become a property with get like this:
private DataSet dsPrint;
public DataSet PrintData
{
get {return dsPrint;}
}
3. Whenever user return to form A, just retrieve the PrintData from form B
FormB formB = new FormB();
formB.ShowDialog();
myDataSet = formB.PrintData
0
 

Author Comment

by:Eric Christianson
ID: 18858964
Something is wrong.  Your code compiles - but - Here are my results:

Form A
protected DataSet myDataSet;

Form B
private DataSet dsPrint;
..
public DataSet PrintData
{
       get {return dsPrint;}
}

Form A (Print Button method)
formB FormB = new formB();
// FormB.ShowDialog();  (Commented out - no effect on results)
myDataSet = FormB.PrintData;

Problem:  During the Initialization of Form B, I retrieve a dataset that has 3 tables, the first one is empty, the others fill comboboxes.  The user selects several filters and I again retrieve the dataset, this time the first table is full.  The user selects what they want to print via a datagrid with imbedded checkboxes.  I delete the unchecked rows from the dataset, and return to Form A with the modified dataset.  All is good.  When the user presses the Print button on Form A, I run your (two) lines above.  For whatever reason, the data retrieval method on FORM B gets run again, empties out the dataset's first table and returns - overwriting the content modified by the user with an empty table.
I've tried adding code to bypass the data retrieval method in Form B, but this causes additional problems.
What am I missing here?
0
 
LVL 13

Accepted Solution

by:
dungla earned 125 total points
ID: 18862126
I see, so you don't need to create new instance of Form B, just make PrintData become public static variable

Form B
private DataSet dsPrint;
public static DataSet PrintData = null;
// Initialization Form B
... your code here
PrintData = dsPrint;

Form A (Print Button method)
myDataSet = FormB.PrintData;
0
 

Author Comment

by:Eric Christianson
ID: 18869664
Beauty - Thank you sir.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question